550

I am using my new mac for the first time today. I am following the get started guide on the mongodb.org up until the step where one creates the /data/db directory. btw, I used the homebrew route.

So I open a terminal, and I think I am at what you called the Home Directory, for when I do "ls", I see folders of Desktop Application Movies Music Pictures Documents and Library.

So I did a

mkdir -p /data/db

first, it says permission denied. I kept trying different things for half and hour and finally :

mkdir -p data/db

worked. and when I "ls", a directory of data and nested in it a db folder do exist.

then I fire up mongod and it complains about not finding data/db

Have I done something wrong?

Now I have done the

sudo mkdir -p /data/db

and when I do a "ls" I do see the data dir and the db dir. inside the db dir though, there is absolutely nothing in it and when I now run mongod

Sun Oct 30 19:35:19 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Sun Oct 30 19:35:19 dbexit: 
Sun Oct 30 19:35:19 [initandlisten] shutdown: going to close listening sockets...
Sun Oct 30 19:35:19 [initandlisten] shutdown: going to flush diaglog...
Sun Oct 30 19:35:19 [initandlisten] shutdown: going to close sockets...
Sun Oct 30 19:35:19 [initandlisten] shutdown: waiting for fs preallocator...
Sun Oct 30 19:35:19 [initandlisten] shutdown: lock for final commit...
Sun Oct 30 19:35:19 [initandlisten] shutdown: final commit...
Sun Oct 30 19:35:19 [initandlisten] shutdown: closing all files...
Sun Oct 30 19:35:19 [initandlisten] closeAllFiles() finished
Sun Oct 30 19:35:19 [initandlisten] shutdown: removing fs lock...
Sun Oct 30 19:35:19 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Sun Oct 30 19:35:19 dbexit: really exiting now

EDIT Getting error message for

sudo chown mongod:mongod /data/db

chown: mongod: Invalid argument

Thanks, everyone!

6
  • 1
    the "Invalid argument" means that the symbolic name for the mongo user is different on your system -- you're probably using a different package or install mechanism for installing MongoDB. You should check your /etc/passwd and /etc/group files for the symbolic name (or uid/gid) of the mongo user grep mongo /etc/passwd /etc/group. If that doesn't work, check the name by checking the owner of the directory ls -ld /var/lib/mongo. Or to see the uid/gid do this: ls -lnd /var/lib/mongo. In my case drwxr-xr-x. 6 *487 480* 4096 Sep 20 2011 ... -- this means use 487:480 as the param
    – Tilo
    Commented Jun 30, 2012 at 20:14
  • using the UID/GID is synonymous to using the symbolic name. e.g. just replace 'mongod:mongod with the uid/gid numbers you found with the method above..
    – Tilo
    Commented Jun 30, 2012 at 20:23
  • Now that we know how to properly add this directory (data/db), why wasn't this directory got included in the installation in the first place?
    – winux
    Commented Nov 28, 2018 at 2:28
  • 1
    If you just upgraded to MacOS 10.15, this answer might help: stackoverflow.com/questions/58283257/… Commented Oct 23, 2019 at 19:02
  • 2
    if you have the last version of MAC:With the new macOS Catalina update, the folder "/data/db" becomes read-only, you cannot modify it. Follow this procedure to create a DB in another folder: 1) Change mongod directory : sudo mongod --dbpath /System/Volumes/Data/data/db 2) Give it an alias : alias mongod="sudo mongod --dbpath/System/Volumes/Data/data/db" Commented Jun 2, 2020 at 19:34

29 Answers 29

687

You created the directory in the wrong place

/data/db means that it's directly under the '/' root directory, whereas you created 'data/db' (without the leading /) probably just inside another directory, such as the '/root' homedirectory.

You need to create this directory as root

Either you need to use sudo , e.g. sudo mkdir -p /data/db

Or you need to do su - to become superuser, and then create the directory with mkdir -p /data/db


Note:

MongoDB also has an option where you can create the data directory in another location, but that's generally not a good idea, because it just slightly complicates things such as DB recovery, because you always have to specify the db-path manually. I wouldn't recommend doing that.


Edit:

the error message you're getting is "Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied". The directory you created doesn't seem to have the correct permissions and ownership -- it needs to be writable by the user who runs the MongoDB process.

To see the permissions and ownership of the '/data/db/' directory, do this: (this is what the permissions and ownership should look like)

$ ls -ld /data/db/
drwxr-xr-x 4 mongod mongod 4096 Oct 26 10:31 /data/db/

The left side 'drwxr-xr-x' shows the permissions for the User, Group, and Others. 'mongod mongod' shows who owns the directory, and which group that directory belongs to. Both are called 'mongod' in this case.

If your '/data/db' directory doesn't have the permissions and ownership above, do this:

First check what user and group your mongo user has:

# grep mongo /etc/passwd
mongod:x:498:496:mongod:/var/lib/mongo:/bin/false

You should have an entry for mongod in /etc/passwd , as it's a daemon.

sudo chmod 0755 /data/db
sudo chown -R 498:496 /data/db    # using the user-id , group-id

You can also use the user-name and group-name, as follows: (they can be found in /etc/passwd and /etc/group )

sudo chown -R mongod:mongod /data/db 

that should make it work..

In the comments below, some people used this:

sudo chown -R `id -u` /data/db
sudo chmod -R go+w /data/db

or

sudo chown -R $USER /data/db 
sudo chmod -R go+w /data/db

The disadvantage is that $USER is an account which has a login shell. Daemons should ideally not have a shell for security reasons, that's why you see /bin/false in the grep of the password file above.

Check here to better understand the meaning of the directory permissions:

http://www.perlfect.com/articles/chmod.shtml

Maybe also check out one of the tutorials you can find via Google: "UNIX for beginners"

25
  • 2
    try to do ls -ld /data/ and ls -ld /data/db/ .. you will see the directory permissions listed on the left side, then the ownership, then the directory name. you should make sure that the directories are writable by the user-id which runs MongoDB.
    – user979339
    Commented Oct 31, 2011 at 3:07
  • 2
    @Tilo Yes I have done them. The first one, the chmod went ok, the second one says "chown: mongod: Invalid argument"
    – Nik So
    Commented Oct 31, 2011 at 18:50
  • 3
    This is whole thread is exactly what I'm going through. But I also can't seem to understand what the user-id and group-id of mongo is on my system nor find any documentation on it anywhere. I'm stuck at the chown mongod:mongod part as its returning an invalid arguement
    – Trip
    Commented Jun 30, 2012 at 13:40
  • 11
    Update I went with sudo touch /data/db/mongod.lock and sudo chmod 0777 /data/db/mongd.lock . She started right up.
    – Trip
    Commented Jun 30, 2012 at 13:51
  • 145
    Running sudo chown -R `id -u` /data/db or sudo chown -R $USER /data/db instead of sudo chown mongod:mongod /data/db did the trick for me Commented Sep 2, 2014 at 20:51
118

After getting the same error as Nik

chown: id -u: Invalid argument

I found out this apparently occurred from using the wrong type of quotation marks (should have been backquotes) Ubuntu Forums

Instead I just used

sudo chown $USER /data/db

as an alternative and now mongod has the permissions it needs.

2
  • 4
    If somebody finds it useful: I had the same problem, but the error message was a bit different (said that the group 'mongod' doesn't exists when I tried chown mongod:mongod) -> however, using chown $USER worked for me, thanks...
    – trainoasis
    Commented Jan 20, 2014 at 10:43
  • 6
    I had to add a -R to this. "sudo chown -R $USER /data/db"
    – Kevin
    Commented Nov 18, 2014 at 21:37
94

This works for me, found in comments:

sudo chown -R $USER /data/db
1
  • 2
    Also for me. Installing mongo via homebrew on my OSX doesn't add a mongod user and group.
    – Zauker
    Commented Sep 23, 2016 at 8:41
79

Create the folder.

sudo mkdir -p /data/db/

Give yourself permission to the folder.

sudo chown `id -u` /data/db

Then you can run mongod without sudo. Works on OSX Yosemite

2
  • 1
    Instead of creating new directory try restarting mongod service. Eg:- service mongod restart Commented Sep 26, 2018 at 9:30
  • 1
    bogdanmac:~ iliebogdanbarbulescu$ sudo chown `id -u` /data/db chown: /data/db: No such file or directory
    – bibscy
    Commented Apr 16, 2020 at 14:49
41

To fix that error on OS X, I restarted and stopped the service: $ brew services restart mongodb $ brew services stop mongodb

Then I ran mongod --config /usr/local/etc/mongod.conf, and the problem was gone.

The error seemed to arise after upgrading the mongodb homebrew package.

0
27

If you run mongo without arguments it's assume you are running on the production machine so it's use the default locations.

for using your own database (dev or just a different one) :

./bin/mongod --dbpath ~/data/db
4
  • Does this explain why it won't use the settings declared in /etc/mongod.conf? Had issues with an installation of 3.6.5 on ubuntu 16.04.
    – Dark Star1
    Commented Jun 27, 2018 at 11:36
  • try to start in in verbose (-v) mode or explicit force the config ( --config ): docs.mongodb.com/manual/reference/program/mongod If you install using apt-get start it with service mongod status/start/stop
    – loreii
    Commented Jun 27, 2018 at 14:46
  • I did look into the mongo logs. All it kept saying way that lack of the /data/db directory was preventing startup. Eventually came across your answer and that seems to be the reason for the issues?
    – Dark Star1
    Commented Jun 28, 2018 at 13:38
  • This also works when using MongoDB in the Linux-Subsystem on Windows, where it's not possible to actually create /data/db at the fs root. Commented Mar 5, 2020 at 8:53
24

Installing through brew on Mac where YOUR_USER_NAME and staff is the group

sudo mkdir -p /data/db
sudo chmod +x+r+w /data/db/
sudo touch /data/db/mongod.lock
sudo chown YOUR_USER_NAME:staff /data/db
sudo chmod +x+r+w /data/db/mongod.lock
sudo chown YOUR_USER_NAME:staff /data/db/mongod.lock
4
  • @MarkusWMahlberg Thanks for that. it one of those things who felt weird but worked. so who should be the owner and group of that file?
    – Gal Bracha
    Commented Dec 3, 2014 at 20:23
  • That depends on your distribution. Have a look at /etc/passwd for the username – the group is likely to be identical. It usually is either mongo or mongodb. Commented Dec 6, 2014 at 11:28
  • @MarkusWMahlberg ok - I fixed it now - when installing through brew it doesn't create the user and group so I just set it to my own user name. see if it's more secure now. thanks
    – Gal Bracha
    Commented Dec 7, 2014 at 19:19
  • Thanks. When developing on mac with brew, this fixes the issue. No need for more security if you are just using test data in mongodb. @MarkusWMahlberg on OS X, the user running mongod is YOUR_USER_NAME if you simply start it with "mongod &". Commented Apr 14, 2015 at 16:53
12

If you are using Mac and running Catalina and Installed Mongodb via Homebrew what you have to do to start is just type this command and you are good to go.

brew services start mongodb-community
0
10

I just wanted to point out here that if you try this and you run into mkdir: /data/db: Read-only file system, please see this comment, which helped me: https://stackoverflow.com/a/58895373.

That way if anyone is on this answer and does Control F for "read only" they will see this

9

MongoDB can be confusing regarding the dbPath folder.

When you run mongod without dbpath then the default path is /data/db

However when you start it as a service, e.g. systemctl start mongod then it reads on configuration file, typially /etc/mongod.cfg and in this config file the defaults are

Platform Package Manager Default storage.dbPath
RHEL / CentOS and Amazon yum /var/lib/mongo
SUSE zypper /var/lib/mongo
Ubuntu and Debian apt /var/lib/mongodb
macOS brew /usr/local/var/mongodb

So, by accident your MongoDB tries to access different data folders depending on how you start the service.

8

Your command will have created the directory structure in the current folder, not the root directory of your computer (which is what the missing / is).

The first command was right, but because you are trying to create a folder in /, which is a protected directory, you need to prefix it with sudo, which is short for "superuser do". You'll then be asked for your password.

So the full command would be:

$ sudo mkdir -p /data/db
8

I had this problem with an existing Mongodb setup. I'm still not sure why it happened, but for some reason the Mongod process couldn't find the mongod.config file. Because it could not find the config file it tried to find the DB files in /data/db, a folder that didn't exist. However, the config file was still available so I made sure the process has permissions to the config file and run the mongod process with the --config flag as follows:

mongod --config /etc/mongod.conf

In the config file itself I had this setting:

storage:
  dbPath: /var/lib/mongodb

And this is how the process could find the real DB folder again.

1
  • 1
    I confirm it happened to me. Instead of the config file, we can just set --dbpath=/var/lib/mongodb. I though I had lost all my data but it's still there.
    – lenhhoxung
    Commented Apr 30, 2019 at 10:46
8

I did

brew install mongodb

on 2018-02-01 and that gave me mongodb version 3.6.2.

Prompted by the answer from orluke above, I tried just

$ brew services restart mongodb

and everything sprang into life. My mongoose.createConnection() call did what I wanted. The GUI MongoDB Compass, the community version, would connect. I used Compass to look at the local.startup_log collection. That had one document in, the log of me just starting the mongoDB service, and that had

cmdLine:Object
    config:"/usr/local/etc/mongod.conf"

and indeed there was such a file:

$ more /usr/local/etc/mongod.conf
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

and there was a /usr/local/var/mongodb directory with lots of obscure files. So that seems to be how the installation works now.

I'm not sure if brew services restart sets the service to run at login. So I did

brew services stop mongodb
brew services start mongodb

and hoped that starts it again after reboot. And, indeed, it did. In fact, now, I think the right thing to do after the initial installation is

brew services start mongodb

and that should start the service and restart it after reboot.

1
6

You're trying to create a directory you don't have root access to.

For testing mongodb, I just use a directory from my user directory like:

cd
mkdir -p temp/
mongod --dbpath .

This will make a mongo database in temp/ from your current working directory

6

I got over this exact same problem by creating the /data/db folders with my window manager. I tried doing it though the terminal at first, and in order to create a folder in the root directory, I had to use sudo.

I just went to the root directory using Finder and created a new folder using 'New Folder'. Totally worked for me.

Note: I'm using OSX.

0
6

Create directory in the Root

sudo mkdir -p /data/db

Now change the Owner

sudo chown -R $USER /data

You're good to go!

mongod

instead of using sudo mongod, you don't need to put everythime password, but for the real project you should use sudo mongod, don't give permission to normal user!

5

You need to create /data/db ... that is a directory called /data/ in your root (i.e. /) and subfolder in there called /db/ ...

You're getting permission errors becuase you need to use sudo to create a direcotry in your root dir in MacOS, sudo lets you run commands as an administrator.

So, run this instead ...

$ sudo mkdir -p /data/db

This will prompt you for a password, it's the same password you use to change system settings (that little dialog that opens when you try and change things in System Preferences for ecample), and likely the same as you use to login.

5

Mongodb when running mongod looks for ~/data/db folder in as a path for db in the root folder of your device.

I solved it by creating a ~/data folder by running mkdir ~/data

In the root folder check if data folder is there by typing ls. Then navigate to the data folder and double check pwd should give you /Users/username/data

Then run this command to create a mongodb db path sudo mongod --dbpath=/Users/username/data

This did it for me and when I ran mongod

4

Just a quick note:

If you tried running mongod without changing the permissions first, you'll likely have a mongod.lock file (and some other files) in the /data/db directory. Even after you change the permissions for the /data/db directory to give access to your $USER, you'll continue to get the "Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied" error. Run ls -al /data/db and you'll probably see that the permissions for the individual files are still set to root for user, not to your $USER. You should remove the mongod.lock file, and the others as well. Then when you run mongod again, everything should work, and you can verify that the file permissions match the directory permissions by running ls -al again.

1
  • this is a good note. I didn't have a lock file, but I did need to change the owner of my data and db folder.
    – Caranicas
    Commented Mar 15, 2016 at 17:49
3

I kept getting the following error when I tried to start mongodb.

"shutting down with code:100" 

I was using the following command:

./mongod --dbpath=~/mongo-data

The fix for me was that I didn't need the "=" sign and this was causing the error. So I did

./mongod --dbpath ~/mongo-data

Just wanted to throw this out there because the error in no way specifies that this is the problem. I almost removed the contents of the ~/mongo-data directory to see if that helped. Glad I remembered that cli args sometimes do not use the "=" sign.

2

Till this date I also used to think that we need to create that /data/db folder for starting mongod command.

But recently I tried to start mongod with service command and it worked for me and there was no need to create /data/db directory.

service mongod start

As to check the status of mongod you can run the following command.

service mongod status
2

This solution solves my problem

  1. Make a directory as

    sudo mkdir -p /data/db

  2. That will make a directory named as db and than try to start with commands

    sudo mongod

If you get another error or problem with starting mongod, You may find problem as

Failed to set up listener: SocketException: Address already in use If you find that another error than you have to kill the running process of mongod by typing to terminal as

ps ax | grep mongod
sudo kill ps_number

and find the mongod running port and kill the process. Another way is to make a specefic port when starting mongod as

sudo mongod --port 27018
2

Starting with MongoDB 4.4, the MongoDB Database Tools are now released separately from the MongoDB Server.

You need to download : https://www.mongodb.com/try/download/database-tools?tck=docs_databasetools

then you copy all the files into /usr/bin and all the command lines will be available.

1

Type "id" on terminal to see the available user ids you can give, Then simply type

"sudo chown -R idname /data/db"

This worked out for me! Hope this resolves your issue.

1

In more current versions of MongoDB, I have 3.2.10, it is stored be default into

/var/lib/mongodb

1
  • 1
    running mongod --dbpath /var/lib/mongodb helps in these circumstances Commented Jun 7, 2017 at 16:28
1

Tilo has the answer that worked for me up until THIS:

sudo chown -R 126:135 /data/db 

I had to use:

sudo chown -R $USER /data/db
0

There is a really dumb way to create this problem, which I have pioneered:

1) leave your mongo install for a while 2) come back and the server is not running 3) attempt to start it, but don't use sudo this time 4) mongo can't find data/db/ because now its looking in the user home dir instead of su home dir

Yes, it is really dumb but if its been a while since you were on the system it can trip you up.

Short answer: make sure you run mongo with the same implied home directory

0

Make sure there's enough Disk Space on your Linux instance.

0

If you are getting mkdir: /data: Read-only file system for command sudo mkdir -p /data/db you may create the directory like this:

mkdir -p ~/data/db. notice the ~ sign please.

After that mongod --dbpath ~/data/db

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