MongoDB was running fine, but now it won't connect. Can you please fix it?
This is a common query we commonly receive from our MongoDB customers as part of our Server Management Services.
One of the most common causes for MongoDB connection refused in the Ubuntu server is conflicting server firewall rules.
Today, let’s get into the details and see how our Support Engineers fix MongoDB errors.
How we fix common MongoDB connection errors
At Bobcares, where we have more than a decade of expertise in managing servers, we see many customers facing problems while managing MongoDB.
In this particular case, the customer was trying to login via the MongoDB connection interface. All his attempts failed.
Now let’s see the major reasons for MongoDB errors and how our Support Engineers fix the top errors.
1. Firewall rule conflict
Recently, one of our customers had a problem with MongoDB. He had a Linode server running Ubuntu 16.04 LTS and MongoDB instance that he couldn’t connect to from an outside source. However, it could accept local connections.
Also, when he tried to run the command
mongo databasedomain/databasename -u username -p password,he got an error looked like,
2019-07-22T23:54:03.093+0000 warning: Failed to connect to databaseserverip:27017, reason: errno:111 Connection refused 2019-07-22T23:54:03.094+0000 Error: couldn't connect to server < ip address >:27017 (databaseserverip), connection attempt failed at src/mongo/shell/mongo.js:148 exception: connect failed
On checking, our Support Engineers found that the error was due to a conflict of rules in the server firewall. So we opened MongoDB port correctly in UFW using
sudo ufw allow from client_ip_address to any port 27017
Further, we verify the connection using telnet too.
A successful telnet session will look like:
telnet 54.xx.xx.115 27017 Trying 54.xx.xx.115... Connected to 54.xx.xx.115. Escape character is '^]'.
2. Misconfiguration in mongod.conf file
Another reason for MongoDB to refuse connection can be the misconfiguration in /etc/mongod.conf.
Most of the customers may set bind_ip incorrectly and it will not allow the connection externally. So, our Support Engineers set the bind_ip = 0.0.0.0 in order to make successful external connections.
[Need assistance to fix Mongodb connection refused in ubuntu? We’ll help you.]
In short, the top most causes of this error Mongodb connection refused in Ubuntu are conflicting rules in the server firewall for the MongoDB port or bad settings in mongod.conf file. Today, we saw the various reasons for MongoDB errors and how our Support Engineers fix them.