[mongodb] Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied

How to I get mongo to use a mounted drive on ec2? I really do not understand. I attached a volume on ec2 formatted the drive as root and start as root and yet as root I cant access? I am running on ubuntu 12.04. No other mongo is running

I see that mongo made a 'db' dir in /data i.e. /data/db

cd /
ls -al
drwxr-xr-x  4 root root  4096 Mar  5 16:28 data

cd /data
ls -al
total 28
drwxr-xr-x  4 root root  4096 Mar  5 16:28 .
drwxr-xr-x 24 root root  4096 Mar  5 16:28 ..
drwxr-xr-x  2 root root  4096 Mar  5 16:28 db
drwx------  2 root root 16384 Mar  5 16:20 lost+found


sudo mkfs.ext3 /dev/xvdh
sudo mkdir /data
sudo su - -c 'echo "/dev/xvdh %s auto noatime 0 0" | sudo tee -a /etc/fstab'
sudo mount /data

sudo service mongodb start
mongodb start/running, process 17169

sudo ps -ef | grep mongod
ubuntu   15763 15634  0 16:32 pts/2    00:00:00 tail -f mongodb.log
ubuntu   18049 15766  0 16:43 pts/3    00:00:00 grep --color=auto mongod


Tue Mar  5 16:33:15 [initandlisten] MongoDB starting : pid=15890 port=27017 dbpath=/data 64-bit host=aws-mongo-server-east-staging-20130305161917
Tue Mar  5 16:33:15 [initandlisten] db version v2.2.3, pdfile version 4.5
Tue Mar  5 16:33:15 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Tue Mar  5 16:33:15 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Tue Mar  5 16:33:15 [initandlisten] options: { bind_ip: "10.157.60.27", config: "/etc/mongodb.conf", dbpath: "/data", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", replSet: "heythat" }
Tue Mar  5 16:33:15 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Mar  5 16:33:15 dbexit: 
Tue Mar  5 16:33:15 [initandlisten] shutdown: going to close listening sockets...
Tue Mar  5 16:33:15 [initandlisten] shutdown: going to flush diaglog...
Tue Mar  5 16:33:15 [initandlisten] shutdown: going to close sockets...
Tue Mar  5 16:33:15 [initandlisten] shutdown: waiting for fs preallocator...
Tue Mar  5 16:33:15 [initandlisten] shutdown: lock for final commit...
Tue Mar  5 16:33:15 [initandlisten] shutdown: final commit...
Tue Mar  5 16:33:15 [initandlisten] shutdown: closing all files...
Tue Mar  5 16:33:15 [initandlisten] closeAllFiles() finished
Tue Mar  5 16:33:15 [initandlisten] shutdown: removing fs lock...
Tue Mar  5 16:33:15 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Mar  5 16:33:15 dbexit: really exiting now

Below is if I restart when I remove a lock file....

Tue Mar  5 16:59:15 [initandlisten] MongoDB starting : pid=21091 port=27017 dbpath=/data 64-bit host=aws-mongo-server-east-staging-20130305161917
Tue Mar  5 16:59:15 [initandlisten] db version v2.2.3, pdfile version 4.5
Tue Mar  5 16:59:15 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Tue Mar  5 16:59:15 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Tue Mar  5 16:59:15 [initandlisten] options: { bind_ip: "10.157.60.27", config: "/etc/mongodb.conf", dbpath: "/data", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", replSet: "heythat" }
Tue Mar  5 16:59:15 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Mar  5 16:59:15 dbexit: 
Tue Mar  5 16:59:15 [initandlisten] shutdown: going to close listening sockets...
Tue Mar  5 16:59:15 [initandlisten] shutdown: going to flush diaglog...
Tue Mar  5 16:59:15 [initandlisten] shutdown: going to close sockets...
Tue Mar  5 16:59:15 [initandlisten] shutdown: waiting for fs preallocator...
Tue Mar  5 16:59:15 [initandlisten] shutdown: lock for final commit...
Tue Mar  5 16:59:15 [initandlisten] shutdown: final commit...
Tue Mar  5 16:59:15 [initandlisten] shutdown: closing all files...
Tue Mar  5 16:59:15 [initandlisten] closeAllFiles() finished
Tue Mar  5 16:59:15 [initandlisten] shutdown: removing fs lock...
Tue Mar  5 16:59:15 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Mar  5 16:59:15 dbexit: really exiting now

This question is related to mongodb

The answer is


Removing the mongodb.lock file was not the issue in my case. I did so and got an error about the port being in use: [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017. I found another solution here: unable to start mongodb local server with instructions to kill the process:

  1. Find out from netstat which process is running mongodb port (27017)

    sudo netstat -tulpn | grep :27017

    Output will be: tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1412/mongod

  2. Kill the appropriate process.

    sudo kill 1412 (replace 1412 with your process ID found in step 1)

And I was able to successfully start mongodb again. I believe mine was still running from an improper shut down.


I had the same problem.

I solved it by changing selinux status to permissive with below command:

setenforce 0

I had a similar issue and followed all the instructions above regarding changing owners using sudo chown etc. I still had an instance of mongodb running in the background after the changes. Running

ps auxw | grep mongo 

showed me other tasks using mongo running in background that weren't closed properly. I then ran kill on all the ones running and then could start my server.


You could try by these ways. 1st.

sudo chown -R mongod:mongod /data/db

but at some times,this is not useful. 2nd. if the above way is not useful,you can try to do this:

mkdir /data/db #as the database storage path

nohup mongod --dbpath /data/db &

or type:

mongod --dbpath /data/db

to get the output stream


I use this method to solve the problem:

sudo chown -R mongodb:mongodb /data/db

My mongo (3.2.9) was installed on Ubuntu, and my log file had the following lines:

2016-09-28T11:32:07.821+0100 E STORAGE  [initandlisten] WiredTiger (13) [1475058727:821829][6785:0x7fa9684ecc80], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.turtle: handle-open: open: Permission denied 
2016-09-28T11:32:07.822+0100 I -        [initandlisten] Assertion: 28595:13: Permission denied 
2016-09-28T11:32:07.822+0100 I STORAGE  [initandlisten] exception in initAndListen: 28595 13: Permission denied, terminating

2016-09-28T11:32:07.822+0100 I CONTROL [initandlisten] dbexit: rc: 100

So the problem was in permissions on /var/lib/mongodb folder.

sudo chown -R mongodb:mongodb /var/lib/mongodb/
sudo chmod -R 755 /var/lib/mongodb
  • Restart the server

Fixed it, although I do realise that may be not too secure (it's my own dev box I'm in my case), bit following the change both db and authentication worked.


After I killed mongod, I just had the same problem: couldn't start mongod.

$> sudo kill `pidof mongod`

2015-08-03T05:58:41.339+0000 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongodbtest/replset/data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

After I delete the lock directly, I can restart the mongod process.

$>  rm -rf /data/mongodbtest/replset/data/mongod.lock

I had similar issue, the actual reason was that there was mongod session running already from my previous attempt.

I ran

killall mongod

and everything else ran just as expected.

killall command would send a TERM signal to all processes with a real UID. So this kills all the running instances of mongod so that you could start your own.


Every time you when you try to start mongod just type

sudo mongod

or if permanently want to fix this just try to give rwx premission to /data/db folder

 chmod +rwx data/

I installed mongodb with EBS on an EC2 with Ubuntu 14.04 following this tutorial:

http://docs.mongodb.org/ecosystem/platforms/amazon-ec2/

But instead of the suggested chown I did:

sudo chown -R mongodb:mongodb /data /log /journal

To fix the problem


Got a similar error, fixed with removing all records (in my case directory journals, and file mongo.lock...), after that check port with sudo lsof -i:27017, if smth running on it kill <PID of the process>, and try to run ./mongod again


Do ls -lato know the user and group of /var/log/mongodb. Then do sudo chown -R user:group /data/db Now run sudo service mongodb start. Check the status with sudo service mongodb status


You just have to give access to your /data/db folder.

Type sudo chown -R <USERNAME> /data/db, replace <USERNAME> by your username.

You can find your username by typing whoami.


I got the same issue when I ran mongod command after installing it on Windows10. I stopped the mongodb service and started it again. Working like a charm

Command to stop mongodb service (in windows): net stop mongodb

Command to start mongodb server: mongod --dbpath PATH_TO_DATA_FOLDER


For those of you experiencing this error on Windows using Task Manager end the instance of "mongod.exe" that is running. Once that is done permanently delete the mongo.lock file and run mongod.exe. It should work perfectly after that.


In my case (AWS EC2 instance, Ubuntu) helped:

$ sudo mkdir -p /data/db/
$ sudo chown `USERNAME` /data/db

And after that everything worked fine.


On windows be sure the console is started as aministrator


In my case the issue was solved by removing the log file.

sudo rm /log/mongod.log

Although the error message refers specifically to the lock file:

exception in initAndListen: 10309 Unable to create/open lock file: 
/data/mongod.lock errno:13 Permission denied 
Is a mongod instance already running?, terminating

For mac users:
Run ls -ld /data/db/
Output should be something like drwrx-xr-x 20 singh wheel 680 21 Jul 05:49 /data/db/
Where singh is the owner and wheel is the group it belongs to.
Run sudo chown -R singh:wheel /data/db
Run mongod


If you literally want a one line equivalent to the commands in your original question, you could alias:

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

https://stackoverflow.com/a/11777141/7160782


On a Fedora 18 with Mongo 2.2.4 instance I was able to get around a similar error by disabling SELinux by calling setenforce 0 as root.

BTW, this was a corporate environment, not an Amazon EC2 instance, but the symptoms were similar.


In Centos Server

this works for me

chown -R mongod:mongod /var/lib/mongo

In Mycase
In mongodb version 2.6.11 default databse directory is /var/lib/mongodb/

  1. $ sudo chown -R id -u /var/lib/mongodb/

  2. $ sudo chown -R id -u /var/lib/mongodb/mongod.lock

  3. $ sudo /etc/init.d/mongod stop

  4. $ sudo /etc/init.d/mongod start


This is what I did to fix the problem:

$sudo mkdir -p /data/db

$export PATH=/usr/local/Cellar/mongodb/3.0.7/bin:$PATH

$sudo chown -R id -u /data/db

and then to start mongo...

$mongod


For me on CentOS 6.x:

sudo chown -R mongodb:mongodb <db-path> sudo service mongod restart

And I have set a custom db-path in /etc/mongod.conf.


Fix: sudo mongod

I had the same problem, running mongod with sudo privileges fixed it. Coming from a windows environment, I used just mongod to start the daemon, well it looks like we need the superuser privileges to access /data/db.

You can also give non root users Read and Write permissions to that path. check answers above for a guide!


As of today, I tried to get my way through the to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating, and tried all the answer posted above to solve this problem, hence nothing worked out by adding

sudo chown -R mongodb:mongodb /data/db

Unless I added my current user permission to the location path by

sudo chown $USER /data/db

Hope this helps someone. Also I just installed Mongo DB on my pi. Cheers!