在我的Node App中使用SSH连接到MongoDB [英] Connection to MongoDB with SSH in my Node App
本文介绍了在我的Node App中使用SSH连接到MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库与DigitalOcean一起使用,并且我试图在我的节点应用程序中连接到它。
My DB is with DigitalOcean and I'm trying to connect to it in my node app.
我发现一个名为的npm tunnel-ssh
但是无法连接到它。我的代码如下。
I've found a npm called tunnel-ssh
however am having trouble connecting to it. My code is below.
它表示数据库连接成功,但是当我执行 console.log(mongoose)
时
It says DB connection successful, however when i do a console.log(mongoose)
it shows the host and host as null.
如果执行 console.log(mongoose)
,在 console.log(数据库连接成功);
然后向我显示主机。
If I do console.log(mongoose)
, after the console.log("DB connection successful");
then it shows me the host.
var tunnel = require('tunnel-ssh');
var config = {
agent : 'myuser',
host: 'xxx:xxx:xxx:xxx'
agent : process.env.SSH_AUTH_SOCK,
privateKey:require('fs').readFileSync('id_rsa'),
port:22,
dstPort:27010,
keepAlive: true
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect('mongodb://127.0.0.1:27017/mysuperdb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log("DB connection successful");
});
});
推荐答案
这是工作代码:
var tunnel = require('tunnel-ssh');
var config = {
username : 'myuser',
host: 'xxx:xxx:xxx:xxx',
privateKey:require('fs').readFileSync('id_rsa'),
port:22,
dstPort:27010,
localPort: 2000
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect('mongodb://127.0.0.1:2000/mysuperdb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log("DB connection successful");
});
});
这篇关于在我的Node App中使用SSH连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文