36

this is the error:

(mysite)zjm1126@zjm1126-G41MT-S2:~$ /usr/local/mongodb/bin/mongod 
/usr/local/mongodb/bin/mongod --help for help and startup options
Wed Apr 27 10:02:41 [initandlisten] MongoDB starting : pid=2652 port=27017 dbpath=/data/db/ 32-bit 

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
**       see http://blog.mongodb.org/post/137788967/32-bit-limitations
**       with --dur, the limit is lower

Wed Apr 27 10:02:41 [initandlisten] db version v1.8.1, pdfile version 4.5
Wed Apr 27 10:02:41 [initandlisten] git version: a429cd4f535b2499cc4130b06ff7c26f41c00f04
Wed Apr 27 10:02:41 [initandlisten] build sys info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Wed Apr 27 10:02:41 [initandlisten] exception in initAndListen std::exception: Unable to create / open lock file for lockfilepath: /data/db/mongod.lock errno:13 Permission denied, terminating
Wed Apr 27 10:02:41 dbexit: 
Wed Apr 27 10:02:41 [initandlisten] shutdown: going to close listening sockets...
Wed Apr 27 10:02:41 [initandlisten] shutdown: going to flush diaglog...
Wed Apr 27 10:02:41 [initandlisten] shutdown: going to close sockets...
Wed Apr 27 10:02:41 [initandlisten] shutdown: waiting for fs preallocator...
Wed Apr 27 10:02:41 [initandlisten] shutdown: closing all files...
Wed Apr 27 10:02:41 closeAllFiles() finished
Wed Apr 27 10:02:41 [initandlisten] shutdown: removing fs lock...
Wed Apr 27 10:02:41 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Wed Apr 27 10:02:41 dbexit: really exiting now

7 Answers 7

87

The user account that is starting MongoDB needs to have write privileges to the /data/db/ directory ... you can change this like so ...

This command will change the owner of the /data/db/ to the user account being used.

$ sudo chown `id -u` /data/db
$ mongod 

If you still run into problems with MongoDB starting, you need to delete /data/db/mongod.lock and then run repair on your database ...

mongod --repair

More about the repair command here.

5
  • The documentation about --repair states: Do not remove the mongod.lock file. If mongod is unable to start, use one of the methods above to correct the situation. Removing the lock file will allow the database to start when its data may be corrupt. Dec 9, 2011 at 11:24
  • 1
    That's why it says if you delete it run --repair, there isn't a problem removing the file if are just going to run repair (which was the point.) You leave yourself open to problems if you remove the file ... then restart the DB like normal, with no repair. Dec 9, 2011 at 22:49
  • 2
    This works if I call mongod directly from the command line. But when I try to start using an init.d service start script, it fails. Even when I use sudo. Any ideas what else I need to do?
    – VinnyD
    Oct 23, 2012 at 22:56
  • 1
    The directory didn't exist for me, so I had to run sudo mkdir -p /data/db first -- then this worked fine, thanks :)
    – meshy
    Nov 6, 2012 at 14:02
  • 2
    I found that I had to run the command recursively for it to work: sudo chown -R id -u /data/db Aug 5, 2014 at 4:44
14

Either sudo the call to mongod or change the permissions of /data/db/mongod.lock so that it is writable by you.

1
  • +1 for using sudo when calling mongod. I had to remove mongod.lock, run mongod --repair and then started the mongo service using sudo service mongod start.
    – Tass
    Aug 2, 2012 at 19:48
12

I resolved this by doing a chown on mongodb ie.

sudo chown -R mongodb:mongodb /data

This is because, when you do

ls -lrth /data
drwxr-xr-x 2 mongodb mongodb  6 Jan  5 02:31 journal
-rwxr-xr-x 1 mongodb mongodb  0 Jan  5 02:31 mongod.lock
drwxr-xr-x 2 ubuntu  root    24 May  8 20:02 configdb

Hope this helps.

4
  • how do i do this on MACOSX i get the following error - Harshas-MacBook-Pro:~ harshamv$ sudo chown -R mongodb:mongodb /data/db Password: chown: mongodb: illegal group name
    – Harsha M V
    Jun 14, 2014 at 6:54
  • Please check the group name. It might be mongod instead of mongodb unlike ubuntu.
    – Swathi
    Jul 2, 2014 at 14:16
  • On my MAC OSX, I got it to work after running this: sudo chown -R id -u /data/db Aug 5, 2014 at 4:45
  • this answer is broken for an ubuntu 16.0.4 distro.. you have to use the id and -u for the chown... this reasonably and frustratingly breaks everything Sep 18, 2017 at 6:47
5

Make sure you do not have a mongod instance running.

This is what worked for me. I checked to see if there was an instance running in the background at first by looking in the terminal like this: ps aux | grep mongo

and I saw that there was: 48358 bla bla mongod

so I killed it: kill 48358

and then I was able to run it fine.

4

Here, how i solved it

  1. After removing lock file I repaired it using sudo mongod --repair --config=/etc/mongodb.conf, After this I Checked mongod.lock permission using ls -l /var/lib/mongodb/mongod.lock
  2. it was showing root is owner of lock file, so i changed it back to mongodb using sudo chown mongodb:mongodb /var/lib/mongodb/*
  3. then restart mongodb service using sudo service mongodb restart
0

On Mac OSX Yosemite after standard install with brew:

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

if it not starts (check if there are no hanging and blocking connections):

lsof -i:27017

outputs something like:

COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mongod  16948 andi    8u  IPv4 0x883a71bc9412939d      0t0  TCP *:27017 (LISTEN)

take value of PID and run:

kill -9 <PID>

now you should be able to start mongo server: mongod and then e.g in separate console window connect to it with mongo

0

If you want to rescue a member of a replica set don't use this methods described above. Instead use the procedures described in the ReSync-documentation of MongoDB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.