How to Install MongoDB on Mac: Step-by-Step Guide
To install
MongoDB on a Mac, use the Homebrew package manager by running brew tap mongodb/brew and then brew install mongodb-community. After installation, start MongoDB with brew services start mongodb-community to run it as a background service.Syntax
These are the main commands to install and start MongoDB on Mac using Homebrew:
brew tap mongodb/brew: Adds the official MongoDB Homebrew repository.brew install mongodb-community: Installs the latest MongoDB community edition.brew services start mongodb-community: Starts MongoDB as a background service.brew services stop mongodb-community: Stops the MongoDB service.
bash
brew tap mongodb/brew brew install mongodb-community brew services start mongodb-community
Example
This example shows how to install MongoDB, start the service, and verify it is running on your Mac.
bash
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-community
mongosh --eval 'db.runCommand({ connectionStatus: 1 })'Output
{ "ok" : 1, "operationTime" : Timestamp(1680000000, 1), "$clusterTime" : { "clusterTime" : Timestamp(1680000000, 1), "signature" : { "hash" : BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : 0 } }, "authInfo" : { "authenticatedUsers" : [ { "user" : "your-username", "db" : "admin" } ], "authenticatedUserRoles" : [ { "role" : "root", "db" : "admin" } ] } }
Common Pitfalls
Common mistakes when installing MongoDB on Mac include:
- Not running
brew tap mongodb/brewbefore installation, which causes the install command to fail. - Forgetting to start the MongoDB service after installation, so MongoDB does not run.
- Using outdated Homebrew or macOS versions that may cause compatibility issues.
- Not checking if MongoDB is running with
brew services listorps aux | grep mongod.
bash
Wrong: brew install mongodb-community Right: brew tap mongodb/brew brew install mongodb-community brew services start mongodb-community
Quick Reference
| Command | Description |
|---|---|
| brew tap mongodb/brew | Add MongoDB Homebrew repository |
| brew install mongodb-community | Install MongoDB community edition |
| brew services start mongodb-community | Start MongoDB as a background service |
| brew services stop mongodb-community | Stop MongoDB service |
| mongosh | Open MongoDB shell to interact with the database |
Key Takeaways
Use Homebrew to install MongoDB easily on Mac.
Always run 'brew tap mongodb/brew' before installing MongoDB.
Start MongoDB with 'brew services start mongodb-community' to run it in the background.
Verify MongoDB is running using 'mongosh' shell or 'brew services list'.
Keep Homebrew and macOS updated to avoid compatibility issues.