mongo shell – JavaScript execution failed: Error: couldn’t connect to server – Quick Fix

I have just installed MongoDB v2.4.5 on my Ubuntu 13.04 64 bit system and when I tried to connect to the server from command line using the mongo shell provided with the installed mongodb-10gen package, I got the following error:

I tried to start, stop and restart the mongodb service using these commands but the error remained there:

Finally, I looked at the log file ( /var/log/mongodb ) and found the following error message listed there:

So, it turned out that the amount of free disk space in my ubuntu installation partition was very low. MongoDB has journaling turned on by default on 64 bit systems whose purpose is to ensure “write durability and data consistency”. When I skimmed through the above linked page, following two solutions became apparent to me:

  1. Keep journaling turned on but instead use the smallfiles option in the database server’s start up configuration ( /etc/mongodb.conf ). It will cause the server to not only reduce the size of Journal files that it uses but also the files of the actual databases.
  2. Turn off journaling altogether without enabling smallfiles . It will allow the MongoDB server to use fully large files for the storage of actual data in the databases but it will not consume any disk space for journaling.

I started with the first one. So, I added the following line to the /etc/mongodb.conf file:

smallfiles = true

Then I issued the command to start the mongodb service again and tried to connect to the server from mongo shell but once again, I got the same error. Probably free disk space on my hard drive was too low even for the smaller journal files. 😛

Then, I tried the second alternative solution. As I have installed MongoDB on my PC just for experimentation and learning purposes, occasional data loss/corruption is not a big issue for me. So, I removed the line smallfiles = true from the configuration file and instead added the following line to it:

nojournal = true

This time it worked. After restarting the MongoDB service, when I launched the ‘mongo’ shell from the command line, I got the following normal output message:

Of course, I will soon add the disk space that I got by removing windows 7 from my dual boot system to my ubuntu partition. Then I will have plenty of free disk space and at that time I will re-enable journaling by removing this line from the configuration file.

Warning: After disabling the journaling feature, If your database shuts down abnormally due to any reason (e.g. a power failure), it will fail to start again in the normal way. This is because it is likely to be in an inconsistent state and has been locked to remind you to perform a repair before working with it any further. Based on the instructions given on this page , I have found the following steps most easy to perform to do the necessary repair operation.

  1. Run this command in the terminal to delete the lock file:

    Where /var/lib/mongodb/ is the path of your MongoDB data directory a.k.a. dbpath . In most distributions, its default value is /data/db/ so please confirm it for yourself before proceeding.
  2. Add following line to your MongoDB configuration file:
  3. Start the MongoDB service:

    MongoDB server will start, perform any necessary repair steps and will shutdown. Now:
  4. Remove the following line from your MongoDB configuration file:
  5. Start the MongoDB service again:

Note: There is an alternate way of starting the MongoDB server i.e. by invoking the mongod from the command line. But in this case, in addition to any other options that you want to specify during the start up, you must also specify the path to your MongoDB data directory with the --dbpath option if it is something other than /data/db/ .

So, for example, you can launch the repair using this method by issuing the following command in the terminal:

And you can start with journaling disabled like this:

I hope that you will find this short tutorial helpful in correcting some of the many start up errors that could occur with your own MongoDB installation on Ubuntu. If I am missing some important detail at some point, have made some serious mistake or if you need some further explanation of any of the steps then please don’t hesitate to comment. I shall be more than happy to get your feedback. 🙂

Thanks for bearing with me so long 🙂

Leave a Reply