MongoNetworkError:首次连接时无法连接到服务器[cluster0-shard-00-02.aprzj.gcp.mongodb.net:27017]

时间:2020-06-14 05:20:04

标签: node.js mongodb mongoose

我正在尝试使用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)

我的方法:

  1. 我尝试搜索此问题,但没有一个帮助我。我还删除了以前的集群,并尝试了一个新集群,但也没有运气。
  2. 我已将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

3 个答案:

答案 0 :(得分:0)

希望您已将IP地址列入白名单吗?

答案 1 :(得分:0)

尝试使用标准连接字符串进行连接并检查是否可以立即连接?

答案 2 :(得分:0)

为我的连接字符串添加&ssl=true对我来说很有效。