Install MongoDB Community Edition on macOS
Overview
The following tutorial usesHomebrew to install MongoDB4.2 Community Edition on macOS systems. To install a differentversion of MongoDB, please refer to that version’s documentation. For example:
Platform Support
MongoDB 4.2+ only supports macOS versions 10.12+ onIntel x86-64. See Supported Platforms for moreinformation.
Install MongoDB Community Edition
Prerequisites
If you have the Homebrew brew
package installed on your OSX hostand you have previously tapped the officialMongoDB Homebrew Tap,skip the prerequisites and go to the Install MongoDB step.
Install XCode
Apple’s XCode includes command-line tools that are requiredby brew
, and is available for free on the App Store. Make sureyou are running the latest version.
Install Homebrew
OSX does not include the Homebrew brew
package by default.Install brew
using theofficial instructions.
Tap the MongoDB Homebrew Tap
Issue the following from the terminal totap the officialMongoDB Homebrew Tap:
- brew tap mongodb/brew
Install MongoDB
From a terminal, issue the following:
- brew install mongodb-community@4.2
Tip
If you have previously installed an older version of the formula,you may encounter a ChecksumMismatchError. To resolve, seeTroubleshooting ChecksumMismatchError.
In addition to the binaries, the installcreates:
- the configuration file(
/usr/local/etc/mongod.conf
) - the
log directory path
(/usr/local/var/log/mongodb
) - the
data directory path
(/usr/local/var/mongodb
)
Run MongoDB
You can run MongoDB as a macOS service using brew
, or you can runMongoDB manually as a background process. It is recommended to runMongoDB as a macOS service, as doing so sets the correct systemulimit
values automatically (seeulimit settings for more information).
- To run MongoDB (i.e. the
mongod
process) as amacOS service, issue the following:
- brew services start mongodb-community
- To run MongoDB manually as a background process, issue thefollowing:
- mongod --config /usr/local/etc/mongod.conf --fork
Both methods use the /usr/local/etc/mongod.conf
file createdduring the install. You can add your own MongoDBconfiguration options tothis file as well.
To verify that MongoDB is running, search for mongod
in yourrunning processes:
- ps aux | grep -v grep | grep mongod
You can also view the log file to see the current status of yourmongod
process: /usr/local/var/log/mongodb/mongo.log
.
Connect and Use MongoDB
To begin using MongoDB, connect a mongo
shell to therunning instance. From a new terminal, issue the following:
- mongo
For information on CRUD (Create,Read,Update,Delete) operations, see:
Additional Considerations
Troubleshooting ChecksumMismatchError
If you have previously installed an older version of the formula,you may encounter a ChecksumMismatchError, as in the followingexample:
- Error: An exception occurred within a child process:
- ChecksumMismatchError: SHA256 mismatch
- Expected: c7214ee7bda3cf9566e8776a8978706d9827c1b09017e17b66a5a4e0c0731e1f
- Actual: 6aa2e0c348e8abeec7931dced1f85d4bb161ef209c6af317fe530ea11bbac8f0
- Archive: /Users/kay/Library/Caches/Homebrew/downloads/a6696157a9852f392ec6323b4bb697b86312f0c345d390111bd51bb1cbd7e219--mongodb-macos-x86_64-4.2.0.tgz
- To retry an incomplete download, remove the file above.
To fix:
Remove the downloaded
.tgz
archive.Retap the formula.
- brew untap mongodb/brew && brew tap mongodb/brew
- Retry the install.
- brew install mongodb-community@4.2
Localhost Binding
Starting with MongoDB 3.6, MongoDB binaries, mongod
and mongos
, bind tolocalhost
by default.
When bound only to the localhost, these binaries can only acceptconnections from clients that are running on the same machine. Remoteclients cannot connect to the binaries bound only to localhost. Formore information, including how to override and bind to other ipaddresses, see Localhost Binding Compatibility Changes.