193

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
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 1
    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 – ACE Mar 05 '13 at 16:49
  • 2
    See updated question. Same issue if remove lock file – Tampa Mar 05 '13 at 17:01
  • 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. – ACE Mar 05 '13 at 17:11
  • The lock file was in /var/lib/mongodb/mongod.lock – Tampa Mar 05 '13 at 17:17
  • but its trying to create lock in /data...what permissions does /data need to have? – Tampa Mar 05 '13 at 17:19
  • I started by using this command...sudo mongod --dbpath=/data but that is in the config file. I dont know what I have to do that. Now it works – Tampa Mar 05 '13 at 17:47
  • 2
    You should make sure that the mongo user has access, `chown mongodb:mongodb on /var/lib/monogdb`, also on the data directory. – Hans N. Hjort Mar 05 '13 at 19:13
  • 1
    https://gist.github.com/adamgibbons/cc7b263ab3d52924d83b – BARJ Nov 29 '16 at 04:48

28 Answers28

485

I use this method to solve the problem:

sudo chown -R mongodb:mongodb /data/db
martinedwards
  • 5,577
  • 1
  • 33
  • 35
Haimei
  • 12,577
  • 3
  • 50
  • 36
  • 8
    Add a `-R` option and it's perfect :) – Adrien May 23 '14 at 13:46
  • 7
    that's: sudo chown -R `id -u` /data/db for the uninitiated. :) – Ryan May 28 '14 at 15:33
  • -R recursively applies it to all files. – peterhurford Aug 05 '14 at 04:43
  • I get `chown: invalid option -- 'u'` – max pleaner Oct 27 '14 at 07:15
  • 1
    I get ```chown: `id: illegal user name``` when I run that command. Any chance someone knows why? – Paymahn Moghadasian Jan 25 '15 at 20:06
  • 8
    Turns out there's a problem with backticks. Try running ```sudo chown $USER /data/db``` instead of the original command. – Paymahn Moghadasian Jan 25 '15 at 20:12
  • Why isn't this the accepted answer? Elegant one line command vs. a pdf, video and fatty paragraph. Hmm.. – Cumulo Nimbus Feb 25 '15 at 05:14
  • 7
    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` – guy mograbi Feb 27 '15 at 08:20
  • 1
    Works great, but could someone explain what this code is doing? Thanks! – Kevin Zhao May 13 '15 at 02:06
  • 1
    @guymograbi ¿Did you mean mo**ng**odb `sudo chown -R mongodb:mongodb /data/db` ? :) – Andreybeta May 19 '15 at 13:58
  • @Andrey - yes. if anyone can edit my comment that would be great. – guy mograbi May 21 '15 at 06:16
  • 12
    What does this actually do? It does work, but would love to understand :) – zero_cool Jun 17 '15 at 06:54
  • 3
    @Jackson_Sandland yea, I had the same thought, and here you go! cheers guys :D http://stackoverflow.com/questions/32383934/meaning-of-sudo-chown-r-id-u-data-db/32384204#32384204 – ey dee ey em Sep 03 '15 at 19:53
  • 1
    Running the -R mongodb:mongodb version of this solution I got an error saying 'chown: mongodb: illegal group name'. Running the 'id -u' version worked great for me, though. Thanks to everyone who has posted about this! – Ross Henderson Apr 19 '16 at 16:09
  • In OSX, I used this to set the owner of the data and log directories for mongodb. – mtadd Jul 11 '16 at 19:46
  • 1
    @zero_cool The command `chown`, an abbreviation of change owner, is used on Unix-like systems to change the owner of file system files, directories. Unprivileged users who wish to change the group membership of a file that they own may use `chgrp`. [See this tutorial for more examples](https://www.howtoforge.com/linux-chown-command/) – Anima-t3d Nov 06 '17 at 03:16
  • @Anima-t3d appreciate the clarification! ty! – zero_cool Nov 06 '17 at 04:56
  • This does not work for Amazon Linux using Mongo version 4.x – Blake Sep 17 '18 at 17:34
  • I was using fish as my shell and I was getting the error `chown: id: illegal user name` because of the backticks likely. Make sure you are using bash for this solution to work. Once I switched my shell to bash, the answer (`sudo chown -R \`id -u\` /data/db`) worked great! – Joshua Dyck Oct 10 '18 at 20:31
123

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

ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75
Randy Swanson
  • 1,254
  • 1
  • 9
  • 4
  • 8
    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. – jmdiego Feb 18 '15 at 07:17
84

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

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

And after that everything worked fine.

Belhor
  • 920
  • 6
  • 8
54

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.

julien bouteloup
  • 3,022
  • 22
  • 16
12

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

Evers
  • 1,858
  • 1
  • 17
  • 18
10

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.

kvn
  • 2,210
  • 5
  • 20
  • 47
  • This is literally the shortest answer and the only answer that solved my problem after countless hours...thanks! – moomoochen Oct 09 '18 at 02:04
  • Glad it helped! :) Wish there is way to help people find this answer more easily. – kvn Oct 09 '18 at 05:12
7

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

amanSingh
  • 81
  • 1
  • 5
7

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!

Abdullah
  • 126
  • 2
  • 3
6

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.

Eugene G
  • 456
  • 4
  • 11
5

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.

Community
  • 1
  • 1
Casey Murray
  • 1,582
  • 17
  • 21
4

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.

b_kik
  • 49
  • 4
4

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.

Nestor Milyaev
  • 5,845
  • 2
  • 35
  • 51
2

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

vijay
  • 10,276
  • 11
  • 64
  • 79
2

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

aditya
  • 575
  • 7
  • 19
1

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.

GSP
  • 3,763
  • 3
  • 32
  • 54
1

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
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
1

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
Andy Dong
  • 405
  • 5
  • 10
1

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

user4660857
  • 773
  • 6
  • 6
1

I had the same problem.

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

setenforce 0
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
0

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

Tasneem Haider
  • 359
  • 3
  • 9
0

On windows be sure the console is started as aministrator

0

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

Community
  • 1
  • 1
Wotchin
  • 160
  • 6
  • 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... – Nic3500 Dec 27 '17 at 04:23
0

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.

yangsibai
  • 1,637
  • 12
  • 23
0

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

Mohammad Bayat
  • 151
  • 2
  • 5
0

In Centos Server

this works for me

chown -R mongod:mongod /var/lib/mongo
Rajiv Sharma
  • 6,746
  • 1
  • 52
  • 54
0

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

chavy
  • 841
  • 10
  • 20
-2

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!

aimuhire
  • 58
  • 4
-2

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/
Shishir
  • 327
  • 3
  • 6
  • 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 – qbert220 Sep 14 '18 at 09:49