Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection to MongoDB Atlas Cluster error

I am facing an issue connecting to my MongoDB Atlas cluster via Node.js, express and mongoose:

{ MongoNetworkError: connection 4 to mongodb-passport-auth-shard-00-00-vp7yg.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (C:\Users\Vishesh\Documents\Projects\nodejs-passport-auth\node_modules\mongodb-core\lib\connection\connection.js:276:9)
    at Object.onceWrapper (events.js:273:13)
    at TLSSocket.emit (events.js:187:15)
    at _handle.close (net.js:606:12)
    at TCP.done (_tls_wrap.js:386:7)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

Here is the code I used for connecting to my cluster via a Short SRV connection string:

mongoose
  .connect(
    'mongodb+srv://myname:[email protected]/test?retryWrites=true',
    { useNewUrlParser: true }
  )
  .then(() => console.log("Connected to mongodb..."))
  .catch(err => console.log(err));
like image 594
Vishesh Thakur Avatar asked Dec 29 '18 18:12

Vishesh Thakur


2 Answers

I think you must add your IP address to the IP Whitelist in mongoDB.atlas console.

mongoDB.atlass console

like image 81
eyupaltindal Avatar answered Sep 17 '22 16:09

eyupaltindal


I was getting error

MongoNetworkError: connection 1 to ......-shard-00-00.b2zap.mongodb.net:27017 closed

even if I added allow access from anywhere in IP whitelist.

After adding my IP address along with allow access from anywhere, it worked for me.

like image 43
luky Avatar answered Sep 16 '22 16:09

luky