MongoDB · error reference
MongoServerSelectionError: Server selection timed out
MongoServerSelectionError: Server selection timed out after 30000 ms
The driver could not find a server to talk to within 30 seconds. With Atlas that is almost always the IP Access List; self-hosted, it is often the host names the replica set advertises.
What the database is telling you
MongoDB drivers first discover the deployment — the replica set members and which is primary — and only then run commands. Server selection fails when no suitable member answers in time: the network refused or dropped the connection (ECONNREFUSED, ETIMEDOUT), the SRV DNS lookup for mongodb+srv:// failed, or the driver reached one member but cannot reach the members it was told about.
Why it shows up in a migration
A migration connects from an address the database has never seen. Atlas refuses addresses not on its IP Access List, and a self-hosted replica set configured with internal host names (mongo1:27017) hands those names to an outside client that cannot resolve them.
Common causes
- Atlas: the client's IP address is not on the project's IP Access List.
- mongodb+srv:// needs DNS SRV and TXT lookups; some networks and older resolvers block or mangle them (querySrv ENOTFOUND).
- The replica set's configured member names are only resolvable inside its own network (Docker, Kubernetes), so selection fails after the first contact.
- mongod listens on 127.0.0.1 only (net.bindIp), or the port is firewalled.
How to fix it
- 1
Atlas: add the client's public address under Network Access → IP Access List.
- 2
Self-hosted: see the names the replica set tells clients to use — every one must be reachable from the client.
rs.conf().members.map(m => m.host)
- 3
To talk to a single member without discovering the rest (for a one-off copy), connect directly.
mongodb://db.example.com:27017/shop?directConnection=true
- 4
If SRV lookups fail, use the standard (non-SRV) connection string Atlas offers for older drivers.
What DBShifts does about it
Connection diagnosis separates a refused connection from a timeout and, for a timeout, names the address to allowlist — on Atlas, the one to add to the IP Access List.
Connection diagnosis
Connecting a database?
Add the database to DBShifts and the connection is tested on the spot. If it fails, you get which of these errors it is and what to change — not the driver's raw message. Once it connects, browse it, migrate it, or let an AI agent read it through the gateway with sensitive columns masked.