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

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

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

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

生产数据库通过 SSH 隧道连接到我的 Digital Ocean 虚拟服务器,如下所示(细节已明显改变).连接到/从我的生产网站时,此设置工作正常:

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:

我现在正在从事一个不同/相关的项目,需要将我的本地机器连接到我的生产> DB,所以我假设我需要使用诸如 tunnel-ssh 之类的东西来获取它完成了.我以 this answer 为例,但我要么得到 Error: (SSH) Channel open失败:打开失败 或者它正在连接到我的开发数据库(当我使用 27017 作为我的 dstHost/dstPort/localPort 时).我一定是在考虑这个错误,或者我对我的配置很愚蠢.不可否认,我是一个完整的 Mongo/Mongoose 新手,所以两者都是同样可能的.有什么建议吗?

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); }); });

提前致谢!

推荐答案

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

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

发布评论

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

>www.elefans.com

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