How to fix MongoNetworkError: connection timeout in MongoDB
This error occurs when your MongoDB client fails to establish a connection within the configured timeout period. It typically happens due to network issues, firewall blocking, misconfigured timeout settings, or unreachable database servers.
Quick fix
Fixes MongoNetworkError: connection timeout
# Test connectivity to MongoDB (port 27017 by default)
telnet mongodb-host.example.com 27017
# Or using nc
nc -zv mongodb-host.example.com 27017- Verify MongoDB server is running and accessible
- Increase connectTimeoutMS setting
- Check and configure firewall rules
- Set appropriate socketTimeoutMS
# Test connectivity to MongoDB (port 27017 by default)telnet mongodb-host.example.com 27017# Or using ncnc -zv mongodb-host.example.com 27017MongoNetworkError: connection timeout
What this error means
MongoNetworkError: connection timeout indicates that your application failed to establish a TCP connection to the MongoDB server within the specified timeframe. The connection timeout determines the maximum amount of response time allowed for the client to connect to the MongoDB instance. This error can occur during initial connection attempts or when the driver tries to reconnect to the database after a connection drop.
Typical symptoms
- Application fails to start with a MongoNetworkError exception
- Database operations hang and eventually fail with timeout error
- Connection succeeds intermittently but fails other times
- Error occurs after application runs for some time (minutes to hours)
- AWS Lambda or serverless deployments timeout on first database call
- Connection works on local machine but fails in production
Common causes
- connectTimeoutMS setting is too low for your network latency
- Firewall blocking MongoDB port (default 27017) on server or client network
- MongoDB server is unreachable or offline
- IP address not whitelisted in MongoDB Atlas network access list
- Network latency exceeds configured timeout value
- DNS resolution delays or DNS server issues
- MongoDB replica set nodes are slow to respond to connection attempts
- Connection pool exhaustion or resource limits
How to fix "MongoNetworkError: connection timeout"
First, confirm that your MongoDB instance is running and listening on the expected port. Test connectivity using telnet or nc:
# Test connectivity to MongoDB (port 27017 by default)
telnet mongodb-host.example.com 27017
# Or using nc
nc -zv mongodb-host.example.com 27017If the connection fails, ensure the MongoDB service is running and the hostname/port are correct.
The default connectTimeoutMS is often too low for networks with high latency. Increase it to at least 10000ms (10 seconds). For high-latency networks, use 30000ms or higher:
// Node.js/Mongoose
const uri = 'mongodb+srv://user:password@host/database?connectTimeoutMS=30000';
const client = new MongoClient(uri);
// Or using connection options
const client = new MongoClient(uri, {
connectTimeoutMS: 30000,
serverSelectionTimeoutMS: 30000
});# PyMongo
from pymongo import MongoClient
client = MongoClient(
'mongodb://host:27017/',
connectTimeoutMS=30000,
serverSelectionTimeoutMS=30000
)Ensure your firewall allows outbound connections to MongoDB on port 27017 (or your custom port). Check both local and cloud firewall settings:
# Check if port is open on Linux
sudo iptables -L -n | grep 27017
# Check ufw (Ubuntu)
sudo ufw status
# For MongoDB Atlas, whitelist your IP in Network Access section
# https://docs.atlas.mongodb.com/security/ip-access-list/For MongoDB Atlas specifically, add your application's IP address to the IP Access List in the MongoDB Atlas console.
Configure socketTimeoutMS to prevent long-running queries from blocking connections. Set it to 2-3 times the longest expected operation duration:
// Node.js example
const uri = 'mongodb+srv://user:password@host/database';
const client = new MongoClient(uri, {
connectTimeoutMS: 30000,
socketTimeoutMS: 45000,
serverSelectionTimeoutMS: 30000
});# PyMongo example
client = MongoClient(
'mongodb://host:27017/',
connectTimeoutMS=30000,
socketTimeoutMS=45000
)Test DNS resolution and ping connectivity to your MongoDB host:
# Test DNS resolution
nslookup mongodb-host.example.com
dig mongodb-host.example.com
# Test ping (may be disabled on some servers)
ping mongodb-host.example.com
# Trace network path
traceroute mongodb-host.example.comIf DNS resolution is slow, consider using IP addresses directly or configuring a faster DNS server.
In addition to connection timeouts, configure operation-level timeouts using maxTimeMS. This prevents individual queries from hanging:
// Node.js
const result = await collection.find(query).maxTimeMS(5000).toArray();# PyMongo
result = collection.find(query, maxTimeMS=5000)Set maxTimeMS to an appropriate value for your use case (typically 5000-30000ms depending on query complexity).
Advanced notes
For serverless environments like AWS Lambda, connection pooling is problematic since each invocation may create new connection pools. Consider using MongoDB connection pooling libraries or increasing timeout values even more (60000ms+) for Lambda functions. For MongoDB Atlas clusters, ensure you are using the correct connection string with the proper authentication and are not missing required TLS settings. If using Mongoose with useUnifiedTopology, update to the latest driver version as older versions had known timeout issues. For load-balanced or multi-node replica sets, serverSelectionTimeoutMS should be set higher to allow the driver time to discover all replica set members. Consider monitoring your application logs and MongoDB server logs to identify whether timeouts occur consistently or intermittently, as this helps diagnose the root cause.
Frequently asked questions
- What does "MongoNetworkError: connection timeout" mean?
- MongoNetworkError: connection timeout indicates that your application failed to establish a TCP connection to the MongoDB server within the specified timeframe. The connection timeout determines the maximum amount of response time allowed for the client to connect to the MongoDB instance. This error can occur during initial connection attempts or when the driver tries to reconnect to the database after a connection drop.
- How do I fix "MongoNetworkError: connection timeout"?
- This error occurs when your MongoDB client fails to establish a connection within the configured timeout period. It typically happens due to network issues, firewall blocking, misconfigured timeout settings, or unreachable database servers.
- What causes "MongoNetworkError: connection timeout"?
- connectTimeoutMS setting is too low for your network latency Firewall blocking MongoDB port (default 27017) on server or client network MongoDB server is unreachable or offline
- Is the "MongoNetworkError: connection timeout" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐MongoDB Official Documentation
- ๐MongoDB Docs - Query Timeouts(Official Docs)
- ๐MongoDB Docs - Connection Troubleshooting (Node.js)(Official Docs)
- ๐MongoDB Docs - Client-Side Operation Timeout (PyMongo)(Official Docs)
- ๐MongoDB Node.js Driver - FAQ(Official Docs)