如何使用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
});
代码是tunnel-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:端口号.
或
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隧道连接的好模块. 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
}
这篇关于如何使用Node.js建立与MongoDB数据库的SSH隧道连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!