×


MongoDB connection timeout

Unlike MySQL, MongoDB easily handles to organize large unstructured data in applications like eBay, Metlife, Facebook, Shutterfly, and so on.
But while connecting, application users may encounter an error like MongoDB connection timeout.
This error happens due to the wrong MongoDB server setting, bad connection parameters and much more.
That is why, at Ibmi Media, we often get requests from our customers to resolve "MongoDB connection timeout" error as part of our Server Management Services.
Today, we will see how we fixed the connection timeout error & its related issues.

The nature of MongoDB connection timeout?

The connection timeout determines the maximum amount of response time that waits for a connection established by the server.
This value is used when making an initial connection to the MongoDB database.
The default connection timeout value ranges from 1 second to 30 seconds.
For example, if you have the connection timeout set to 30 seconds, your applications will never wait more than 30 seconds for the result.

Top common causes & fixes to MongoDB connection timeout?

You can apply the following tips to fix this error.
1. Bad settings of MongoDB client
Recently, one of our customers approached us with a problem regarding MongoDB connection. When he tried to connect MongoDB using MongoDB client, it resulted in a server error message box that showed connection timeout error.
This often happens due to settings in the MongoDB client. When the client takes too much time to connect to MongoDB server than the preset timeout value, it can result in error.
And the fix involves in raising the timeout limits on the MongoDB client. For this, we first check with the customer on the settings that they use on their client. We then compare the values set in the MongoDB server. Depending on the values, we suggest tweaking the variables.
For example, in the case of this customer, it was the timeout that was causing the problem. Therefore, we set the max connection idle time to a higher value and this solved the problem. Now, the user could connect to MongoDB using MongoDB client.

2. Wrong connection parameters
Sometimes this error happens when there are incorrect settings in the connection parameter. In such cases, we modify the settings by navigating to the database in MongoDB.
For example, we tweak the settings in the database field like this.

MONGO_URI=mongodb://user:password@127.0.0.1:27017/dbname?keepAlive=true&poolSize=30&autoReconnect=true&socketTimeoutMS=360000&connectTimeoutMS=360000


3. Firewall blocks IP
In a similar case, a DigitalOcean customer contacted us that after creating the MongoDB he could not connect to the database using Node.js. It resulted in an error like;

message: 'failed to connect to server [droplet_ip:27017] on first connect [MongoError: connection 0 to droplet_ip:27017 timed out]'

This error is most common with MongoDB & happens when the connection gets dropped by the firewall.
So, to fix the issue, simply open the firewall settings on the server side and allowed the corresponding IP to allow connections.

[Having issues with connection timeout in MongoDB? We are available to fix it for you.]


Conclusion

This article will help you to fix common MongoDB errors such as MongoDB connection timeout which generally occurs due to the wrong settings in the MongoDB server, bad connection parameters and so on.