Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:[email protected]/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)
like image 761
Sparksido Avatar asked Jun 12 '20 11:06

Sparksido


People also ask

How do I fix Mongoserverselectionerror?

Whitelist your current IP address in your mongo DB cluster: Click on Add IP Address in the top-right corner. This will open the Add IP Whitelist Entry dialog. Click on Add current IP address. MongoDB will automatically detect your current address, then click Confirm.

How do you whitelist IP address in MongoDB?

To whitelist multiple IP addresses, go to your target cluster on MongoDB Atlas. Next, go to Network Access under the Security tab. On the IP Whitelist tab, click on Add IP address. Type your IP address manually under Whitelist Entry, then click Confirm.

How to resolve “MongoDB connection error failed to connect to server [localhost]”?

Sometimes, website owners may face error like “MongoDB connection error: MongoError: failed to connect to server [localhost:27017]”. This error occurs when a firewall blocks the connection from an IP address. In order to solve this error, our Support Engineers allow IP address in the firewall configuration file based on the client request.

How to fix MongoDB Atlas connection is not working?

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 . Show activity on this post. Show activity on this post.

Why does MongoDB fail to connect to my cluster?

If you try to connect when you are at this limit, MongoDB displays an error stating connection refused because too many open connections. For a detailed comparision of cluster tiers and their maximum concurrent connections, see Connection Limits and Cluster Tier.

Why doesn't my client connect to MongoDB with TLS?

If it does not connect then it could be your MongoDB instance is configured to not permit non-TLS connections, in which case: If you pass in a secureContext to your client then it will be passed directly to the TLS Socket construction: If it connects then we know there is a bug in the driver somewhere but also we'll have a workaround


Video Answer


6 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

like image 75
Aravind Siruvuru Avatar answered Oct 26 '22 00:10

Aravind Siruvuru


Go to your network access at your cluster in MongoDB

like image 32
Vince Avatar answered Oct 26 '22 00:10

Vince


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

like image 43
Rosulen Avatar answered Oct 25 '22 23:10

Rosulen


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.

like image 22
D. SM Avatar answered Oct 25 '22 23:10

D. SM


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.

like image 26
Omkar Mankar Avatar answered Oct 25 '22 23:10

Omkar Mankar


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!

like image 25
kay-adamof Avatar answered Oct 26 '22 00:10

kay-adamof