How to Fix MongoDB Server Selection Timeout Error
In this tutorial, you'll learn about How to Fix MongoDB Server Selection Timeout Error. We cover key concepts, practical examples, and best practices.
The Problem
Your application tries to connect to MongoDB and gets:
MongooseServerSelectionError: Server selection timed out after 30000 ms
Or:
MongoTimeoutError: Server selection timed out after 30000 ms
MongoDB driver could not select a server to send the operation within the timeout window. The driver could not reach any of the configured hosts.
Quick Fix
1. Check network connectivity
Verify reachability from the application host:
# Ping the MongoDB host
ping -c 4 mongodb-host
# Check port 27017
nc -zv mongodb-host 27017
# Trace the route
traceroute mongodb-host
If the host is unreachable, check the network, VPN, or DNS settings.
2. Check replica set health
If using a replica set, check the status:
rs.status()
Expected output shows all members with stateStr: "PRIMARY" or "SECONDARY". If a member shows "(not reachable/healthy)", restore connectivity or reconfigure the replica set.
3. Increase the server selection timeout
The default timeout is 30 seconds. Increase it in slow networks:
// Wrong — default 30000ms may be too short
mongoose.connect('mongodb://host:27017/mydb')
// Right — increase timeout
mongoose.connect('mongodb://host:27017/mydb', {
serverSelectionTimeoutMS: 60000,
connectTimeoutMS: 30000,
socketTimeoutMS: 45000
})
4. Check the connection string
A wrong hostname or port causes server selection timeout:
# Wrong — typo in hostname
mongodb://mongod-local:27017/mydb
# Right
mongodb://mongodb-local:27017/mydb
For replica sets, list all members:
mongodb://host1:27017,host2:27017,host3:27017/mydb?replicaSet=rs0
5. Check connection pool size
Too many concurrent operations can exhaust the connection pool:
// Wrong — default pool may be too small
mongoose.connect('mongodb://host:27017/mydb')
// Right — increase pool size for high concurrency
mongoose.connect('mongodb://host:27017/mydb', {
maxPoolSize: 50,
minPoolSize: 5
})
6. Check DNS resolution
SRV connection strings rely on DNS:
# Test SRV resolution
nslookup -type=SRV _mongodb._tcp.cluster.mongodb.net
# Test standard resolution
nslookup mongodb-host
Prevention
- Monitor network latency and packet loss between application and database.
- Use a connection pooler or proxy for high-traffic applications.
- Set up replica set monitoring with alerts for unhealthy members.
- Keep
serverSelectionTimeoutMSat 30-60 seconds for production.
Common Mistakes with timeout
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world MONGODB code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro