且构网

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

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

更新时间:2023-01-20 08:12:46

正如mscdex所述,ssh2不是用来与数据库建立ssh隧道连接的好模块. tunnel-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:数据库连接网址(... 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
}