What is the default database path for MongoDB?

Mongodb

Mongodb Problem Overview


I got an error about dbpath (/data/db/) does not exist, but /etc/mongodb.conf named it dbpath = /var/lib/mongodb. So, which is the default dbpath for MongoDB?

Mongodb Solutions


Solution 1 - Mongodb

The default dbpath for mongodb is /data/db.

There is no default config file, so you will either need to specify this when starting mongod with:

 mongod --config /etc/mongodb.conf

.. or use a packaged install of MongoDB (such as for Redhat or Debian/Ubuntu) which will include a config file path in the service definition.

Note: to check the dbpath and command-line options for a running mongod, connect via the mongo shell and run:

db.serverCmdLineOpts()

In particular, if a custom dbpath is set it will be the value of:

db.serverCmdLineOpts().parsed.dbpath           // MongoDB 2.4 and older
db.serverCmdLineOpts().parsed.storage.dbPath   // MongoDB 2.6+

Solution 2 - Mongodb

I have version 2.0.7 installed on Ubuntu and it defaulted to /var/lib/mongodb/ and that is also what was placed into my /etc/mongodb.conf file.

Solution 3 - Mongodb

For a Windows machine start the mongod process by specifying the dbpath:

mongod --dbpath \mongodb\data

Reference: Manage mongod Processes

Solution 4 - Mongodb

I depends on the version and the distro.

For example the default download pre-2.2 from the MongoDB site uses: /data/db but the Ubuntu install at one point used to use: var/lib/mongodb.

I think these have been standardised now so that 2.2+ will only use data/db whether it comes from direct download on the site or from the repos.

Solution 5 - Mongodb

The dbPath in Mongo can be confusing. If you don't specify the dbPath at all (neither as command line parameter nor in mongod.conf file) then it defaults to

  • /data/db on Linux and macOS
  • \data\db on Windows (on current drive)

However, the default mongod.conf files which comes along the installation uses these ones:

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
Windows MSI C:\Program Files\MongoDB\Server\{release}\data\

So, you must carefully check what you are using.

See Run-time Database Configuration

Solution 6 - Mongodb

The Windows x64 installer shows the a path in the installer UI/wizard.

You can confirm which path it used later, by opening your mongod.cfg file. My mongod.cfg was located here C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg (change for your version of MongoDB!

When I opened my mongd.cfg I found this line, showing the default db path:

dbPath: C:\Program Files\MongoDB\Server\4.0\data

However, this caused an error when trying to run mongod, which was still expecting to find C:\data\db:

2019-05-05T09:32:36.084-0700 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory C:\data\db\ not found., terminating

You could pass mongod a --dbpath=... parameter. In my case:

mongod --dbpath="C:\Program Files\MongoDB\Server\4.0\data"

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
QuestionholysView Question on Stackoverflow
Solution 1 - MongodbStennieView Answer on Stackoverflow
Solution 2 - MongodbHeatfanJohnView Answer on Stackoverflow
Solution 3 - MongodbAbhiView Answer on Stackoverflow
Solution 4 - MongodbSammayeView Answer on Stackoverflow
Solution 5 - MongodbWernfried DomscheitView Answer on Stackoverflow
Solution 6 - MongodbThe Red PeaView Answer on Stackoverflow