如何使用 Node.js 建立到 MongoDB 数据库的 SSH 隧道连接 [英] How to use Node.js to make a SSH tunneling connection to a MongoDB database
问题描述
我的凭据与 Robomongo 完美配合,但我无法与 node.js 建立连接
我尝试使用 ssh2 和 tunnel-ssh npm 模块建立连接,但两次都失败了.
- mongo 连接不需要密码
- ssh 连接是使用 pem 密钥建立的
My credentials work perfectly with Robomongo but I can't make the connection with node.js
I have tried to make the connection using ssh2 and tunnel-ssh npm module and failed both times.
-The mongo connection does not require a password
-The ssh connection is made with a pem key
这是我在 ssh2 模块中使用的代码,我可以正确建立隧道,但 mongo 连接失败
This is the code I've used with ssh2 module, I can establish the tunneling correctly but the mongo connection fails
var Client = require('ssh2').Client;
var conn = new Client();
conn.on('ready', function() {
console.log('Client :: ready');
//mongo connection
mongoose.connect('mongodb://localhost:27000/');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("database connection established");
var users = db.collection('user');
var getallUsers = function (date, callback){
users.find({}).toArray(function(err,data){
callback(data);
})
};
getallUsers(null, function (data){
console.log('data :'+ data);
});
});
//end of mongo connection
}).connect({
host: '**.**.**.**.**',
port: 22,
username: 'ec2-user',
privateKey: key
});
以及隧道 ssh 的代码
And the code the tunnel-ssh
var config = {
dstPort: 27000,
user: 'ec2-user',
host: '**.**.**.**.**',
privateKey: key
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
console.log('database connection initalizing');
mongoose.connect('mongodb://localhost:27000/');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("database connection established");
var users = db.collection('user');
var getallUsers = function (date, callback){
users.find({}).toArray(function(err,data){
callback(data);
})
};
getallUsers(null, function (data){
console.log(data);
});
});
});
我不确定是在建立隧道后使用常规的MongoDB连接字符串还是将数据库称为localhost例如
mongodb://localhost:portnumber.
或
mongodb://databasepath.subpath.mongodbdns.com:27000
I'm not sure whether to use the regular MongoDB connection string after establishing the tunnel or referring to the database as localhost such as
mongodb://localhost:portnumber.
or
mongodb://databasepath.subpath.mongodbdns.com:27000
Localhost 给了我一个权限被拒绝的错误,后者给了我一个超时
Localhost gives me a permission denied error, the latter gives me a timeout
推荐答案
正如 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
}
这篇关于如何使用 Node.js 建立到 MongoDB 数据库的 SSH 隧道连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!