118

I followed the MongoDb Docs to setup my first MongoDb,

When I start MongoDB using the command

C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe

I get the following error

exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating 
shutdown: going to close listening sockets...
shutdown: going to flush diaglog...
now exiting
shutting down with code:100 
Kraigolas
  • 5,121
  • 3
  • 12
  • 37
Aravind
  • 40,391
  • 16
  • 91
  • 110

19 Answers19

203

MongoDB needs a folder to store the database. Create a C:\data\db\ directory:

mkdir C:\data\db

and then start MongoDB:

C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe

Sometimes C:\data\db folder already exists due to previous installation. So if for this reason mongod.exe does not work, you may delete all the contents from C:\data\db folder and execute mongod.exeagain.

cespon
  • 5,630
  • 7
  • 33
  • 47
  • If you see the details of the error, there is a line that says that the directory `C:\data\db\ ` was not found. Use: `mkdir C:\data\db` – cespon Jan 02 '17 at 02:28
  • @cespon Gold tag.. :P – Aravind May 08 '17 at 18:04
  • Mine was actually `/mongodb_data/db` – Fabricio Feb 09 '18 at 22:16
  • Make sure that data dir and files are owned by mongodb user and group (on linux). – PeterM Apr 16 '18 at 10:34
  • Can it work in d:/ directory. Because the "mongod" command work perfectly in c:/ drive but when I run it in d:/ drive it close after show some errors. And I've already set the "C:/data/db/" folder. Can't we run the command globally in any directory. – Rushi Kadivar May 26 '21 at 09:57
81

For macOS users to fix this issue:

You need to go through the following steps:

Create the “db” directory. This is where the Mongo data files will live. You can create the directory in the default location by running:

sudo mkdir -p /data/db

Make sure that the /data/db directory has the right permissions by running:

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

You're all set now and you can run sudo mongod to start the Mongo server.

It's not working if you run only mongod

Source.

Rodolfo
  • 9
  • 1
  • 7
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • 8
    same in ubuntu 16.04 – Manoj Rana May 28 '18 at 05:00
  • 6
    I get `mkdir: /data/db: Read-only file system` when running the first command. How could I fix this? Thank you. – L C Jul 24 '20 at 10:38
  • 1
    I have the same issue as @LC mentioned here, first I do: ```sudo mkdir -p /data/db```, then I got: ```mkdir: /data/db: Read-only file system```, then I do: ```sudo chown -R `id -un` /data/db```, then I got: ```chown: /data/db: No such file or directory```, can someone help me with this? – Tony Lucas Sep 16 '21 at 04:16
25

Same issue on my Mac (using Brew) solved using:

sudo mongod  
siem
  • 283
  • 2
  • 4
  • it should have been mentioned in the document https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/?_ga=2.112030212.1812901370.1536869187-316348816.1536869187 – Rizwan Atta Sep 13 '18 at 20:38
  • 1
    Old solution, not working. Use @ahmedSanad 's solution. – Parsa Noori Jan 19 '22 at 17:32
23

For macOS users take care of below issue:

if you installing MongoDB Community on macOS using .tgz Tarball

((Starting with macOS 10.15 Catalina, Apple restricts access to the MongoDB default data directory of /data/db. On macOS 10.15 Catalina, you must use a different data directory, such as /usr/local/var/mongodb.))

you can solve it as the following:

(MacOS Catalina onwards)

Apple created a new Volume in Catalina for security purposes. If you’re on Catalina, you need to create the /data/db folder in System/Volumes/Data.

Use this command:

sudo mkdir -p /System/Volumes/Data/data/db

Then, use this command to give permissions:

sudo chown -R `id -un` /System/Volumes/Data/data/db

this will replace normal

sudo mkdir -p /data/db

Make sure that the /data/db directory has the right permissions by running:

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

once you finish and start mongoDB you can use the following in terminal:

sudo mongod --dbpath /System/Volumes/Data/data/db
Dharman
  • 30,962
  • 25
  • 85
  • 135
ahmedSanad
  • 246
  • 2
  • 5
19

To change default db folder C:\data\db in windows, the command is:

--dbpath

For example:

\mongod --dbpath C:\myfolder
backslash-f
  • 7,923
  • 7
  • 52
  • 80
Xu Guanghao
  • 199
  • 1
  • 2
7

Mac Users

Instead of running MongoDB with:

sudo mongod

You can use mongod instead if you:

  1. Locate the data folder of mongodb (usually ~/data)
  2. Add permission to read + write with sudo chmod -R ugo+rw data

If you need to use sudo when running mongodb (sudo mongod), that means you don't have read and write permission on the mongodb data folder

Jay C
  • 103
  • 1
  • 5
  • 1
    Thanks for this. I had to add permission to read + write to /data I don't understand what i did wrong but i have set my data folder to ~/Applications/mongodb/data – vinsentos Dec 05 '17 at 03:31
5

Please take following steps:

  1. As other friends mentioned, you should make a directory first for your database data to be stored. This folder could be something like:

    C:\mongo-data

  2. From command line navigate to where you have installed mongodb and where mongod.exe resides. In my case the full path is:

    C:\Program Files\MongoDB\Server\3.4\bin

  3. From here run mongod.exe and pass it the path to the folder you created in step one using the flag --dbpath as follows:

    mongod.exe --dbpath "C:\mongo-data"

Please Note: If you are on windows it is necessary to use double-quotes ("") in the above to run properly.

In this way you will get something like the following:

2017-06-14T12:45:59.892+0430 I NETWORK  [thread1] waiting for connections on port 27017

If you use single quotes (' ') on windows, you will get:

2017-06-14T01:13:45.965-0700 I CONTROL  [initandlisten] shutting down with code:100

Hope it helps to resolve the issue.

3

To run Mongo DB demon with mongod command, you should have a database directory, probably you need to run:

mkdir C:\data\db

Also, MongoDB need to have a write permissions for that directory or it should be run with superuser permissions, like sudo mongod.

Dmytro Medvid
  • 4,988
  • 3
  • 25
  • 29
2

1.If it shows error (shutting down with code 100) that means it is not finding the desired location of file.

1.a If its before macOS Catalina then create directory with sudo mkdir -p /data/db and give permissions to use it
sudo chown -R id -un /data/db.

1.b if it macOS Catalina onwards then make sudo mkdir -p /System/Volumes/data/db and give it permissions sudo chown -R id -un /System/Volumes/data/db.

2.Starting mongo db brew services run mongodb-community

3.Type mongod or mongod --dbpath /System/Volumes/Data/data/db

4.And if the mongod show error (shutting down with code 48) that
means the port is being already use so you can do two things

4.a Either you change the port of mongod by specifying port
number mongod --dbpath /System/Volumes/Data/data/db —port 27018.

4.b Or You can kill the process at that port by finding the process by sudo lsof -i :27017 and then kill by command
kill -9

5.Repeat the step 2 and 3.

1

I kept getting the following error when I tried to start mongodb (on mac os).

"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.

Cloudish123
  • 847
  • 1
  • 9
  • 14
1

first you have to create data directory where MongoDB stores data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB.

if you have install in C:/ drive then you have to create data\db directory. for doing this run command in cmd

C:\>mkdir data\db

To start MongoDB, run mongod.exe.

"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath="c:\data\db"

The --dbpath option points to your database directory. enter image description here

Connect to MongoDB.

"C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"

to check all work good :

show dbs

enter image description here

0

In MacOS:-

If you forgot to give the path of the previously created database while running the mongo server, the above error will appear.

sudo ./mongod --dbpath ../../mongo-data/

Reference

Note :- ./mongod && ../../mongo-data is relative path. So you can avoid it by configuration in environment variable

Reference

Shrawan
  • 7,128
  • 4
  • 29
  • 40
0

For windows i've got same issue.

The fix was - i need to run command line as administrator.

Tomas J
  • 21
  • 4
  • 1
    I would suggest to reword the answer to something like this: "If you have this issue under Windows, make sure that you're running the command line as an administrator". – Filnor May 30 '18 at 07:18
0

if you already have the directory, check the dir permissions or try to restart mongo with sudo. sudo brew services start mongodb

rabie jegham
  • 125
  • 2
  • 3
0

In my case, I got a similar error and it was happening because I had run mongod with the root user and that had created a log file only accessible by the root. I could fix this by changing the ownership from root to the user you normally run mongod from. The log file was in /var/lib/mongodb/journal/

0

I you are using Virtualbox check your VM.

docker-machine ssh

df -h

Look at dev/sda1 if you do not have any free space this may be due to a large number of images, or containers. you can remove them using "docker rm" and "docker rmi"

halfelf
  • 9,737
  • 13
  • 54
  • 63
0

This exit code will also be given if you are changing MongoDB versions and the data directory is incompatible, such as with a downgrade. Move the old directory elsewhere, and create a new directory (as per the instructions given in other answers).

Pistos
  • 23,070
  • 14
  • 64
  • 77
0

Aravind.

It happened with me too because I stopped the MongoDB by the Task Manager.

Creating the C:\data\db folder and starting the MongoDB at C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe worked for me, as cespon suggested, but the MongoDB didn't show any of my Databases previously created.

Then, removing the C:\data\db folder and repairing the installation with the MongoDB installer, I recovered my data and started normally the MongoDB.

(I'm very new with MongoDB, but it helped me solve this problem and recover may previews data).

0

typed mongod and getting error

Errors:

exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating

shuts down with Code 100

Then try with (create data and db folder with all permission)

mongod --dbpath=/data

use new tab and type mongo.

>use dbs

If still you are facing prob then you can check for mac catalina: (https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x-tarball/)

for windows: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows-unattended/

Prakash Singh
  • 377
  • 2
  • 4