25

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)
Sparksido
  • 585
  • 2
  • 11
  • 22

11 Answers11

25

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

Aravind Siruvuru
  • 1,019
  • 12
  • 7
9

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

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Rosulen
  • 101
  • 1
  • 2
7

Go to your network access at your cluster in MongoDB

Vince
  • 776
  • 11
  • 21
5

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.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • when I use the srv uri, I get an ENOTFOUND error, and when I add the tls=true option, I get an authentication error. – Sparksido Jun 12 '20 at 18:33
  • Feel free to update your question with the errors you get. For auth error, create a database user in your atlas deployment. – D. SM Jun 12 '20 at 18:36
  • I have added a new user with read and write permissions but the authentication error persists – Sparksido Jun 12 '20 at 19:37
  • Permissions do not affect whether authentication succeeds. The information you provided in insufficient to diagnose the auth issue. – D. SM Jun 12 '20 at 19:46
  • What else do I need to provide? – Sparksido Jun 12 '20 at 20:16
  • 1
    Exact steps you used to add the user, your exact URI with the password redacted. https://docs.atlas.mongodb.com/security-add-mongodb-users/ – D. SM Jun 12 '20 at 21:05
  • Also, review the server logs. There must be somewhere in the UI they can be accessed. – D. SM Jun 12 '20 at 21:06
  • I managed to get it to work, after you asked me to state my exact uri, I realised that I was using an incorrect uri I got from a code sample, thank you – Sparksido Jun 12 '20 at 21:45
3

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!

kay-adamof
  • 121
  • 1
  • 2
2
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.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 06 '21 at 06:43
2

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.
Ihtisham Khattak
  • 170
  • 1
  • 2
  • 11
0

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.

utkarsh-k
  • 836
  • 8
  • 17
0

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

Romanric Akam
  • 663
  • 1
  • 5
  • 13
0

For Cloud DB Server:

This problem mainly occurs if you're using online cloud storage. This is because you are not adding the IP address correctly. Here you can add the IP address as 0.0.0.0 as shown in the figure. If you add an IP like this, it will auto-import your machine IP address. enter image description here

0

Try Add Current IP Address in Database instead of Network Access

Januar
  • 49
  • 6