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
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
You can use mongo as root, works for me:
$ sudo mongod
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
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
I have faced with exactly the same problem and I solved it and here are the explanations step by step:
Before trying everything else, just change to the mongodb directory which contains the bin directory for mongodb, and simply use command in the Terminal:
sudo bin/mongod
running mongodb as the root user and you may be asked to enter the password as the root user. If you still can not run the mongodb, then let's do the following:
Firstly, let's see the permission mode of the data directory of mongodb by typing in the Terminal:
ls -ld /data
(p.s. or we can just type "ls -l /" to see the permission modes of all the directories and files in the root directory including the /data directory.)
And the permission mode for the root user should be "rwx", namely the root user being able to read, write and execute the files.
To make this happen, we use the command to change the permission mode by typing in Terminal:
chmod 755 /data
(i.e. 755 is a specific octal notation of permission setting argument)
This sets the permission modes of /data directory to "rwxr-xr-x", namely, the root user being able to read, write and execute whereas the Group user and Everyone else being able to only read and execute.
Note: when you are denied to carry out this operation, type instead:
sudo chmod 755 /data
to set the permission as the root user.
Then when this step is done, let's recapture the permission modes by typing
again:
ls -ld /data
and the output should look like this:
drwxr-xr-x 3 root wheel 102 Mar 3 17:00 /data
(p.s.you don't need to worry about the "d" at the beginning) and notice the "rwxr-xr-x" setting is now done.
Then we are all set and you can now change back to the mongodb directory and type:
sudo bin/mongod
to run the mongodb.
Hope this helps.
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.
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
you just need to run the command as a super user:
just type from the terminal: sudo su mongod