70

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
holys
  • 12,423
  • 13
  • 42
  • 50

6 Answers6

84

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+
Stennie
  • 61,270
  • 14
  • 141
  • 170
  • 5
    To get the path to the database from the mongo shell is, as of 2.6.1: db.serverCmdLineOpts().parsed.storage.dbPath – Marius Jul 31 '14 at 20:45
23

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.

HeatfanJohn
  • 6,806
  • 2
  • 33
  • 41
  • why the book `mongodb:the definitive guide` said that it defaulted to `/data/db`? I don't know why . – holys Oct 05 '12 at 01:54
  • This could be a factor of how the distribution package was built. I installed my copy using `apt-get`. How did you install your copy? Are you still getting the error about dbpath not existing? – HeatfanJohn Oct 05 '12 at 01:59
15

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

mongod --dbpath \mongodb\data

Reference: Manage mongod Processes

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Abhi
  • 6,249
  • 6
  • 39
  • 55
10

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.

Sammaye
  • 42,090
  • 7
  • 95
  • 143
  • 1
    The difference in the distros is based on the packaging & service definition. Start `mongod` without any parameters and you will get a default dbpath of `/data/db` (the only hardcoded default). – Stennie Oct 05 '12 at 07:22
  • 2
    I am using Debain and the default went to var/lib/mongodb as well – Vass Jan 04 '15 at 22:57
7

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

Wernfried Domscheit
  • 46,769
  • 7
  • 65
  • 91
5

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"
The Red Pea
  • 14,933
  • 15
  • 89
  • 118