Error : "Could not connect to any servers in your MongoDB Atlas cluster"

Viewed 52876

My node app gives me this message "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist".

I've already added my current IP address along with the 0.0.0.0 on the IP whitelist.

Below is a picture of the error message and the code I've written to connect it. I'm new to node.js and mongodb. I looked through all the solutions on here but none of them were able to solve this.

var express = require('express');
var bodyparser = require('body-parser');
var mongoose = require('mongoose');

var app = express();
app.use(bodyparser.json());

mongoose
     .connect("mongodb+srv://<username:password>@my-cluster.mongodb.net/test?retryWrites=true&w=majority", 
     { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true })
     .then(() => console.log( 'Database Connected' ))
     .catch(err => console.log( err ));

    app.listen(3003)

This is the error I receive after running nodemon.

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
MongooseError [MongooseServerSelectionError]: Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.
    at new MongooseServerSelectionError (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\error\serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\connection.js:823:32)
    at Mongoose.connect (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\index.js:333:15)
    at Object.<anonymous> (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\server.js:9:7)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  message: "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.",
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'cluster1-shard-00-01-m3rzz.mongodb.net:27017' => [ServerDescription],
      'cluster1-shard-00-02-m3rzz.mongodb.net:27017' => [ServerDescription],
      'cluster1-shard-00-00-m3rzz.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}


Error Message and code

15 Answers

Try doing this:- MongoDB Atlas -> NetworkAccess -> Edit -> Allow Any

This worked for me!

Are you using proxy?? please try with different internet connection first.

Follow below steps:
1] Go to https://www.mongodb.com/cloud/atlas
2] login to your mongodb atlas account
3] Click on network access

i] if you have already added ip address, then here you can see ip access list
a] click on delete 

ii] if you have not already added ip address, then just follow below steps:

4] click on add ip address
5] select 'add current ip address' or if you want you can also select 'allow access from anywhere'
6] type in comment if you want
7] click on confirm button
8] this will show pending loader, wait until it shows active status
9] now restart your server on terminal by typing 'npm start'

-- That's it!

Not sure if it is the right reason. But, its working for me when i disconnect the VPN.

When connected to VPN, I'm getting below error message.

Error: "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist".

It's not always problem with the ip. Can have problem in db name , username or password.Then also connection failed.

If the password is correct and you have allowed access from anywhere and you still getting the same error then, The problem is with your network which has a firewall. I solved my issue by just changing the network because mine was having a firewall.

At times, it may be due to system settings as well.

In my case it was server system's date & time, it was not updated to current date & time settings, hence the connection to db from the server was refused. Updated the same, and restart the server, connection was successful.

In your Mongo project, go to the network access section, and edit the IP to allow access from anywhere (0.0.0.0/0). Ensure that strong credentials (username and password) are used for all database users when allowing access from anywhere.

I was able to solve the connection problem to the server on MongoDB by doing this change.

MongoDB Atlas -> NetworkAccess -> Edit -> Allowed Access from Anywhere

I struggled with this for so long, then finally thought to check the firewall on my router. I had set it to the highest security level at one point, and set it one level lower instead, and that fixed it!

The default IP address for routers is usually 192.168.0.1, but it may differ in some cases. Just go to that IP, login as admin, and try changing your firewall settings. Of course, there's a trade off of some security, but in my case, this setting was messing up other things as well, such as smart device connectivity.

if you are used any proxy or certificate based internet then this type problem is raise.Try different internet connection.

As lakshitha-samod said, it's not only the IP address issue, It may be different

(In my case I was using my login password instead db password)

mongodb+srv://<username>:<password>@<clusterName>.p9ykp.mongodb.net/<databaseName>?retryWrites=true&w=majority

1. IP Address (for cross check)

Go to Network Access -> Add/ Edit ->Add Current IP Address or Allow Access from Anywhere

1. check DB user/ create one

Go to Database access -> create new / edit and reset the password if don't remember

2. Database name

Go to your cluster -> Browse Collections -> select a collection(database name: ex: sample_training)

I had the exact same issue here is an example broken down:

mongodb+srv://<userWithAccessToDB>:<password>@dev-12abc.mongodb.net/<yourDatabaseName>?retryWrites=true&w=majority

  1. Check the username at the spot userWithAccessToDB has access
  2. Check that the password is correct in the password part of the string
  3. Check that the database exists and you are connecting to the correct one in the yourDatabaseName slot
  4. Check that you have the +srv from the beginning of the string

mongodb+srv

To whitelist an IP address, 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. run your code it should run without any problem.

Try keeping these questions in mind when you face such an issue with your mongodB that:

Did I changed my IP address before I started working with my mongodb again after a long break? If no, then please change it because usually mongodb removes it after every 6 hours.

Did I moved from the place where I worked last time? If yes, then please change your IP again and activate it.

Please do visit the link https://studio3t.com/knowledge-base/articles/mongodb-atlas-login-ip-whitelisting/#whitelist-multiple-or-additional-ip-addresses and learn more about why you get such error and how deal with them. It helps a lot.

Related