MongoDB data directory /data/db not found

In this MongoDB tutorial, I will explain how I managed to resolve the error “MongoDB data directory /data/db not found. We will cover the solution of this by using the different operating systems.

  • MongoDB data directory /data/db not found in windows
  • MongoDB data directory /data/db not found in Ubuntu
  • MongoDB data directory /data/db not found in Mac

MongoDB data directory /data/db not found in windows

Recently, when I installed the MongoDB database in the Windows operating system. I didn’t find any issues while installing it installed successfully. But, when I was trying to run or start the MongoDB server using the mongod command in the terminal getting the following error.

mongodb data directory /data/db not found

You can see the error below:

MongoDB data directory /data/db not found
MongoDB data directory /data/db not found

The error is saying that it is unable to find the /data/db directory. Usually, this error occurs due to incorrect directory settings.

The /data/db directory is used to store the files. And, these directories are mandatory in our system otherwise we are unable to start the MongoDB server.

I also tried uninstalling, reinstalling, restarting, etc but all ended up showing the same error.

Solution:

When I was trying to troubleshoot this error in the windows operating system. I find one solution for this.

You can fix the error “data directory /data/db not found” by creating the /data/db directory on the root or any other custom path.

You can create the directory using the below command:

mkdir -p \data\db

Also, If you want to provide your custom path for the db then you can use the below command:

mongod --dbpath "path"

Now, you can start the MongoDB server and use the MongoDB shell.

mongodb data directory /data/db not found windows
mongodb data directory /data/db not found windows

We have resolved the error and the server is started successfully.

Read MongoDB backup and restore

MongoDB data directory /data/db not found in Ubuntu

If you got the same error “MongoDB data directory /data/db not found” in Ubuntu OS then you can resolve the error following ways.

You can see the below snap for error reference:

MongoDB data directory /data/db not found in Ubuntu
MongoDB data directory not found in Ubuntu

The reason behind getting an error because the system is not able to find the /data/db directory so it is terminating without starting the server.

Solution:

So, you have to create the /data/db directory after that you also have to provide ownership to the directories so that you will be able to start the MongoDB server.

Follow the below commands for creating the directory and providing the ownership:

Step 1: Create the /data/db directory

sudo mkdir /data/db

After executing this command, if you start the server it throws an error “Attempt to create a lock file on a read-only directory” so you have to provide the ownership as well using the below command.

Step 2: Provide the ownership to the /data/db directory

sudo chown -R $USER /data/db

Let’s see the execution of the code the Ubuntu terminal:

MongoDB data directory /data/db not found in Ubuntu
MongoDB data directory /data/db not found in Ubuntu

Now, you can start the MongoDB server and use the MongoDB shell.

Start the server using the below command

mongod --port 27107 --dbpath /data/db

Start the MongoDB shell using the below command

mongo
data directory /data/db not found mongodb ubuntu
data directory /data/db not found mongodb ubuntu

We have successfully resolved the error and we are able to access the MongoDB shell.

Read How does MongoDB create collection

MongoDB data directory /data/db not found in Mac

You are facing the error “MongoDB data directory /data/db not found” in mac OS then you can resolve using the below commands.

Actually, This error occurs because the system is unable to find the /data/db directory. And, these directories are must require to store the files. So, to resolve this error we have to create the directories.

You have to follow the below commands to create the directories:

  • Open the terminal
  • Go to the Users directory using below command
$ cd /Users
  • Now, we will create the data and db directory using below command
# create data directory
$ mkdir data

# go to data directory
$ cd data

# create the db directory in data directory
$ mkdir db
  • Now write the full path to start the MongoDB server
$ mongod --dbpath ~/data/db

After executing this command MongoDB server will start successfully without any error fall. And, you can access the MongoDB shell using the below command.

$ mongo

We have successfully resolved the error in mac OS and now can access the MongoDB shell.

Related MongoDB tutorials:

Conclusion

In short, the MongoDB error dbpath data/db does not exist occurs due to errors in the dbpath settings and the system was not able to find these directories so we created the /data/db directory again and it resolved the error.

In this tutorial, we have resolved the error “MongoDB data directory /data/db not found“. We also cover it by using the different operating systems. These are the following topics that we covered in this tutorial:

  • MongoDB data directory /data/db not found in windows
  • MongoDB data directory /data/db not found in Ubuntu
  • MongoDB data directory /data/db not found in Mac
  • data/db in mongodb
  • mongodb error nonexistentpath: data directory /data/db not found