Mongoose cannot reach the MongoDB Atlas cluster at all
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas clusterShort 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.
Fix it fast
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> 270172Match what you see, then jump to its fix
- 1
The 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
The address is allowed in Atlas, but the TCP test still fails; works on another networkA firewall or VPN blocks port 27017Fix - 3
Everything on your side is correct and no node respondsThe cluster is pausedFix
- 1
What does the error look like?
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas clusterMongoServerSelectionError
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 IP2Fix
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 Address3Verify
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
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> 270172Fix
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 hosts3Verify
nc reports a successful connection.
nc -zv <node-hostname> 27017Confirmed by mongodb.com
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 cluster2Fix
Resume the cluster from the Atlas UI and wait until it is running.
# Atlas UI: Resume3Verify
The cluster shows as active and the app connects.
nc -zv <node-hostname> 27017Confirmed by mongodb.com
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
Node.js cannot resolve the mongodb+srv address of an Atlas cluster
Error: querySrv ECONNREFUSED _mongodb._tcp.<SRV Record>
A mongodb+srv connection string needs a DNS SRV lookup before any connection starts.
4 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix