Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mongoose cannot reach the MongoDB Atlas cluster at all

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

Short answer

This error means the driver found no server it could reach. On Atlas, the most common cause is that your current IP address is not in the project's IP access list. Next check whether a firewall or VPN blocks port 27017, and whether the cluster is paused. Each has a one-line test below.

  • Applies to MongoDB Atlas, MongoDB Node.js driver 6, Mongoose 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Test whether your machine can open a TCP connection to a cluster node. Take the hostname from the standard connection string in Atlas. A timeout points at the network or the access list, not at your code.

    nc -zv <node-hostname> 27017
  2. 2Match what you see, then jump to its fix

    1. 1The TCP test times out, and it started after your IP changed (new network, new server, cloud deploy)Your IP address is not in the Atlas IP access listFix
    2. 2The address is allowed in Atlas, but the TCP test still fails; works on another networkA firewall or VPN blocks port 27017Fix
    3. 3Everything on your side is correct and no node respondsThe cluster is pausedFix

What does the error look like?

  • MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster
  • MongoServerSelectionError

Cause 1 of 3

Your IP address is not in the Atlas IP access list

1Check

Atlas accepts connections only from addresses in the project's IP access list. Look up your current public address (Atlas shows it when you add an entry) and compare it with Network Access in Atlas.

# Atlas UI: Security > Network Access, compare the list with your public IP

2Fix

In Atlas open Network Access and add your address. For a deployed app, add the server's fixed public address.

# Atlas UI: Security > Network Access > Add IP Address

3Verify

Restart the app and watch it connect.

node -e "require('mongoose').connect(process.env.MONGODB_URI).then(()=>{console.log('connected');process.exit(0)})"

Confirmed by mongodb.com

Did this fix it?

Cause 2 of 3

A firewall or VPN blocks port 27017

1Check

Atlas clusters use port 27017, and sharded clusters also use 27016. Corporate firewalls and VPNs often block them.

nc -zv <node-hostname> 27017

2Fix

Allow outbound TCP on 27017 (and 27016 for sharded clusters), or disconnect the VPN and test again.

# Ask the network owner to allow outbound TCP 27017 to your cluster hosts

3Verify

nc reports a successful connection.

nc -zv <node-hostname> 27017

Confirmed by mongodb.com

Did this fix it?

Cause 3 of 3

The cluster is paused

1Check

Atlas pauses idle Free clusters after 30 days with no connections. The cluster page shows a Paused label.

# Atlas UI: Database > your cluster

2Fix

Resume the cluster from the Atlas UI and wait until it is running.

# Atlas UI: Resume

3Verify

The cluster shows as active and the app connects.

nc -zv <node-hostname> 27017

Confirmed by mongodb.com

Did this fix it?

How do I stop it coming back?

  • Give deployed apps a fixed outbound address and put exactly that in the access list.
  • Set a short serverSelectionTimeoutMS in development so a bad network fails in seconds, not 30.
  • Keep one MONGODB_URI per environment and never share it between laptops and servers.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.