skip to Main Content

Error I get when I try to connect atlas mongodb instance using Express.

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
    at Connection.openUri (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/connection.js:825:32)
    at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:411:10
    at /Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:1285:10)
    at Mongoose.connect (/Users/aviralgupta/Development/NodeJs/MongoDB/node_modules/mongoose/lib/index.js:410:20)
    at connectDB (/Users/aviralgupta/Development/NodeJs/MongoDB/config/dbConfig.js:8:24)
    at Object.<anonymous> (/Users/aviralgupta/Development/NodeJs/MongoDB/index.js:12:1)
    at Module._compile (node:internal/modules/cjs/loader:1275:14) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-a6to6lu-shard-00-00.nfmpr49.mongodb.net:27017' => [ServerDescription],
      'ac-a6to6lu-shard-00-01.nfmpr49.mongodb.net:27017' => [ServerDescription],
      'ac-a6to6lu-shard-00-02.nfmpr49.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-tvdhx4-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
Error in dbConfig.js

dbConfig.js

const mongoose = require("mongoose");

mongoose.set('strictQuery', true);

const connectDB = async () => {

    try {
        await mongoose.connect(process.env.MONGO_URL, {
            useUnifiedTopology: true, 
            useNewUrlParser: true,
        });


    } catch (error) {
        console.error(error);
        console.error("Error in dbConfig.js");
    }

}

module.exports = connectDB; 

As it mentions that you're trying to access the database from an IP that isn't whitelisted I tried to add my current IP address but still shows the same problem.

Also this project was running perfectly fine a week ago, but now its not working and also one thing to note is that I did not changed my Internet connection when I builded, tested it but now its not working.

3

Answers


  1. For now, until you get a static IP just allow any IP address to avoid the hassle of adding the new IP every time.

    enter image description here

    You don’t need to change the ISP(Internet Service Provider) to get a different IP address.Every time you connect to the internet, your ISP (Internet Service Provider) gives you an IP address.

    Visit Mongo DB dashboard and change the IP whitelist to your current IP by pressing the button.

    Login or Signup to reply.
  2. The above comments are correct but if it still doesn’t fix the issue
    Update your systems clock.
    The clock not been Updated can cause issues like this

    sudo ntpdate -s time.nist.gov
    
    Login or Signup to reply.
  3. if all the above solutions provided do not solve the issue. then check your network connection if you have data(MB), I mean your network provider(wifi).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search