Mongod complains that there is no /data/db folder

MacosMongodb

Macos Problem Overview


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!

Macos Solutions


Solution 1 - Macos

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"

Solution 2 - Macos

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.

Solution 3 - Macos

This works for me, found in comments:

sudo chown -R $USER /data/db

Solution 4 - Macos

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

Solution 5 - Macos

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.

Solution 6 - Macos

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

Solution 7 - Macos

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

Solution 8 - Macos

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

Solution 9 - Macos

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

Solution 10 - Macos

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.

Solution 11 - Macos

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.

Solution 12 - Macos

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

Solution 13 - Macos

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

Solution 14 - Macos

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.

Solution 15 - Macos

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.

Solution 16 - Macos

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.

Solution 17 - Macos

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!

Solution 18 - Macos

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.

Solution 19 - Macos

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.

Solution 20 - Macos

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

Solution 21 - Macos

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

Solution 22 - Macos

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.

Solution 23 - Macos

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

Solution 24 - Macos

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.

Solution 25 - Macos

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

/var/lib/mongodb

Solution 26 - Macos

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

Solution 27 - Macos

After (re)-installing the tools package, I got a similar error on a Windows 10 device;

> exception in initAndListen: NonExistentPath: Data directory C:\data\db\ not found., terminating

Solution Analog to as explained for the linux systems: simply making the folder is sufficient to be able to start the mongod.exe (mongoDB server).

Thought I might leave it for people that end up here with the same search terms on a Windows device.

Solution 28 - Macos

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

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
QuestionNik SoView Question on Stackoverflow
Solution 1 - MacosTiloView Answer on Stackoverflow
Solution 2 - MacosjagoughView Answer on Stackoverflow
Solution 3 - MacosIman MohamadiView Answer on Stackoverflow
Solution 4 - MacosConnor LeechView Answer on Stackoverflow
Solution 5 - MacosorlukeView Answer on Stackoverflow
Solution 6 - MacosloreiiView Answer on Stackoverflow
Solution 7 - MacosGal BrachaView Answer on Stackoverflow
Solution 8 - MacosvhiktordomView Answer on Stackoverflow
Solution 9 - MacosRussellView Answer on Stackoverflow
Solution 10 - MacosTal DelbariView Answer on Stackoverflow
Solution 11 - Macosemrys57View Answer on Stackoverflow
Solution 12 - MacosblubberboView Answer on Stackoverflow
Solution 13 - MacosEhevuTovView Answer on Stackoverflow
Solution 14 - MacosJupoView Answer on Stackoverflow
Solution 15 - MacosWernfried DomscheitView Answer on Stackoverflow
Solution 16 - MacosJustin JenkinsView Answer on Stackoverflow
Solution 17 - MacosEricgitView Answer on Stackoverflow
Solution 18 - Macoswhiny_nilView Answer on Stackoverflow
Solution 19 - MacosCloudish123View Answer on Stackoverflow
Solution 20 - MacosDevendra BhatView Answer on Stackoverflow
Solution 21 - MacosMehedi AbdullahView Answer on Stackoverflow
Solution 22 - MacosJeremy PiednoelView Answer on Stackoverflow
Solution 23 - MacosstacksonstacksonstacksView Answer on Stackoverflow
Solution 24 - MacosPrakhar AgrawalView Answer on Stackoverflow
Solution 25 - MacosillcrxView Answer on Stackoverflow
Solution 26 - MacosKingdomBView Answer on Stackoverflow
Solution 27 - Macosa.t.View Answer on Stackoverflow
Solution 28 - MacosmtysonView Answer on Stackoverflow