我正尝试通过nodejs通过ssh用户名/密码连接到Mongodb,如下所示:
var mongoose = require('mongoose');
var tunnel = require('tunnel-ssh');
var config = {
username : 'xyz',
host: 'xx-xxx-xx.com',
port:22,
password:'xxx',
dstPort:27017,
localPort:27017
};
console.log(tunnel);
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
console.log(server);
mongoose.connect('mongodb://localhost:27017/myDB');
var db = mongoose.connection;
console.log(db);
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log("DB connection successful");
});
});
但我收到的错误如下: 数据库连接错误:{MongoError:连接0到localhost:27017超时
这里有什么问题?
答案 0 :(得分:2)
可能是因为您错过了配置选项中的' dstHost'
' dstHost'必须是目标服务器URL。
来自官方tunnel-ssh doc
var config = {
username:'root',
Password:'secret',
host:sshServer,
port:22,
dstHost:destinationServer,
dstPort:27017,
localHost:'127.0.0.1',
localPort: 27000
};
var tunnel = require('tunnel-ssh');
tunnel(config, function (error, server) {
//....
});