MongoDB on Ubuntu won’t start as a service, nothing in the log

OK, this all comes down to permissions, but let’s take it step by step. When you run sudo mongod it does not load a config file at all, it literally starts with the compiled in defaults – port 27017, database path of /data/db etc. – that is why you got the error about not being able to find that folder. The “Ubuntu default” is only used when you point it at the config file (if you start using the service command, this is done for you behind the scenes).

Next you ran it like this:

sudo mongod -f /etc/mongodb.conf

If there weren’t problems before, then there will be now – you have run the process, with your normal config (pointing at your usual dbpath and log) as the root user. That means that there are going to now be a number of files in that normal MongoDB folder with the user:group of root:root.

This will cause errors when you try to start it as a normal service again, because the mongodb user (which the service will attempt to run as) will not have permission to access those root:root files, and most notably, it will probably not be able to write to the log file to give you any information.

Therefore, to run it as a normal service, we need to fix those permissions. First, make sure MongoDB is not currently running as root, then:

cd /var/log/mongodb
sudo chown -R mongodb:mongodb .
cd /var/lib/mongodb
sudo chown -R mongodb:mongodb .

That should fix it up (assuming the user:group is mongodb:mongodb), though it’s probably best to verify with an ls -al or similar to be sure. Once this is done you should be able to get the service to start successfully again.

Leave a Comment