如何解决 MongoDB 实例化失败的问题

我跟随 MongoDb 医生设置了我的第一个 MongoDb,

当我使用以下命令启动 MongoDB 时

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

我得到以下错误

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
185411 次浏览

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.

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

sudo mongod

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.

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

--dbpath

For example:

\mongod --dbpath C:\myfolder

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.

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

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

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.

For windows i've got same issue.

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

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

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.

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/

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"

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

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

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/

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

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.

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