How to fix MongoNetworkError: connection timeout in MongoDB

MongoDBINTERMEDIATEHIGHUpdated Dec 29, 2025ยท 303 views

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
  1. Verify MongoDB server is running and accessible
  2. Increase connectTimeoutMS setting
  3. Check and configure firewall rules
  4. Set appropriate socketTimeoutMS
mongodb โ€” error
# Test connectivity to MongoDB (port 27017 by default)
telnet mongodb-host.example.com 27017
# Or using nc
nc -zv mongodb-host.example.com 27017
MongoNetworkError: 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

  1. connectTimeoutMS setting is too low for your network latency
  2. Firewall blocking MongoDB port (default 27017) on server or client network
  3. MongoDB server is unreachable or offline
  4. IP address not whitelisted in MongoDB Atlas network access list
  5. Network latency exceeds configured timeout value
  6. DNS resolution delays or DNS server issues
  7. MongoDB replica set nodes are slow to respond to connection attempts
  8. Connection pool exhaustion or resource limits

How to fix "MongoNetworkError: connection timeout"

1Verify MongoDB server is running and accessible

First, confirm that your MongoDB instance is running and listening on the expected port. Test connectivity using telnet or nc:

bash
# Test connectivity to MongoDB (port 27017 by default)
telnet mongodb-host.example.com 27017

# Or using nc
nc -zv mongodb-host.example.com 27017

If the connection fails, ensure the MongoDB service is running and the hostname/port are correct.

2Increase connectTimeoutMS setting

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:

javascript
// 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
});
python
# PyMongo
from pymongo import MongoClient
client = MongoClient(
    'mongodb://host:27017/',
    connectTimeoutMS=30000,
    serverSelectionTimeoutMS=30000
)
3Check and configure firewall rules

Ensure your firewall allows outbound connections to MongoDB on port 27017 (or your custom port). Check both local and cloud firewall settings:

bash
# 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.

4Set appropriate socketTimeoutMS

Configure socketTimeoutMS to prevent long-running queries from blocking connections. Set it to 2-3 times the longest expected operation duration:

javascript
// Node.js example
const uri = 'mongodb+srv://user:password@host/database';
const client = new MongoClient(uri, {
  connectTimeoutMS: 30000,
  socketTimeoutMS: 45000,
  serverSelectionTimeoutMS: 30000
});
python
# PyMongo example
client = MongoClient(
    'mongodb://host:27017/',
    connectTimeoutMS=30000,
    socketTimeoutMS=45000
)
5Verify network connectivity and DNS resolution

Test DNS resolution and ping connectivity to your MongoDB host:

bash
# 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.com

If DNS resolution is slow, consider using IP addresses directly or configuring a faster DNS server.

6Configure maxTimeMS for query operations

In addition to connection timeouts, configure operation-level timeouts using maxTimeMS. This prevents individual queries from hanging:

javascript
// Node.js
const result = await collection.find(query).maxTimeMS(5000).toArray();
python
# 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