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)
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.
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.
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.
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.
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.
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
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 .
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:
And now, the right IP address to connect with your PC is shown in IP Access List.
That's all!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With