MongoDB atlas connection fails with error MongoServerSelectionError: connection <monitor> to 52.64.0.234:27017 closed

Viewed 18468

I have set up a mongodb Atlas free tier cluster. When I try to connect to it with node js, it throws an error. I have white listed my IP both manually and with select current. I have also tried adding +srv to my connection url but that just causes more errors.

Here is the node js code I was trying to connect with

const { MongoClient } = require("mongodb");                                                                                                                                       

const url = "mongodb://user1:password1!@cluster0-shard-00-00-bc7dh.mongodb.net/test?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true";

const client = new MongoClient(url);

async function run() {
    try {
        await client.connect();
        console.log("Connected correctly to server");

    } catch (err) {
        console.log(err.stack);
    }
    finally {
        await client.close();
    }
}

run().catch(console.dir);

and here is the error I get

MongoServerSelectionError: connection to 52.64.0.234:27017 closed at Timeout._onTimeout (C:\Users\YOUNG\node_modules\mongodb\lib\core\sdam\topology.js:430:30) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)

people with a similar problem were able to solve it by whitelisting their ip addresses, but it hasn't worked for me. What could possibly be the problem?

I have tried allowing access for all ips but the error persists and when I use the uri with +srv, I get the following error

MongoServerSelectionError: Authentication failed.
at Timeout._onTimeout (C:\Users\YOUNG\node_modules\mongodb\lib\core\sdam\topology.js:430:30)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
9 Answers

It is because your IP address is not whitelisted . Go to network access in your mongodb atlas in your project and ADD IP ADDRESS . to test just add access from anywhere option there you go get back and try again with the connection string .

enter image description here

Go to your network access at your cluster in MongoDB

Go to network access in mongo db and add in the Ip address 0.0.0.0, and everything will be okay.

You are missing tls=true URI option in the connection string.

You should also use the SRV URI that is provided by Atlas by default which takes care of this.

const client = new MongoClient(uri, 
{
useNewUrlParser: true,
useUnifiedTopology: true,
});
client.connect(() =>
{
your_collection = client.db('your_db_name').collection('collection_name')
})

This worked for me.

I also had the same problem you encountered and found out the IP address is not correct which is set in "Network Access".

Try the followings:

  • Go to your MongoDB Atlas Project page
    • Click "Network Access"
    • Click "ADD CURRENT IP ADDRESS" button (it's very useful!)
    • Click "Confirm"

And now, the right IP address to connect with your PC is shown in IP Access List.

That's all!

For me this error was occurring because my connection string was wrong.To be very specific - I copied the sample connection string from a course I was learning and just replaced the username and password with my credentials. So, the credentials were right but not the rest of the connection string.

Just for the sake of understanding. Please see below :

  mongodb+srv://myusername:mypassword@courseproject.h1mpg.mongodb.net/?retryWrites=true&w=majority" 

myusername and mypassword are correct i.e belongs to the cluster in my MongoDB atlas account, but the rest of the string is wrong as I copied it from somewhere instead of copying it from my own MongoDB atlas account.

So please make sure to double check if your entire connection string is correct.

There is a very simple and straight forward fix for this issue,

  1. Goto your mongodb account dashboard,
  2. Enter Network Access
  3. Where you see add IP Address, add a global address, thats is access from anywhere. The images below better illustrates this procedue

Go to the network Tab of your dashboard and click in the add IP address

Fill this ass your address or just select global access or access from anywhere

And thats its, your done

You can fix the issue in a few steps.

  1. Click on the Network Access
  2. Click on the Add IP Address.
  3. Pass your current IP Address and your mongo atlas will start the work.
Related