我正在尝试使用node.js中的mongoose模块与MongoDB URL连接,但是从今天早上开始我无法连接。到昨天为止,它的工作情况更好。详细信息如下:
URI1: 'mongodb+srv://storeManagementSystem:geZZtcuZOGE2ESCF@cluster0.aprzj.gcp.mongodb.net/storeManagementSystem?retryWrites=true&w=majority'
URI2: 'mongodb://storeManagementSystem:geZZtcuZOGE2ESCF@cluster0-shard-00-00.aprzj.gcp.mongodb.net:27017,cluster0-shard-00-01.aprzj.gcp.mongodb.net:27017,cluster0-shard-00-02.aprzj.gcp.mongodb.net:27017/storeManagementSystem?ssl=true&replicaSet=atlas-a7ttlm-shard-0&authSource=admin&retryWrites=true&w=majority'
当我使用URI1 时,它会引发以下错误
MongoNetworkError: failed to connect to server [cluster0-shard-00-02.aprzj.gcp.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND cluster0-shard-00-02.aprzj.gcp.mongodb.net
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
name: 'MongoNetworkError',
errorLabels: [Array],
[Symbol(mongoErrorContextSymbol)]: {}
}]
当我使用URI1 时,它会引发以下错误
Error: querySrv ETIMEOUT _mongodb._tcp.cluster0.aprzj.gcp.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19)
我的方法:
0.0.0.0/0 (includes your current IP address)
添加到我的网络访问权限中,以便可以从任何地方访问它。这是我的代码
function onListening() {
var addr = server.address()
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
('Listening on ' + bind)
console.log('API end url ', addr);
logger.debug({ level: 'info', message: 'serverOnListeningHandler | server listening on port' + addr.port + ' ', applicationrefnumber: "", applicationstatus: "serverOnListeningHandler", lastviewedpage: "" });
logger.log({ level: 'info', message: 'serverOnListeningHandler | server listening on port' + addr.port + ' ', applicationrefnumber: "", applicationstatus: "serverOnListeningHandler", lastviewedpage: "" });
let db = mongoose.connect(appConfig.db.uri, { useNewUrlParser: true, useCreateIndex: true })
console.log(db);
}
process.on('unhandledRejection', (reason, p) => {
logger.log({ level: 'info', message: 'serverOnListeningHandler | Unhandled Rejection at: Promise' + JSON.stringify(p) + ' ', reason: JSON.stringify(reason), applicationstatus: "serverOnListeningHandler", lastviewedpage: "" });
// application specific logging, throwing an error, or other logic here
})
// handling mongoose connection error
mongoose.connection.on('error', function (err) {
logger.log({ level: 'info', message: 'mongooseConnection | Mongoose DB connection error occured' + JSON.stringify(err) + ' ', applicationrefnumber: "", applicationstatus: "mongooseConnection", lastviewedpage: "" });
console.log('database connection error');
console.log(err);
}); // end mongoose connection error
// handling mongoose success event
mongoose.connection.on('open', function (err) {
if (err) {
logger.log({ level: 'info', message: 'mongooseConnection | Mongoose DB connection error occured' + JSON.stringify(err) + ' ', applicationrefnumber: "", applicationstatus: "mongooseConnection", lastviewedpage: "" });
console.log("database error");
console.log(err);
} else {
logger.log({ level: 'info', message: 'mongooseConnection | Mongoose DB connection successfull ', applicationrefnumber: "", applicationstatus: "mongooseConnection", lastviewedpage: "" });
console.log("database connection open success");
}
}); // end mongoose connection open handler
答案 0 :(得分:0)
希望您已将IP地址列入白名单吗?
答案 1 :(得分:0)
尝试使用标准连接字符串进行连接并检查是否可以立即连接?
答案 2 :(得分:0)
为我的连接字符串添加&ssl=true
对我来说很有效。