I am working on a project where I connect to MongoDB from PHP using the PECL extension. Regardless of your stack, we might time to time face strange issues when our clients can’t connect to the mongodb server. In this post, I would document what I did in my case.
First test if the mongodb server can be connected to. Use the “mongo” shell client.
1 |
mongo --host localhost:27017 |
If the connection succeeds, then the server is definitely reachable. You have made some mistakes in configuring your client. A very common mistake is having a malformed connection URI. So double check if you typed in your host, port and other details properly in the connection string.
If the above fails, that means the server is not responding. To begin with, try restarting it.
1 |
sudo service mongodb restart |
If the server started well, try again. Still connection failed? OK, the server might be damaged. How about checking the log files?
1 |
tail -f /var/log/mongodb/mongodb.log |
Nothing interesting? Or do not understand the format? Well, then let’s try blindly repairing the server:
1 2 3 |
sudo rm /var/lib/mongodb/mongod.lock sudo -u mongodb mongod -f /etc/mongodb.conf --repair sudo service mongodb start |
Your problem should be resolved by now. But if it doesn’t solve the issue, your problem is not a common one. Try Googling further or asking on StackOverflow or other programming forums.