113

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.

Spaniard89
  • 2,359
  • 4
  • 34
  • 55

6 Answers6

252

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.

Mark Edington
  • 6,484
  • 3
  • 33
  • 33
  • I was looking for some light in the darkness by checking the formula at https://github.com/Homebrew/homebrew/blob/master/Library/Formula/mongodb.rb thanks. – alexserver Mar 12 '14 at 17:42
  • 5
    In newer versions of Homebrew, it's `/opt/homebrew/var/mongodb`. – JW. Jan 15 '22 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. – Mark Edington Jan 16 '22 at 23:43
  • Thanks~ I just need the m1 version~ – Ali Nov 04 '22 at 07:51
164

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
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Simon de Lorean
  • 2,045
  • 1
  • 13
  • 9
101

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"
},
Stennie
  • 63,885
  • 14
  • 149
  • 175
  • 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 Dec 11 '12 at 20:41
  • 1
    When I input getCommandLineOpts(), I get something like `ReferenceError: getCommandLineOpts is not defined (shell):1´. Any help?? – Spaniard89 Dec 12 '12 at 09: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 Dec 12 '12 at 09: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 Dec 12 '12 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 Dec 12 '12 at 10:22
  • Yes, if you don't see an entry for `dbpath` under the `parsed` config options the default path of `/data/db` is being used. – Stennie Dec 12 '12 at 11:54
  • @Stennie so your suggestion worked. `db.getSiblingDB("admin").runCommand({getCmdLineOpts:1})` Anyone care to edit this answer to make it more visible? – kgpdeveloper Feb 15 '14 at 19:23
  • @kgpdeveloper: updated with even shorter syntax: db.adminCommand("getCmdLineOpts") – Stennie Feb 15 '14 at 20:06
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
Runnerdave
  • 455
  • 1
  • 5
  • 9
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.

csonuryilmaz
  • 1,715
  • 24
  • 24
  • 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 Apr 27 '17 at 05:23
  • Thanks @Molomby for the contribution. I wasn't started that way, I didn't even know that way. :) – csonuryilmaz Apr 27 '17 at 06: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

jyjin
  • 95
  • 7