Home › Databases

MongoServerSelectionError: Server selection timed out after 30000 ms — how to fix

MongoServerSelectionError: Server selection timed out after 30000 ms
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
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.abcde.mongodb.net:27017: connection closed, Timeout: 30s

Quick fix: Atlas: add your current IP under Network Access (or 0.0.0.0/0 temporarily to test). Then test the connection with mongosh "<your connection string>". Local or Docker single-node replica set: add directConnection=true to the URI or fix the member hostnames. Use 127.0.0.1 instead of localhost if ECONNREFUSED ::1 shows up.

What it means

Before every operation, MongoDB drivers do server selection: they pick a reachable server of the right type (for example the primary for writes). If no such server is found within serverSelectionTimeoutMS (30 seconds by default), you get this error. The timeout itself isn't the problem. Something prevents the driver from reaching any suitable server.

The detailed reason is usually inside the error. Print the whole error object (in Node, err.reason, which lists each server and its last error).

Cause 1: your IP isn't on the Atlas IP Access List

Atlas refuses connections from IPs not listed under Security → Network Access. Your home IP changes, a new office/VPN/cloud host has a different IP, so it "suddenly" stops working.

Fix: in Atlas, open Network Access → Add IP Address → Add Current IP Address. For servers, add the server's public egress IP (for PaaS hosts, see their docs for static outbound IPs). 0.0.0.0/0 allows everyone and is fine for a quick test, but restrict it again afterwards.

Changes take a minute or two to apply.

Cause 2: wrong connection string or credentials

Copy the string again from Database → Connect → Drivers and test it outside your app:

mongosh "mongodb+srv://user:PASSWORD@cluster0.abcde.mongodb.net/mydb"

Special characters in the password (@, :, /, %) must be percent-encoded. The URL encoder does that. Authentication failures normally have their own error (see bad auth), but some drivers only show the selection timeout.

Cause 3: DNS can't resolve the mongodb+srv record

mongodb+srv:// needs DNS SRV and TXT lookups. Some routers, corporate DNS servers and VPNs don't answer them, and you see querySrv ECONNREFUSED or querySrv ETIMEOUT before the timeout.

nslookup -type=SRV _mongodb._tcp.cluster0.abcde.mongodb.net

Fix: use a public DNS resolver on the machine, or use the standard (non-SRV) connection string Atlas offers for older drivers, which lists the hosts directly.

Cause 4: firewall or network blocks port 27017

Many corporate and school networks block outbound 27017. Test from the same machine:

nc -vz cluster0-shard-00-00.abcde.mongodb.net 27017

If it times out, try another network (phone hotspot) to confirm, then ask for the port to be opened.

Cause 5: local replica set with unreachable member names

A replica set (including a single-node one in Docker) tells the driver its members' hostnames from its config. If that says mongo1:27017 or a container ID, a client on the host can't resolve it, even though the initial connection worked.

Fix: connect straight to the one node:

mongodb://127.0.0.1:27017/mydb?directConnection=true

Or configure the replica set with hostnames the clients can resolve (rs.conf() shows them).

Cause 6: localhost resolves to ::1

Node 17+ may resolve localhost to IPv6 ::1 while mongod listens only on 127.0.0.1, which gives ECONNREFUSED ::1:27017 inside the selection error. Use mongodb://127.0.0.1:27017. See connect ECONNREFUSED 27017.

Fail faster while debugging

await mongoose.connect(uri, { serverSelectionTimeoutMS: 5000 });

Last updated 2026-12-04.