Connection to MongoDB Atlas Cluster error

Viewed 10531

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:mypwd@myapp-vp7yg.mongodb.net/test?retryWrites=true',
    { useNewUrlParser: true }
  )
  .then(() => console.log("Connected to mongodb..."))
  .catch(err => console.log(err));
7 Answers

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

mongoDB.atlass console

I was getting the same error, here is the solution which resolved this issue. Add your ip in IP Whitelist.

Hope this hepls.

enter image description here

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.

Update your mongoose.connect method to avoid deprecation warnings

mongoose.connect(
  mongoAtlasUri,
  {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
  },
  () => console.log(" Mongoose is connected")
);

If you don't know your IP address then you can also this IP address- 0.0.0.0/0 ,but recommended is your own IP address.

enter image description here

I recently faced this issue, this is some kind of error from altas side. Even if you have allowed all the IPs that there is an atlas, it will still be going to throw the same error.

The way to fix it by adding your location IP. You have a specifically whitelist your own IP which is a bit strange considering, that you have allowed all.

That worked for me, I hope this helps others as well.

MondoDB version 4.7.0

I was getting below error in vs code terminal.

error - MongoServerSelectionError: connection to 3.6.8.93:27017 closed

I got the solution by reconnecting the IP address.

Just delete (if its already there) from Network access > IP access list > delete the selected IP address

then add it again. Problem solved

I was getting this error

MongoServerSelectionError: connection to <ip-addr>:27017 closed

And I solved it by going to Security > Network access > IP address and then deleting my IP address and adding a new one allowing access from anywhere.

Related