एरर समाधान · डेवलपर एरर

MongoServerSelectionError: Server Selection Timed Out - Fixes

Fix MongoDB server selection timed out errors. Check Atlas IP access lists, DNS SRV, TLS, bindIp, replica set hostnames, firewalls and serverSelectionTimeoutMS.

Error messageMongoServerSelectionError: Server selection timed out after 30000 ms

लेखक: SiteDownCheck संपादकीय टीम · अपडेट: नवंबर 11, 2026 · 5 मिनट पढ़ने में

MongoDB drivers do not connect to one server and hope for the best. They first discover the topology, then select a server that fits the operation, such as a primary for writes. If no suitable server can be found within serverSelectionTimeoutMS, which defaults to 30000 ms (30 seconds), the driver raises a server selection error.

MongoServerSelectionError: Server selection timed out after 30000 ms
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.abcde.mongodb.net:27017: timed out

The message tells you the driver could not reach any usable member. The cause is usually network, DNS, TLS, authentication of the cluster's access rules, or a replica set with no primary. The details in the error, especially the topology description, are your best clue, so read the whole message, not just the first line.

Decision order

  1. Can you reach the host and port at all?
  2. Does the hostname resolve, including SRV records?
  3. Is your IP allowed and is TLS working?
  4. Is the replica set healthy, with a primary?
  5. Do the replica set member hostnames resolve and connect from the client?

Step 1: test reachability from the same machine

Run these on the host that runs your app, not your laptop:

nc -vz db.example.com 27017
mongosh "mongodb://db.example.com:27017/admin" --eval "db.runCommand({ping:1})"

For Atlas and other mongodb+srv:// strings:

dig +short SRV _mongodb._tcp.cluster0.abcde.mongodb.net
dig +short TXT cluster0.abcde.mongodb.net
mongosh "mongodb+srv://cluster0.abcde.mongodb.net/test" --username appuser

You can check an open port from the internet with the पोर्ट चेकर and examine the host's records with the DNS चेकर. If the SRV lookup returns nothing, your resolver or network blocks it. Some restrictive networks, VPNs and older DNS setups do not handle SRV queries. Using the standard (non-SRV) connection string from the Atlas "Connect" dialog is a workaround.

Step 2: IP access list (Atlas and managed services)

The top cause on Atlas is that your server's public IP is not allowed. Find it from the app host:

curl -s https://api.ipify.org

Or use What Is My IP. In Atlas, add that address under Network Access, in the IP Access List. Serverless platforms and containers often have changing outbound addresses, so you may need a fixed egress IP, VPC peering or a private endpoint. Allowing 0.0.0.0/0 makes the error disappear but exposes the cluster to the internet, so avoid it in production.

Step 3: self-hosted mongod configuration

By default, mongod binds to localhost only. A client on another host cannot connect until you change mongod.conf:

net:
  port: 27017
  bindIp: 127.0.0.1,10.0.0.5
security:
  authorization: enabled

Restart the service with sudo systemctl restart mongod. Check that it is listening with sudo ss -ltnp | grep 27017, and review the log at /var/log/mongodb/mongod.log. Open the port in the host firewall and the cloud security group, but only for your app servers. Exposing MongoDB publicly without authorization is a well-known source of data theft.

In Docker, use the service name instead of localhost, and publish the port if you connect from the host: -p 27017:27017.

Step 4: replica set problems

With a replica set connection string, the driver learns member addresses from the set's own configuration, not from the host you typed. If members are listed by internal hostnames your client cannot resolve, discovery succeeds on one server but selection then times out.

rs.status()
rs.conf().members.map(m => m.host)

Check these on the server:

  • The hostnames in rs.conf() resolve from the client machine (add DNS records or fix /etc/hosts).
  • A primary exists. If a majority of members are down, no primary is elected, and writes cannot be done. rs.status() shows the state of each member.
  • The replicaSet= name in the URI matches the real set name.
  • For testing only, directConnection=true connects to one member without discovery.

Step 5: TLS and certificates

Atlas requires TLS. Self-hosted clusters may too. TLS errors can present as server selection timeouts, with the real cause in the topology description (for example SSL routines or certificate has expired). Check the server certificate with the SSL Checker, keep the client's CA bundle up to date, and see SSL/TLS certificate errors explained. A wrong system clock also breaks certificate validation.

Step 6: tune timeouts, but only after the cause is fixed

A short timeout makes failures appear faster, which is useful for web requests, while a long one hides problems:

const client = new MongoClient(uri, {
  serverSelectionTimeoutMS: 5000,
  connectTimeoutMS: 10000,
});

Also reuse a single client per process. Creating a new MongoClient on every request, a frequent mistake in serverless functions, causes connection storms and slow selection.

Step 7: authentication and URI mistakes

Wrong credentials generally produce an authentication error rather than a selection timeout, but a URI with an unencoded password containing @ or / can corrupt the host part, so the driver tries to reach the wrong host. Encode the password with the URL Encoder/Decoder or the equivalent.

Verify

mongosh "$MONGODB_URI" --eval "db.runCommand({ping:1})"

You should see { ok: 1 }. Add a TCP check on port 27017 in your अपटाइम मॉनिटर, but keep the port closed to the public and monitor from inside your network where possible.

अगले कदम

सामान्य प्रश्न

What does serverSelectionTimeoutMS control?

How long a driver searches for a suitable server before throwing the selection error. The default is 30000 ms.

Why does mongosh work but my app does not?

They may run on different machines, with different IP addresses, DNS resolvers or environment variables. Test from the app host itself.

Is this the same as a connection refused error?

Related. A refused connection to a single host appears inside the selection error. The selection timeout is the broader failure of finding any eligible server.

Should I allow 0.0.0.0/0 in Atlas?

Not in production. Allow your app's egress IPs, or use private networking.

Get notified when this fix updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.