Lauren Sofe
Lauren Sofe

Reputation: 13

MongoNetworkError: failed to connect to server [cluster0-shard-00-02.aprzj.gcp.mongodb.net:27017] on first connect

I am trying to connect with MongoDB URL using the mongoose module in node.js but I am not able to connect from today morning itself. It was working better until yesterday. Here are the details:

URI1: 'mongodb+srv://storeManagementSystem:[email protected]/storeManagementSystem?retryWrites=true&w=majority'

URI2: 'mongodb://storeManagementSystem:[email protected]: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'

When I use the URI1, it throws the following error

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)]: {}
}]

When I use URI1, it throws the following error

Error: querySrv ETIMEOUT _mongodb._tcp.cluster0.aprzj.gcp.mongodb.net
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19)

My approaches:

  1. I have tried searching about this issue but none helped me out. I have also deleted the previous cluster and tried with a new one but no luck on this too.
  2. I have added the 0.0.0.0/0 (includes your current IP address) to my network access to access it from anywhere.

Here are my codes

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

Upvotes: 1

Views: 2847

Answers (3)

eli
eli

Reputation: 187

Adding &ssl=true to my connection string is what worked for me.

Upvotes: 1

Techgeek
Techgeek

Reputation: 260

Try connecting using standard connection string and check if you are able to connect now ?

Upvotes: 0

Uday Koneru
Uday Koneru

Reputation: 1

Hope you have whitelisted your IP address?

Upvotes: 0

Related Questions