[mongodb] MongoDB: exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating

I created /data/db in root directory and ran ./mongod:

[initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
[initandlisten] shutdown: going to close listening sockets...
[initandlisten] shutdown: going to flush diaglog...
[initandlisten] now exiting
[initandlisten] shutting down with code:100

This question is related to mongodb

The answer is


enter image description hereFirst of all stop all the mongoDB services, then create a directory on / , it means root, if you don't have, and remove the port file also. give all permission to that directory, become that directory owner, run below command:

sudo service mongod stop
sudo rm -rf /tmp/mongod*
sudo mkdir -p /data/db
sudo chmod -R a+wxr /data
sudo chown -R $USER:$USER /data

Now you're done, just start the MongoDB service, if didn't help, try to change the port like:

sudo service mongod restart && mongod # if didn't help run below cmd
mongod --port 27018

Note: For me all this stuff works and hoping would work for you, guy!


Check if SElinux is enabled. If it is in enforcing mode just try with permissive mode. In case that helps you should create SElinux policies for mongodb.

You can try with audit2allow - check https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security-enhanced_linux/sect-security-enhanced_linux-fixing_problems-allowing_access_audit2allow


I experienced the same problem and following solution solved this problem. You should try the following solution.

sudo mkdir -p /data/db
sudo chown -R 'username' /data/db


you just need to run the command as a super user:

just type from the terminal: sudo su mongod


If you do not need to have the database directories in root, you can create data/db in your home directory (mkdir -p ~/data/db).

Then you can start the MongoDB server with the --dbpath option:

mongod --dbpath=$(echo ~)/data/db

(This is assuming you're starting it from a terminal. For some strange reason, mongod does not accept ~ as the home directory path, hence the bash echo substitution trickery, you may as well write /home/yourusername/data/db)

In fact, it does not have to be ~/data/db, it can be anywhere you want and this way you can have multiple database paths and organize everything nicely. May not be the best option for production (depending on what you want to do), but for developing works fine.


If your system is using SELinux, make sure that you use the right context for the directory you created:

ls -dZ /data/db/
ls -dZ /var/lib/mongo/

and clone the context with:

chcon -R --reference=/var/lib/mongo /data/db

If you are checking your mongodb logs and you are getting such an error then just follow steps as i did. It occurs due to permission issues with your /data/db directory. I am using ubuntu16.04 and I find solution with running two simple commands as follow.

Step 1: Give permission to /data/db directory:

sudo chmod -R 0777 /data/db

Step 2: Try to start mongod service as :

sudo service mongod start

Step 3: Check status of mongod service:

sudo service mongod status

Fix the permissions of /data/db (or /var/lib/mongodb):

sudo chown -R mongodb: /data/db

then restart MongoDB e.g. using

sudo systemctl restart mongod

In case that does not help, check your error message if you are using a data directory different to /var/lib/mongodb. In that case run

sudo chown -R mongodb: <insert your data directory here>

source


Nice solutions, but I wonder why nobody is giving the solution for windows.

If you are using windows you just have to "Run as Administrator" the cmd.


I dealt with the same problem:

Change to the MongoDB directory which contains the bin directory, and run:

sudo bin/mongod 

Running MongoDB as the root user you might be asked to enter the root password. If you still can not run the MongoDB, then check the permissions for MongoDB's data directory. Enter:

ls -ld /data

or type ls -l / for the permissions for all directories and files in the directory including the /data directory.

The permission mode for the root user should be "rwx", meaning the root user is able to read, write and execute the files. To make this happen, we use:

chmod 755 /data 

755 is a octal notation to set the permissions, of the /data directory to "rwxr-xr-x", which means the root user can read, write and execute, whereas the "group" and "everyone"are able to only read and execute.

Note: When you can't do this, type instead:

sudo chmod 755 /data   

to set the permission as the root user.

Then, when done with that step, recapture the permission modes using:

ls -ld /data

which should look like:

drwxr-xr-x  3 root  wheel  102 Mar  3 17:00 /data

You don't need to worry about the "d" at the beginning, and notice that the permissions reflect "rwxr-xr-x".

You can now change back to the MongoDB directory and type:

sudo bin/mongod  

to run MongoDB.


The problem is that the directory you created, /data/db is owned by and only writable by the root user, but you are running mongod as yourself. There are several ways to resolve this, but ultimately, you must give the directory in question the right permissions. If this is for production, I would advise you to check the docs and think this over carefully -- you probably want to take special care.

However, if this is just for testing and you just need this to work and get on with it, you could try this, which will make the directory writable by everyone:

> sudo chmod -R go+w /data/db

or this, which will make the directory owned by you:

> sudo chown -R $USER /data/db

On a Mac, I had to do the following:

sudo chown -R $USER /data/db
sudo chown -R $USER /tmp/

because there was also a file inside /tmp which Mongo also needed access


If you are On Windows, and you are trying to setup MongoDB, run cmd as Admnistrator is the way forward as Enrique suggested above see it here


This worked for me in Ubuntu LTS 20.04:

$ sudo service mongod start