MongoDB Won’t Start on Ubuntu – Troubleshooting Guide

mongodbUbuntu

I installed MongoDb on Ubuntu and was able to run it using sudo service mongod start. Then I had to restart it since I changes the config file. Now I can't start it again. When I run sudo service mongod start, it just says nothing (but I can't connect using mongo). And the log says,

2018-06-06T18:45:49.279+0000 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /var/lib/mongodb, terminating

Here are the permissions:

ubuntu@ip-172-31-24-92:~$ stat /var/lib/mongodb
Access: (0755/drwxr-xr-x) Uid: ( 1000/ ubuntu) Gid: ( 116/ mongodb)

So, how do I run it again?

Bonus question: how do I make the command output the actual error, so that I don't have to look it up in the log every time?

P.S. I'm a long time Windows user, just learning Linux.

Best Answer

First of all, welcome to Linux!

Second, I'm guessing in this case mongodb runs as the user mongodb

If this is the case, then the user mongodb needs permission to write to the directory.

Given your current permissions: the owner is ubuntu which can rwx read, write, and execute, while the group is mongodb which can only r-x read, execute. Since the mongodb user is in the mongodb group but does not own the file, it cannot write.

Access: (0755/drwxr-xr-x)  Uid: ( 1000/  ubuntu)   Gid: (  116/ mongodb)

To solve this either make mongodb the owner sudo chown mongodb:mongodb -R /var/lib/mongodb or let the group write as well sudo chmod 775 -R /var/lib/mongodb

Assuming you installed the mongodb server using a standard method on standard linux system, you will likely need to restart the service using the systemctl (SystemD) or service (upstart) command.

SystemD: sudo systemctl restart mongodb or sudo systemctl restart mongod

Upstart: sudo service mongod restart

If you are running SystemD, then you can check the status of your service with

sudo systemctl status mongodb or sudo systemctl status mongod

And you can view the logs of your service with

sudo journalctl -u mongodb or sudo journalctl -u mongod

Related Topic