且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 Node.js 建立到 MongoDB 数据库的 SSH 隧道连接

更新时间:2023-01-20 08:08:49

正如 mscdex 提到的,ssh2 不是用于建立到数据库的 ssh 隧道连接的好模块.隧道 ssh 更合适.

As mscdex mentioned ssh2 isn't a good module to use to make an ssh tunnel connection to a database. tunnel-ssh is more appropriate.

以下是我使用过的配置选项:

Here are the configuration options I've used :

dstPort:远程数据库连接端口

dstPort: remote database connection port

localPort:与 dstPort 相同,它将是您本地机器使用的端口

localPort: same as dstPort, It'll be the port you'll use for your local machine

用户名:SSH 用户名,

username: SSH username,

主机:SSH地址

dstHost: 数据库连接 url (...mongodbns.com) ,

dstHost: database connection url (...mongodbns.com) ,

privateKey:SSH 密钥

privateKey: SSH key

然后一旦您的隧道连接,通过猫鼬连接到您的本地主机,例如 mondodb://localhost:27000(使用您在 localPort 中定义的本地端口)

Then once your tunnel is connected connect via mongoose to your localhost such as mondodb://localhost:27000 (use the localport you defined in localPort)

var server = tunnel(config, function (error, server) {
    if(error){
        console.log("SSH connection error: " + error);
    }
    mongoose.connect('mongodb://localhost:27000/');
    //...rest of mongoose connection
}