Shardherdfor MongoDB Atlas
Guide

Fix MongoServerSelectionError: server selection timed out

By the Shardherd team · Updated · 3 min read · Sources

Short answer

MongoServerSelectionError means the driver could not reach any usable server in your cluster before serverSelectionTimeoutMS ran out (30 seconds by default). With MongoDB Atlas the cause is almost always that your current public IP is not on the project's IP access list. After that, check whether the cluster is paused, whether DNS can resolve the mongodb+srv host, and whether a firewall blocks outbound port 27017.

What the error looks like

The Node.js driver throws it after waiting out the server selection timeout:

MongoServerSelectionError: Server selection timed out after 30000 ms

Mongoose wraps it and, when the host is an Atlas cluster and no server answered, adds a hint:

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.

Variants carry the underlying network error instead, for example MongoServerSelectionError: connect ECONNREFUSED or connect ECONNRESET, or a DNS failure such as querySrv ENOTFOUND _mongodb._tcp.cluster0.abcd1.mongodb.net. Other drivers say the same thing in their own words; the Java driver, for example, reports that it timed out after 30000 ms while waiting for a server. The 30 seconds is the default of the serverSelectionTimeoutMS connection option.

Note what it is not: if the message says bad auth : authentication failed, the driver reached the cluster and the password or user is wrong. That has its own fix in bad auth : authentication failed.

Causes, most common first

CauseTell-tale signFix
Your IP isn't on the project's IP access listWorks from one network, times out from another; worked yesterday on a home connectionAdd your current public IP in Database & Network Access, ideally as a temporary entry. See how to whitelist an IP
The cluster is pausedEvery client fails; a free cluster unused for 30 daysResume it in Atlas (M10+ clusters also resume themselves after 30 days)
DNS can't resolve the SRV recordquerySrv ENOTFOUND or ECONNREFUSED on _mongodb._tcp…Check the host name; try a public DNS resolver; as a last resort use the standard mongodb:// string
A firewall blocks outbound 27017Fails only on the office or corporate networkAllow outbound TCP 27015 to 27017 to Atlas (27016 too for sharded clusters)
The app's server IP changedFails after a redeploy on a platform without fixed IPsUse a static egress IP or private networking, not 0.0.0.0/0
The cluster no longer exists or the host is mistypedENOTFOUND on a name you can't find in AtlasCopy a fresh connection string from Atlas

A five-minute diagnosis

  1. Check the cluster. In Atlas, is it running? A paused cluster or one still provisioning won't accept connections.
  2. Check your IP. Look up your public IP and compare it with the project's IP Access List. Add it if missing, then wait until its status reads Active.
  3. Check DNS. Ask for the SRV record behind your mongodb+srv:// host. If this fails, the driver will too:
    nslookup -type=SRV _mongodb._tcp.cluster0.abcd1.mongodb.net
  4. Check the port. From the same machine, test that outbound port 27017 is open, for example with nc -vz against one of the hosts the SRV lookup returned, or MongoDB's suggested portquiz.net:27017.
  5. Connect with mongosh. mongosh "mongodb+srv://cluster0.abcd1.mongodb.net/" --username app_user. If mongosh connects from the same machine, the network is fine and the problem is in the app's configuration or environment variables.

Connection timeouts: don't just raise the number

When a connection times out, it is tempting to raise serverSelectionTimeoutMS or connectTimeoutMS. That only makes a blocked connection fail more slowly. Raise a timeout only when you have measured real latency to a far-away region; MongoDB's Node.js docs note that connectTimeoutMS must not be lower than the slowest member's latency, or the driver will never reach that member.

Two other timeout-shaped problems look similar but are different:

  • Pool exhaustion. Messages like timed out while checking out a connection from connection pool mean every connection in the client's pool (maxPoolSize, 100 by default in Node.js) is busy. Reuse one client per process instead of creating one per request.
  • Too many connections on the cluster. Atlas limits concurrent connections by tier (500 on the free tier). At the limit you'll see connection refused because too many open connections; close idle clients or scale up.

On serverless platforms and CI

Serverless functions, CI runners and many hosting platforms send traffic from changing addresses, which is the classic cause of an error that comes and goes. Options, best first: private endpoints or VPC peering on a dedicated cluster; a static outbound IP from your platform added to the access list; for CI, a step that adds the runner's IP as a temporary entry with the Atlas CLI (atlas accessLists create --currentIp --deleteAfter …) before the tests run. Opening the project to 0.0.0.0/0 should be the last resort, not the first.

Questions

Why do I get MongoServerSelectionError only on some networks?

Because Atlas only accepts connections from addresses on the project's IP access list. Your home, office, phone hotspot and servers all have different public IPs, and each one needs an entry (or private networking).

What is the default serverSelectionTimeoutMS?

30,000 milliseconds (30 seconds). That is why the Node.js message usually says "Server selection timed out after 30000 ms".

Does querySrv ENOTFOUND mean my IP is blocked?

No. It means DNS could not find the SRV record for the mongodb+srv host: a typo, a deleted or paused cluster, or a DNS resolver that doesn't handle SRV lookups. The access list comes into play only after DNS works.

Is it safe to fix this with 0.0.0.0/0?

It removes the error by letting any IP connect, leaving only the database password between the internet and your data. Use specific IPs, temporary entries or private networking instead.

Sources

Checked on 28 November 2026. Prices and limits change; the linked pages are the authority.

  1. Troubleshoot Connection Issues (Atlas)www.mongodb.com
  2. Connection Troubleshooting (Node.js driver)www.mongodb.com
  3. Connection String Optionswww.mongodb.com
  4. Mongoose MongooseServerSelectionError sourcegithub.com

Shardherd brings your Atlas clusters to iPhone.