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

I keep getting this error although I have allowed access from anywhere in the Network access section of the cluster. (Screenshot of the same is attached below)

here is the connection code

`const mongoose = require('mongoose');
const mySecret = process.env['mongoUrl']
const intialDbConnection = async () => {
  try {
    await mongoose.connect(mySecret, {
      useNewUrlParser: true,
      useUnifiedTopology: true
    })
    console.log("db connected")
    
  }
  catch (error) {
    console.error(error);
  }
}

module.exports = { intialDbConnection }`

**

And the entire code is here https://replit.com/@KumaraswamyA/store-cipher-backend-1

**(store-cipher-backend-1 - Replit)

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 (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/connection.js:824:32)
    at /home/runner/store-cipher-backend-1/node_modules/mongoose/lib/index.js:380:10
    at /home/runner/store-cipher-backend-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/index.js:1225:10)
    at Mongoose.connect (/home/runner/store-cipher-backend-1/node_modules/mongoose/lib/index.js:379:20)
    at intialDbConnection (/home/runner/store-cipher-backend-1/db/db.connect.js:5:20)
    at Object.<anonymous> (/home/runner/store-cipher-backend-1/index.js:15:1) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'cluster0-shard-00-00.fkvic.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-01.fkvic.mongodb.net:27017' => [ServerDescription],
      'cluster0-shard-00-02.fkvic.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-on2j4f-shard-0',
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}

What do you get when you try to go at:

http://portquiz.net:27017/
2 Likes

This server listens on all TCP ports, allowing you to test any outbound TCP port.

You have reached this page on port 27017 (from http host header).

Your network allows you to use this port. (Assuming that your network is not doing advanced traffic filtering.)

Network service: unknown
Your outgoing IP: 59.91.26.158

did you do this from replit console or your pc? where do you run your app? replit, work pc or home pc?

I have tried and replit has no problem connecting Atlas clusters.

One possibility is that you just haven’t waited long enough when you added “0.0.0.0” to your network list. for me, it took about 15-20 seconds for the change to take effect.

  1. I did that from a browser from my home PC(brave) not from replit console. (I run my app on home pc and the backend is in replit)
  2. Yea I checked a while back the DB was connected but now again the same issue has been arrised.
  3. It’s been more than 2 days since adding this “0.0.0.0” and the issue arrises sometimes and it does connect sometimes.
1 Like

is that free or powered? free repls are stopped when you left the page or after a while.

or ports got a problem somehow. try this on repl’s “shell”: curl http://portquiz.net:27017/

it should show “Port test successful!” and give the IP of that repl. try running your backend after this test and see if it connects.

edit: do this also when you get that error and see if the port test succeeds.

  1. Checked in replit.
  2. Yes, It’s a free repl, and yes it stops but once I restart the server, The server gets connected successfully and the MongoDB error raises. (DB doesn’t connect)
  3. The port test succeeds but the error isn’t resolved. The port test is always successful.
1 Like

Today, I could finally get the same problem you are experiencing.

Then, I did a side-by-side test with replit and Compass. Removing and re-adding the access from everywhere. Compass had no problem connecting.

It seems the problem arises from the container (virtual machine) our repl is started. I got two different addressses that both succeeded portquiz.net test but one failed to connect mongodb:

23.251.145.77 → Failed
35.203.153.221 → Connected

I have sent a bug report within the repl. I can’t say what happens next. I suggest you close/reopen the repl by going back to “My Repls” page, waiting a bit, and re-open it. check if you connect. if not, try again.

1 Like

Struggling for 2 days with the same connection issue. Tried suggested test methods and all of them are passed however still receiving error from mongodb.
Waiting for a solution impatiently (my certification got stuck halfway).

Hi @Valikhan_Dumshebayev, Although the error is generic, the problem here turned out to be related to replit.com container. If your case is also related to a repl, please read my post just above yours. Else read on.

The root of the problem is the strict access rules on the host network your app runs. You might be behind a strict school/work network that prevents certain connections to the internet. In these cases, you need to move to a more permissive network such as home or mobile.

PS: It is still a mystery to me why we can connect to portquiz.net but not to mongodb servers.

I am on my home network with no restriction. Actually, for me it has started at Saturday, then all of a sudden it got connected on Monday for some time and get disconnected again. Didn’t check on Sunday, though.

Can you please make a new post, include the full error message you get, and include a link here (because of irregularities)? That will allow both us community helpers and the mongodb staff to look with fresh sight.

Sure.
No connection to MongoDB from Replit - MongoDB Atlas - MongoDB Developer Community Forums

I have been doing this and sometimes it just doesn’t work.

I know it is a pain to get a working container :frowning_face: just continue with your luck.

Please also consider sending a bug report from within the repl (help button on bottom-left) about this problem so to make replit team aware of the situation.

Yep. I just did that.
Thanks for taking the time and helping me out. :raised_hands:
I will update you once the issue gets resolved.

1 Like

If you are following this thread, here is the temporary solution provided by replit support team.

“You can run “kill 1” in the shell of your Repl which will reboot it and may fix the issue, although it isn’t a 100% reliable workaround.”

Apart from this the solution that has worked for me is changing the file name and running, the DB gets connected. In case you have already used the APIs in the frontend, after doing the above step change back your file name to the old one and the DB gets connected without any issues.

Cheers.

1 Like

that is a weird one.

1 Like

maybe the repl becomes active with different IP when we change the repl file name.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.