ChatGPT解决这个技术问题 Extra ChatGPT

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
Looks like mongod shut down badly last time it ran and was not able to clean up the mongod.lock file it created. This file exists to prevent multiple mongod instances from working on the file. If you delete the file and try running mongod again, you should have no problems
See updated question. Same issue if remove lock file
Seems to still be a problem with the lockfile, what are the permissions on the directory that the lockfile is in? TBH I have only seen this in 2 cases: 1) the lockfile already exists and 2) mongod does not have permissions to create the lockfile in the desired location.
You should make sure that the mongo user has access, chown mongodb:mongodb on /var/lib/monogdb, also on the data directory.

m
martinedwards

I use this method to solve the problem:

sudo chown -R mongodb:mongodb /data/db

Add a -R option and it's perfect :)
that's: sudo chown -R id -u /data/db for the uninitiated. :)
Turns out there's a problem with backticks. Try running sudo chown $USER /data/db instead of the original command.
This is indeed the correct answer. @Tampa you should accept this answer. btw - you don't need the id -U or $USER stuff. mongo has its own user/group. you can and should hard code mongodb:mongodb. so the command is simply sudo chown -R mognodb:mognodb /data/db
What does this actually do? It does work, but would love to understand :)
a
ankitjaininfo

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

First I ran sudo service mongodb stop. 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) Then I updated the path in etc/mongodb.conf to /data/db and deleted the old mongo files in /var/lib/mongodb directory. 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. 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


I was struggling with this issue, and realized that if you list you list your destination folder with ls -lahZ it will give you the security context, the context for the mongo data folder should be set like: "sudo chcon -R -u system_u -t mongod_var_lib_t /folder/data" this besides the obvious permissions and user:group combination. Hope it helps.
B
Belhor

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

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

And after that everything worked fine.


This is the best answer, it is most likely a permissions issue with the user running mongod
We are changing/transfering the ownership of /data/db to USERNAME
j
julien bouteloup

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.


E
Evers

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


Yes, it seems owner must be "mongodb" instead of "root" something.
b
bitbot

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.


This is literally the shortest answer and the only answer that solved my problem after countless hours...thanks!
Glad it helped! :) Wish there is way to help people find this answer more easily.
a
amanSingh

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


A
Abdullah

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!


E
Eugene G

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.


C
Community

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:

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


b
b_kik

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.


N
Nestor Milyaev

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.


v
vijay

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

$ sudo chown -R id -u /var/lib/mongodb/ $ sudo chown -R id -u /var/lib/mongodb/mongod.lock $ sudo /etc/init.d/mongod stop $ sudo /etc/init.d/mongod start


should be `id -u`
a
aditya

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


G
GSP

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.


I
Ian Mercer

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

This pointed me in the right direction. ON my server I had to do sudo rm /var/log/mongodb/mongodb.log and sudo rm /tmp/mongodb-27017.sock .
A
Andy Dong

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

u
user4660857

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


F
FelixSFD

I had the same problem.

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

setenforce 0

T
Tasneem Haider

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


I
Iacobescu Radu

On windows be sure the console is started as aministrator


C
Community

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


Considering this is an old question, and your answer does not add anything that is not already there, you should ask your self if it is valuable...
y
yangsibai

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.


M
Mohammad Bayat

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


R
Rajiv Sharma

In Centos Server

this works for me

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

c
chavy

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


a
aimuhire

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!


Running mongod as root is not necessary and may make the system more vulnerable to exploits
S
Shishir

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/

Neither of these are a good idea. The first could make the system more vulnerable to security exploits. The second could allow unprivileged users to tamper with your mongo databases

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now