Mongo Couldn’t Connect to Server

There are lots of reasons for Mongo not being able to connect to server – the most obvious one is that the service has not started or a file ownership issue as in owner not being mongodb. First place to check is in the log files /var/log/mongo.log . Once you found the issue and you are still getting the same problem try mongodb --repair .

mongo.lock file

The mongo.lock file is created when the Mongo server dies before mongodb is gracefully closed. When restarting the mongod service and the lock file is present it will stop the boot process of the mongo server. The mongo.lock file is often found in OSX or Linux installs at: /var/lib/mongodb. You can rm it quite easily and will no effect on your Mongo data.

The less obvious reason… Journal option

When Mongo installs it sets aside a wapping 2GB of space just for the Journal! Now, if you are running a small cloud instance of say 3GB just a say for a sandbox project this will most probably gobble up all your space – when that that happens there is no room to allocate the journal. This can often cause the error below. If it does it should appear in the log file. The reason why I am posting this is it can save you a shed load of time because you could easily over look the the Journal error not relevant to the connection error you are getting.

Error: couldn't connect to server shell/mongo.js

Do you have to run Mongo with the Journal option?

No you don’t and for the average dev setup you should be fine without it:

--nojournal when starting Mongo will sort that. But for the latter part of your dev cycle and when in production having Journal-ling turned on is really good practice. Essentially the journal option is a like a scratch card or the middle ground between user input and committing to permanent memory. When it comes into its own is when your Mongo instance crashes – any changes in memory are recorded in the journal and can be replayed so that a minimal amount of data is lost.

A really good article on Journal-ling in Mongo can be found in the this comprehensive post by Kristina Chodorow, How MongoDB’s Journaling Works (accessed 6 Aug, 2017).