MongoDB: “Could not connect to any servers in your MongoDB Atlas cluster (whitelist)”
Asked Answered
R

6

6

I am having an issue which I tried to fix for a long time. I am trying to connect to Mongo Atlas cloud from nodejs viamongoose. It's not my first time but I just can't find the answer.

Mongoose version: 5.9.22

Here is my code:

  const express = require('express')
    const mongoose = require('mongoose')
    const bodyparser = require('body-parser')
    
    const app = express()
    app.use(bodyparser.json())
    
  mongoose.connect("mongodb+srv://dudvil1:[email protected]/shopping_list? 
  retryWrites=true&w=majority", {
      useNewUrlParser: true,
      useUnifiedTopology: true
  })
          useNewUrlParser: true,
          useUnifiedTopology: true
      })
      .then(() => console.log('mongo connected'))
       .catch(err => console.log(err));   
    
    const port = process.env.PORT || 5000   
    
    app.listen(port, () => console.log(`server started in port ${port}`))

My whitelist settings include only 0.0.0.0/0 My username and password in the Database access is very simple and without any special characters, but always get the same error:

 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 NativeConnection.Connection.openUri (C:\Users\Dudu\Desktop\MERN\node_modules\mongoose\lib\connection.js:826:32)
    at Mongoose.connect (C:\Users\Dudu\Desktop\MERN\node_modules\mongoose\lib\index.js:335:15)
    at Object.<anonymous> (C:\Users\Dudu\Desktop\MERN\server.js:10:10)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  message: "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/",
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'cluster0-shard-00-02.guxmm.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-00.guxmm.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-01.guxmm.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

Thanks a lot.

Repro answered 12/7, 2020 at 8:58 Comment(2)
I changed my network connection from the router to the mobile hotspot connection and it worked.Firework
i really really happy.. Sometimes, I also get this error without understanding why, but now I understand it's really easy. What you need to do is go to the project and edit the Time Zone....it's very easyEdda
M
26

You can try the following steps:

  1. Click on the Network Access.

enter image description here

  1. Click on the edit button located right next to the delete button.
  2. Click on "ADD CURRENT IP ADDRESS" Button and click confirm.

This way you will be able to access your database from any IP address.

Let me know if it works out for you :)

Mercerize answered 12/7, 2020 at 9:7 Comment(5)
first of all thanks for your help man! the true is that as i said in the question i try it alredy by put 0.0.0.0 that should let me to access from any ip address. i try also to Click on "ADD CURRENT IP ADDRESS" to let me access from my ip address only but steel not workingBuseck
Are you accessing it from the same network where it was working before. If not try switching your network because in some networks it is blocked by the firewall.Mercerize
I finally solved it. The problem was the firewall on my computer and not on the networkBuseck
@דודושטראוס How did you resolve the firewall error? I'm facing the same issue.Coltin
I am facing the same issue and on my macbook I don't have any firewall running so, not sure what's stopping my connection.Exon
D
1

I had the same problem with my strapi app, I solved the problem adding 2 options in my database config:

'AUTHENTICATION_DATABASE', null
'DATABASE_SSL', true

In strapi database.js:

options: {
    authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
    ssl: env.bool('DATABASE_SSL', true),
  },
Disinterested answered 23/8, 2021 at 4:10 Comment(0)
A
1

If you are using it for learning or testing purposes, you can select the "Allow Access from Anywhere" option. It's quick and easy.

enter image description here

Aftmost answered 29/8, 2023 at 17:37 Comment(0)
P
0

use current IP address 1)Go to network access on mongodb 2)select current Ip ADDRESS

Peerless answered 28/3, 2021 at 16:8 Comment(1)
what if we are facing this issue in production?Staciestack
N
0

I had the same problem. don't add your ip address from your terminal or network preference. Instead...

  1. Go to MongoDb Network Access, Then click edit on the ip address, or add Ip address if you haven't added any. monogodb eddit or add ip address

  2. Click add the current IP address then click confirm... vuala you're set. mongodb adding current Ip address

Number answered 13/11, 2022 at 8:3 Comment(0)
A
0

so thanks, that helped! So something really happened with my provider, and he started assigning me a dynamic IP address

mongoDB

Accidental answered 29/3 at 19:16 Comment(1)
Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From ReviewKenti

© 2022 - 2024 — McMap. All rights reserved.