116

I am kind of new to mac as well as mongodb.

I have a weird doubt, accessing the database created using mongodb on mac?

I know, in windows there is a folder called c:\data\db, where my database files are stored.

How and where in mac, the database is stored.

I remember doing something like

sudo mkdir -p /data/db
sudo chown `id -u` /data/db

to create such a folder on mac, but I didn't find any database file in this folder, though i created a database.

Where are the database files saved on mac?

Any help would be really appreciated.

1

6 Answers 6

271

If MongoDB is installed on macOS via Homebrew, the default data directory depends on the type of processor in the system.

Intel Processor Apple Silicon Processor (M1, M2, etc)
Data Directory /usr/local/var/mongodb /opt/homebrew/var/mongodb
Configuration file /usr/local/etc/mongod.conf /opt/homebrew/etc/mongod.conf
Log directory /usr/local/var/log/mongodb /opt/homebrew/var/log/mongodb

Run brew --prefix to see where Homebrew installed these files.

See the MongoDB "Install on macOS" documentation for additional details.

3
  • I was looking for some light in the darkness by checking the formula at github.com/Homebrew/homebrew/blob/master/Library/Formula/… thanks.
    – alexserver
    Commented Mar 12, 2014 at 17:42
  • 6
    In newer versions of Homebrew, it's /opt/homebrew/var/mongodb.
    – JW.
    Commented Jan 15, 2022 at 19:29
  • 1
    Thanks @JW. I updated the answer to include what I found in the MongoDB docs. Let me know if you see anything else that needs updating. Commented Jan 16, 2022 at 23:43
165

Thanks @Mark, I keep forgetting this again and again. After installing MongoDB with Homebrew:

  • The databases are stored in the /usr/local/var/mongodb/ directory
  • The mongod.conf file is here: /usr/local/etc/mongod.conf
  • The mongo logs can be found at /usr/local/var/log/mongodb/
  • The mongo binaries are here: /usr/local/Cellar/mongodb/[version]/bin
3
  • 2
    When i check /usr/local/var/mongodb/, the directory is empty. And no, i haven't changed mongoldb's default dbpath. Can you help me in finding the database file? Commented Jul 29, 2017 at 9:13
  • Add a symbolic link for it: sudo ln -s /usr/local/var/mongodb /data/db
    – TheJeff
    Commented Sep 13, 2017 at 20:07
  • I don't seem to have a folder etc in /usr/local/. Where are the config files located or how can I find out lol?
    – Reinier68
    Commented Oct 13, 2020 at 20:00
102

The default data directory for MongoDB is /data/db.

This can be overridden by a dbpath option specified on the command line or in a configuration file.

If you install MongoDB via a package manager such as Homebrew or MacPorts these installs typically create a default data directory other than /data/db and set the dbpath in a configuration file.

If a dbpath was provided to mongod on startup you can check the value in the mongo shell:

db.serverCmdLineOpts()

You would see a value like:

"parsed" : {
    "dbpath" : "/usr/local/data"
},
8
  • 1
    It is also worth noting that your mongod server does not have to run as the root user. You do need to ensure that your data directory permissions include read/write access for the user account the mongod server is using.
    – Stennie
    Commented Dec 11, 2012 at 20:41
  • 1
    When I input getCommandLineOpts(), I get something like `ReferenceError: getCommandLineOpts is not defined (shell):1´. Any help??
    – Spaniard89
    Commented Dec 12, 2012 at 9:45
  • 4
    What version of MongoDB are you using? You should be able to check that with db.serverStatus().version. I believe the shell helper for getCommandLineOpts() was added in MongoDB 2.0. You could also try: db.getSiblingDB("admin").runCommand({getCmdLineOpts:1}).
    – Stennie
    Commented Dec 12, 2012 at 9:57
  • I use version 2.2.2, when i tried db.getSiblingDB("admin").runCommand({getCmdLineOpts:1}) I got something like { "argv" : [ "./bin/mongod" ], "parsed" : { }, "ok" : 1 } Does that mean, my database directory is not set??
    – Spaniard89
    Commented Dec 12, 2012 at 10:05
  • I got it mate, I created a folder called data in the root and a subfolder db. Now it's showing the path. Thanks a lot for the help.
    – Spaniard89
    Commented Dec 12, 2012 at 10:22
30

I had the same problem, with version 3.4.2

to run it (if you installed it with homebrew) run the process like this:

$ mongod --dbpath /usr/local/var/mongodb
1
  • This solution solved my issue after upgrading to v4.2.5 via Homebrew. Commented Apr 10, 2020 at 0:09
10

I have just installed mongodb 3.4 with homebrew.(brew install mongodb) It looks for /data/db by default.

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

Here is the output of mongod execution.

2
  • 9
    This is if you start mongod directly from the terminal. If you use brew services start mongodb, the process will use the config file at /usr/local/etc/mongod.conf which, in turn, specifies a dbPath of /usr/local/var/mongodb
    – Molomby
    Commented Apr 27, 2017 at 5:23
  • Thanks @Molomby for the contribution. I wasn't started that way, I didn't even know that way. :) Commented Apr 27, 2017 at 6:33
1

Env: macOS Mojave 10.14.4

Install: homebrew

Location:/usr/local/Cellar/mongodb/4.0.3_1

Note :If update version by brew upgrade mongo,the folder 4.0.4_1 will be removed and replace with the new version folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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