200

My host came with a mongodb instance and there is no /db directory so now I am wondering what I can do to find out where the data is actually being stored.

4
  • 7
    There is a more recent question on this which has a better answer: "What is the default database path for mongodb?". The data path will either be the default of /data/db (if no config file is being used) or discoverable via db.adminCommand("getCmdLineOpts").parsed.dbpath.
    – Stennie
    Jan 31, 2014 at 5:34
  • 6
    on OSX, mongo 3.0.4, i had to use "db.adminCommand("getCmdLineOpts").parsed.storage.dbPath"
    – ski_squaw
    Jul 24, 2015 at 23:45
  • 2
    @Stennie your comment helped me (I'm on Windows so I'm using Robomongo), but your question-link points at this question, not at your intended target -- stackoverflow.com/a/12738557/112764 -- and in 3.x, it's actually db.adminCommand("getCmdLineOpts").parsed.storage.dbpath
    – NateJ
    May 3, 2016 at 16:10
  • 5
    @NateJ Thanks! The answer on the related question has examples for MongoDB 2.6+ as well as the older form in <= 2.4. There's also a shell helper you can use instead of remembering to change to the admin database or using adminCommand: db.serverCmdLineOpts().parsed.storage.dbPath (MongoDB 2.6+).
    – Stennie
    May 4, 2016 at 0:15

13 Answers 13

164

mongod defaults the database location to /data/db/.

If you run ps -xa | grep mongod and you don't see a --dbpath which explicitly tells mongod to look at that parameter for the db location and you don't have a dbpath in your mongodb.conf, then the default location will be: /data/db/ and you should look there.

6
  • 24
    @user2384994 mine was located at /etc/mongod.conf
    – basickarl
    Mar 28, 2015 at 2:44
  • 1
    Did not see the path, but /usr/bin/mongod -f /etc/mongod.conf. Took the dbpatch from the config.
    – Putnik
    Jun 5, 2016 at 16:41
  • 11
    Check /usr/local/etc/mongod.conf(config file) and /usr/local/var/mongodb (database location) as well. On some systems, these will be the defaults. Aug 5, 2016 at 2:43
  • 1
    Well this one SHOULD print the current path precisely for all mongodb version stackoverflow.com/a/52036070/248616
    – Nam G VU
    Jun 2, 2019 at 10:51
  • i.e. the db.serverCmdLineOpts()
    – Nam G VU
    Jun 2, 2019 at 10:51
63

What does your configuration file say?

$ grep dbpath /etc/mongodb.conf

If it is not correct, try this, your database files will be present on the list:

$ sudo lsof -p `ps aux | grep mongodb | head -n1 | tr -s ' ' | cut -d' ' -f 2` | grep REG

It's /var/lib/mongodb/* on my default installation (Ubuntu 11.04).

Note that there is also a /var/lib/mongodb/mongod.lock file holding mongod PID for convenience, however it is located in the data directory - which we are looking for...

3
  • No /var/lib directory on this server and those two commands do not work (first one cannot find /etc/mongodb.conf second one unrecognized lsof) Aug 30, 2011 at 20:33
  • Maybe you should add infos for your host. Doesn't seem to be an Ubuntu like distro.
    – DrColossos
    Aug 31, 2011 at 7:15
  • 6
    On Ubuntu 16.04 mongodb v3.4.10, it is $ grep dbPath /etc/mongod.conf
    – ihsan
    Nov 20, 2017 at 11:10
54

I find db.serverCmdLineOpts() the most robust way to find actual path if you can connect to the server. The "parsed.storage.dbPath" contains the path your server is currently using and is available both when it's taken from the config or from the command line arguments.

Also in my case it was important to make sure that the config value reflects the actual value (i.e. config didn't change after the last restart), which isn't guaranteed by the solutions offered here.

db.serverCmdLineOpts()

Example output:

{
    "argv" : [ 
        // --
    ],
    "parsed" : {
        "config" : "/your-config",
        "storage" : {
            "dbPath" : "/your/actual/db/path",
            // --
        }
    },
    "ok" : 1.0
}
3
  • This looks great, but is there a way to do it on mongocxx?
    – Dariusz
    Sep 19, 2021 at 23:45
  • @Dariusz, as far as I understand it, it's just a C++ driver. Are you unable to connect to mongo directly via its native client?
    – glock18
    Sep 20, 2021 at 7:22
  • This should have been the correct answer! It always shows what the db sees
    – EigenFool
    Feb 14 at 13:58
36

In the newer version of mongodb v2.6.4 try:

grep dbpath /etc/mongod.conf

It will give you something like this:

dbpath=/var/lib/mongodb

And that is where it stores the data.

1
  • Mine was here also. For people that wanted to find this directory to check file sizes: du -sh * | sort -hr
    – user993683
    May 8, 2016 at 9:25
31

While this question asks about Linux/Unix instances of Mongo, it's one of the first search results regardless of the operating system used, so for future Windows users that find this:

If MongoDB is set up as a Windows Service in the default manner, you can usually find it by looking at the 'Path to executable' entry in the MongoDB Service's Properties:

Properties Window showing 'Path to Executable' option

2
  • It is not that obvious, but questions is for *nix os
    – st78
    Nov 19, 2017 at 7:01
  • 4
    @st78 I know, but it's still one of the first Google results when searching for the location in Windows, which is why I left an answer addressing the Windows path (once I figured it out) for the people like me who stumble across it.
    – Robotnik
    Nov 19, 2017 at 10:09
15

From my experience the default location is /var/lib/mongodb after I do

sudo apt-get install -y mongodb-org
1
  • 1
    I can confirm this for Linux Mint 17.3 users. May 30, 2017 at 13:44
14

Actually, the default directory where the mongod instance stores its data is

/data/db on Linux and OS X,

\data\db on Windows

To check the same, you can look for dbPath settings in mongodb configuration file.

  • On Linux, the location is /etc/mongod.conf, if you have used package manager to install MongoDB. Run the following command to check the specified directory:
    grep dbpath /etc/mongodb.conf
    
  • On Windows, the location is <install directory>/bin/mongod.cfg. Open mongod.cfg file and check for dbPath option.
  • On macOS, the location is /usr/local/etc/mongod.conf when installing from MongoDB’s official Homebrew tap.

The default mongod.conf configuration file included with package manager installations uses the following platform-specific default values for storage.dbPath:

+--------------------------+-----------------+------------------------+
|         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 |
+--------------------------+-----------------+------------------------+

The storage.dbPath setting in the configuration file is available only for mongod.

The Linux package init scripts do not expect storage.dbPath to change from the defaults. If you use the Linux packages and change storage.dbPath, you will have to use your own init scripts and disable the built-in scripts.

Source

1
  • Actually this should be the top-answer! The default dbPath are different to the dbPath in pre-installed mongod.cfg config files. Many people don't recognize it. Mar 4, 2021 at 9:24
9

I found mine here on a OSX system /usr/local/var/mongodb

2
  • 4
    This is true for Homebrew users
    – Matthew
    Nov 20, 2017 at 21:47
  • 1
    The question is "How can I find" the mongodb data storage location. not just the path for a specific user or platform.
    – BJYC
    Jul 31, 2018 at 16:11
7

For windows Go inside MongoDB\Server\4.0\bin folder and open mongod.cfg file in any text editor. Then locate the line that specifies the dbPath param. The line looks something similar

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

4

On MongoDB 4.4+ and on CentOS 8, I found the path by running:

grep dbPath /etc/mongod.conf
2

Found it just by poking around in /var/db. Thanks for the help though--I am sure these answers apply to other systems (e.g. Ubuntu) and will help others!

2

If you could somehow locate mongod.log and the do a grep over it

grep dbpath mongod.log

The value for dbpath is the data location for mongodb!! All the best :)

1

When you start it up it shows you. But I don't know if it is something you can do or not on your host. If you have access to the command line and can restart the service, you will get something like:

    2016-11-15T12:57:09.182-0500 I CONTROL  [initandlisten]
 MongoDB starting : pid=16448 port=27017 dbpath=C:\data\db\ 

Not the answer you're looking for? Browse other questions tagged or ask your own question.