使用Mongoose的Node.js SSH隧道传输到MongoDB

编程入门 行业动态 更新时间:2024-10-22 16:22:11
本文介绍了使用Mongoose的Node.js SSH隧道传输到MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个Mongo数据库.一个用于我的开发环境,一个用于生产,如我的Robomongo设置所示:

生产数据库通过SSH隧道传输到我的Digital Ocean虚拟服务器,如此处所示(具体信息已明显更改).当连接到我的生产网站或从我的生产网站连接时,此设置可以正常工作:

我现在正在从事其他项目/相关项目,需要将我的 本地 机器连接到我的 生产 > DB,所以我认为我需要使用 tunnel-ssh 之类的东西完成了.我以此答案为例,但我却得到了Error: (SSH) Channel open failure: open failed或它正在连接到我的开发数据库(当我将27017用作dstHost/dstPort/localPort时).我必须考虑这个错误,否则我的配置会变得愚蠢.我无疑是Mongo/Mongoose的新手,因此两者都有可能.有什么建议吗?

var fs = require("fs"); var mongoose = require('mongoose'); var tunnel = require('tunnel-ssh'); //===== db connection ===== var config = { username:'my.username', host:'my.ip.address', agent : process.env.SSH_AUTH_SOCK, privateKey:require('fs').readFileSync('/Users/myusername/.ssh/id_rsa'), port:22, dstHost:'mongodb://localhost:27000/mydbname', dstPort:27000, localHost:'127.0.0.1', password:'mypassword', localPort: 27000 }; var server = tunnel(config, function (error, server) { if(error){ console.log("SSH connection error: " + error); } mongoose.connect('mongodb://localhost:27000/mydbname'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'DB connection error:')); db.once('open', function() { // we're connected! console.log("DB connection successful"); // console.log(server); }); });

提前谢谢!

解决方案

dstHost必须是主机名/IP,而不是MongoDB连接字符串.在这种情况下,实际上您可以省略dstHost,localHost和localPort,因为它们已经默认为您提供的值.

I have two Mongo DBs. One for my dev environment, one for production as seen here from my Robomongo setup:

The production db is SSH tunneled to my Digital Ocean virtual server as seen here (specifics have been changed obviously). This setup works fine when connecting to/from my production website:

I am now working on a different/related project and need to connect my local machine to my production DB, so I assumed I'd need to use something like tunnel-ssh to get it done. I've followed this answer as an example, but I'm either getting Error: (SSH) Channel open failure: open failed OR it's connecting to my Dev db (when I use 27017 as my dstHost/dstPort/localPort). I must be thinking about this wrong or I'm being dumb with my configuration. I am admittedly a total Mongo/Mongoose novice, so both are equally possible. Any advice?

var fs = require("fs"); var mongoose = require('mongoose'); var tunnel = require('tunnel-ssh'); //===== db connection ===== var config = { username:'my.username', host:'my.ip.address', agent : process.env.SSH_AUTH_SOCK, privateKey:require('fs').readFileSync('/Users/myusername/.ssh/id_rsa'), port:22, dstHost:'mongodb://localhost:27000/mydbname', dstPort:27000, localHost:'127.0.0.1', password:'mypassword', localPort: 27000 }; var server = tunnel(config, function (error, server) { if(error){ console.log("SSH connection error: " + error); } mongoose.connect('mongodb://localhost:27000/mydbname'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'DB connection error:')); db.once('open', function() { // we're connected! console.log("DB connection successful"); // console.log(server); }); });

Thanks in advance!

解决方案

dstHost needs to be a hostname/IP, not a MongoDB connection string. In this particular case, you can actually omit dstHost, localHost, and localPort in this particular case because they already default to the values you're providing.

更多推荐

使用Mongoose的Node.js SSH隧道传输到MongoDB

本文发布于:2023-11-22 19:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618690.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:隧道   Node   Mongoose   js   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!