89
MongoDBERRORCriticalNetworkHIGH confidence

A network operation timed out

What this means

This error indicates that a network operation between the client and the server did not complete within the configured socket timeout period. It is a client-side error indicating a potential network or server performance issue.

Why it happens
  1. 1A firewall is blocking traffic on the MongoDB port (default 27017)
  2. 2The MongoDB server is down or not reachable from the client
  3. 3High network latency between the application and the database server
  4. 4The server is under heavy load (e.g., CPU or I/O) and cannot respond to the request in time
How to reproduce

A client attempts to connect to a MongoDB server at an IP address where no server is running, and the connection times out.

trigger — this will error
trigger — this will error
// Node.js driver example
// This will fail if no MongoDB server is running on localhost:27018.
const client = new MongoClient("mongodb://localhost:27018", {
  serverSelectionTimeoutMS: 5000 // Wait 5 seconds before timing out
});
await client.connect();

expected output

MongoServerSelectionError: connection timed out

Fix 1

Verify Network Connectivity

WHEN Connections are failing with timeouts.

Verify Network Connectivity
// From the client machine, try to connect using a tool like telnet or nc.
telnet <mongodb_host> 27017
// Or use mongosh
mongosh --host <mongodb_host> --port 27017

Why this works

Use basic network diagnostic tools to confirm that the client machine can reach the MongoDB server over the network. Check for firewalls or security groups that might be blocking the connection.

Fix 2

Check Server Status and Load

WHEN Network connectivity is confirmed.

Why this works

Ensure the `mongod` process is running on the server. Check the server's CPU, memory, and disk I/O to see if it is overloaded, as this can prevent it from responding in time.

Fix 3

Adjust Connection Timeout Settings

WHEN Operating over a high-latency network (e.g., cross-region).

Adjust Connection Timeout Settings
// Increase the connection timeout in the driver options.
const client = new MongoClient(uri, {
  connectTimeoutMS: 10000 // 10 seconds
});

Why this works

If slow network is a known factor, you can increase the connection timeout settings in your driver. This should be a last resort after ruling out other issues.

What not to do

Set excessively long timeouts

This can cause your application to become unresponsive while it waits for a connection that will never succeed. It is better to fail fast and report the error.

Sources
Official documentation ↗

mongodb/mongo src/mongo/base/error_codes.yml

Connection Troubleshooting

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MongoDB errors