[macos] Mongod complains that there is no /data/db folder

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!

This question is related to macos mongodb

The answer is


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

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.


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.


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.


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

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.


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


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.


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.


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.


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


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

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

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


This works for me, found in comments:

sudo chown -R $USER /data/db

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.


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!


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


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.


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

/var/lib/mongodb


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


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.


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

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.


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

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

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.


Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to mongodb

Server Discovery And Monitoring engine is deprecated Avoid "current URL string parser is deprecated" warning by setting useNewUrlParser to true MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Failed to start mongod.service: Unit mongod.service not found db.collection is not a function when using MongoClient v3.0 MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB: How To Delete All Records Of A Collection in MongoDB Shell? How to resolve Nodejs: Error: ENOENT: no such file or directory How to create a DB for MongoDB container on start up?