我正在尝试使用 ssh 隧道从我的 nodeJS Express 项目连接到我的 vpc 上的 documnetDB 集群。并收到此错误:
Mongoose connection disconnected
db.js:69
MongooseTimeoutError {message: "Server selection timed out after 15000 ms", name: "MongooseTimeoutError", reason: MongoNetworkError: getaddrinfo ENOTFOUND xxxxx-xxx…, stack: "MongooseTimeoutError: Server selection timed out a…", Symbol(mongoErrorContextSymbol): Object}
db.js:38
message:"Server selection timed out after 15000 ms"
name:"MongooseTimeoutError"
reason:MongoNetworkError: getaddrinfo ENOTFOUND xxxx-xxxx-xx-xx-xx-xx-xxx.xxxxxxx.us-east-2.docdb.amazonaws.com xxxx-xxxx-xx-xx-xx-xx-xxx.xxxxxxx.us-east-2.docdb.amazonaws.com
stack:"MongooseTimeoutError: Server selection timed out after 15000 ms\n at new MongooseTimeoutError
在代码之前有几件事:我使用了一个名为 ssh-tunnel 的包为简单起见我禁用了 TLS 我成功地通过 MongoDB 指南针连接了与我在这里得到的完全相同的参数
我担心在错误中我试图连接的 dstHost 缺少一些我在 ssh-tunnel 配置中提供的地址,而不是 xxxx-xxxx-xx-xx-xx-xx-xxx.cluster -xxxxxxx.us-east-2.docdb.amazonaws.com 我们得到 xxxx-xxxx-xx-xx-xx-xx-xxx.xxxxxxx.us-east-2.docdb.amazonaws.com 以下是代码行:
const config = require('./config');
const mongoose = require('mongoose');
const fs = require('fs');
const tunnel = require('tunnel-ssh');
const tunnelConfig = {
username:'xxx-xxxx',
// agent: process.env.SSH_AUTH_SOCK,
host:'xxx-x-xx-xx-x.us-east-2.compute.amazonaws.com',
privateKey: fs.readFileSync('./documentDBKeyPrivate.pem'),
port:22,
dstHost:'xxxxx-xxxx-xx-xx-xx-xx-xx.xxxxx-xxxxxxxx.us-east-2.docdb.amazonaws.com',
dstPort:27017,
localHost:'localhost',
localPort: 27018
};
let dbURI;
if(process.env.NODE_ENV === 'DEV') {
dbURI = config.DB_CONN_STRING_DEV;
const sshTunnel = tunnel(tunnelConfig, (error, tnl) => {
if (error) {
console.log("SSH connection error: " + error);
}
// Create the database connection
mongoose.connect(dbURI,
{
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 15000,
poolSize: 10,
}).catch(err => console.log(err));
})