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

Mongodb

Mongodb Problem Overview


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

Mongodb Solutions


Solution 1 - Mongodb

I use this method to solve the problem:

sudo chown -R mongodb:mongodb /data/db

Solution 2 - Mongodb

I was having the same problem on a Ubuntu ec2 instance. I was following this amazon article on page 7:

http://d36cz9buwru1tt.cloudfront.net/AWS_NoSQL_MongoDB.pdf

Mongodb path in /etc/mongodb.conf was set to /var/lib/mongodb (primary install location and working). When I changed to /data/db (EBS volume) I was getting 'errno:13 Permission denied'.

  1. First I ran sudo service mongodb stop.
  2. Then I used ls -la to see what group & owner mongodb assigned to /var/lib/mongodb (existing path) and I changed the /data/db (new path) with chown and chgrp to match. (example: sudo chown -R mongodb:mongodb /data/db)
  3. Then I updated the path in etc/mongodb.conf to /data/db and deleted the old mongo files in /var/lib/mongodb directory.
  4. Then I ran sudo service mongodb start and waited about a minute. If you try to connect to 27017 immediately you won't be able to.
  5. After a minute check /data/db (EBS volume) and mongo should have placed a journal, mongod.lock, local.ns, local.0, etc. If not try sudo service mongodb restart and check a minute later.

I just spent over a hour with this. Changing the group and deleting the old files is probably not necessary, but that's what worked for me.

This is a great video about mounting a ebs volume to ec2 instance:

http://www.youtube.com/watch?v=gBII3o3BofU

Solution 3 - Mongodb

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

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

And after that everything worked fine.

Solution 4 - Mongodb

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.

Solution 5 - Mongodb

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

Solution 6 - Mongodb

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.

Solution 7 - Mongodb

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

Solution 8 - Mongodb

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!

Solution 9 - Mongodb

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.

Solution 10 - Mongodb

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: https://stackoverflow.com/questions/6478113/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

  1. 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.

Solution 11 - Mongodb

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.

Solution 12 - Mongodb

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.

Solution 13 - Mongodb

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

Solution 14 - Mongodb

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

Solution 15 - Mongodb

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.

Solution 16 - Mongodb

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

Solution 17 - Mongodb

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

Solution 18 - Mongodb

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

Solution 19 - Mongodb

I had the same problem.

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

setenforce 0

Solution 20 - Mongodb

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

Solution 21 - Mongodb

On windows be sure the console is started as aministrator

Solution 22 - Mongodb

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

Solution 23 - Mongodb

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.

Solution 24 - Mongodb

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

Solution 25 - Mongodb

In Centos Server

this works for me

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

Solution 26 - Mongodb

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

Solution 27 - Mongodb

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!

Solution 28 - Mongodb

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTampaView Question on Stackoverflow
Solution 1 - MongodbHaimeiView Answer on Stackoverflow
Solution 2 - MongodbRandy SwansonView Answer on Stackoverflow
Solution 3 - MongodbBelhorView Answer on Stackoverflow
Solution 4 - Mongodbjulien bouteloupView Answer on Stackoverflow
Solution 5 - MongodbEversView Answer on Stackoverflow
Solution 6 - MongodbatmanView Answer on Stackoverflow
Solution 7 - MongodbamanSinghView Answer on Stackoverflow
Solution 8 - MongodbAbdullahView Answer on Stackoverflow
Solution 9 - MongodbEugene GView Answer on Stackoverflow
Solution 10 - MongodbCasey MurrayView Answer on Stackoverflow
Solution 11 - Mongodbb_kikView Answer on Stackoverflow
Solution 12 - MongodbNestor MilyaevView Answer on Stackoverflow
Solution 13 - MongodbvijayView Answer on Stackoverflow
Solution 14 - MongodbadityaView Answer on Stackoverflow
Solution 15 - MongodbGSPView Answer on Stackoverflow
Solution 16 - MongodbIan MercerView Answer on Stackoverflow
Solution 17 - MongodbAndy DongView Answer on Stackoverflow
Solution 18 - Mongodbuser4660857View Answer on Stackoverflow
Solution 19 - MongodbMaryam JafariView Answer on Stackoverflow
Solution 20 - MongodbTasneem HaiderView Answer on Stackoverflow
Solution 21 - MongodbIacobescu RaduView Answer on Stackoverflow
Solution 22 - MongodbWotchinView Answer on Stackoverflow
Solution 23 - MongodbyangsibaiView Answer on Stackoverflow
Solution 24 - MongodbMohammad BayatView Answer on Stackoverflow
Solution 25 - MongodbRajiv SharmaView Answer on Stackoverflow
Solution 26 - MongodbchavyView Answer on Stackoverflow
Solution 27 - MongodbaimuhireView Answer on Stackoverflow
Solution 28 - MongodbShishirView Answer on Stackoverflow