MongoDB: exception in initAndListen: 20 Attempt to create a lock file in a read-only directory: / data / db, completion

I created /data/db in the 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 
+42
mongodb
Feb 24 '17 at 19:36
source share
7 answers

The problem is that the directory /data/db you created is owned and accessible only by the root user, but you use mongod as your own. There are several ways to solve this problem, but ultimately you must provide the directory referenced by the correct permissions. If this is for production, I would advise you to check the documents and think it over carefully - you will probably want to be especially careful.

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

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

or this is what the directory belonging to you will do:

 > sudo chown -R $USER /data/db 
+82
Feb 24 '17 at 20:01
source share

You can use mongo as root, works for me:

 $ sudo mongod 
+22
Mar 08 '17 at 17:50
source share

I had the same problem, and after solving this problem, this problem was solved. You should try the following solution.

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

+9
Apr 11 '17 at 13:49 on
source share

If your system uses SELinux, make sure you use the correct 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 
+3
Mar 07 '17 at 12:54 on
source share

On a Mac, I needed to do the following:

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

since inside /tmp was also a file for which Mongo also needed access

+2
Dec 06 '17 at 13:11
source share

I ran into the same problem, and I solved it, and here are the step-by-step instructions:

Before trying everything else, just go to the mongodb directory, which contains the bin directory for mongodb, and just use the command in the terminal:

sudo bin / mongod

running mongodb as root, and you may be asked to enter a password as root. If you still cannot run mongodb, then do the following:

First, let's see the mongodb data directory resolution mode by typing in Terminal:

ls -ld / data strong>

(ps or we can simply type "ls -l /" to see permission modes for all directories and files in the root directory, including the / data directory.)

And the permission mode for the root user must be "rwx", namely, the root user can read, write and execute files. To make this happen, we use the command to change the resolution mode by typing in the terminal:

chmod 755 / data strong>

(that is, 755 is a separate octal entry of the permission setting argument) This sets the permission modes / data directory to "rwxr-xr-x", namely, the root user can read, write and execute, while the group user and everyone else can read and execute.

Note: if you are denied this operation, enter instead:

sudo chmod 755 / data strong>

to set permission as root user.

Then, when this step is completed, return the permission modes by typing again: ls -ld / data strong>

and the output should look like this:

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

(ps you donโ€™t need to worry about โ€œdโ€ at the beginning) and note that the setup of โ€œrwxr-xr-xโ€ is now done.

Then we are all set up, and now you can return to the mongodb directory and enter:

sudo bin / mongod

to run mongodb.

Hope this helps.

+1
Mar 11 '17 at 0:22
source share

Good solutions, but I wonder why no one gives a solution for Windows.

If you use windows, you just need "Run as administrator" cmd.

+1
Dec 16 '17 at 21:25
source share



All Articles