问题描述
我有一个使用 SSH 密钥创建的 mongodb 小滴。然后,我在数字海洋上创建了一个 NodeJs 应用程序。 (使用 github repo 配置它)。在那个应用程序中,我正在使用那个 mongodb droplet 数据库。在我的 nodeJs 应用程序中访问数据库时,我遇到了这个握手问题。这两种方式我都试过了:
a) 将 sshkey 放在 github 仓库中
let mongoConfigArray = {
username: 'root',connectTimeout: 99999,host: dropletIpAddress,privateKey: require('fs').readFileSync('./config/mysshkey'),secure : true,port: 22,dstPort: 27017,};
let connectionString = 'mongodb://user:pwd@localhost:27017/dbname';
var server = tunnel(mongoConfigArray,function (error,server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect(connectionString,{ useCreateIndex: true,useNewUrlParser: true,useUnifiedTopology: true });
var db = mongoose.connection;
db.on('error',console.error.bind(console,'DB connection error:'));
db.once('open',function() {
// we're connected!
console.log("DB connection successful");
});
});
b) 或者将它放在网上的某个地方并在应用程序运行时访问它。
request.get(fileUrl,response,body) {
if (!error && response.statusCode == 200) {
// console.log(body);
mongoConfigArray.privateKey = body;
tunnel(mongoConfigArray,server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect(connectionString,useUnifiedTopology: true });
var db = mongoose.connection;
db.on('error','DB connection error:'));
db.once('open',function() {
// we're connected!
console.log("DB connection successful");
});
});
}
});
但我无法在数字海洋应用程序上成功,而这两种方式在本地都运行良好。如果有人知道如何解决此问题,将不胜感激。 谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)