Install MongoDB Community Edition on macOS
Using the third-party Homebrew package manager, install MongoDB 5.0 Community Edition on macOS in this tutorial.
Installing MongoDB using Homebrew now includes the MongoDB Database Tools as of MongoDB 4.4.1. For further information, see Using the MongoDB Database Tools.
MongoDB Version
MongoDB 5.0 Community Edition is installed in this tutorial. Use the version drop-down option in the upper-left corner of this page to select the documentation for a different version of MongoDB Community.
Requirements
Confirm that your system satisfies all of the requirements listed below. Each prerequisite step on your system only has to be completed once. You can skip ahead to the installation method if you had completed the needed steps as part of a previous MongoDB installation with Homebrew.
Apple’s Xcode command-line tools are required for Homebrew to run.
Run the following command in your Mac’s Terminal to install the Xcode command-line tools:
xcode-select --install
By default, the Homebrew brew package is not installed with Homebrew macOS.
Use the standard Homebrew installation instructions to get brew up and running.
Install MongoDB Community Edition with Homebrew’s brew package management by following these steps. Before continuing, make sure you’ve completed the installation prerequisites listed above.
Select MongoDB Homebrew from the drop-down menu. Run the following command in your macOS Terminal to obtain the official Homebrew recipe for MongoDB and the Database Tools:
brew tap mongodb/brew
You can skip this step if you’ve previously done it for a previous MongoDB installation.
Run the following command in your macOS Terminal application to install MongoDB:
brew install mongodb-community@5.0
Run MongoDB Community Edition
To use MongoDB Community Edition, follow these steps. These instructions presume you’re working with the default options.
You can use brew to start MongoDB as a macOS service, or you can manually run MongoDB as a background process. MongoDB should be run as a macOS service, as this automatically sets the necessary system ulimit values (see ulimit settings for more information).
Run the following command to start MongoDB (the mongod process) as a macOS service:
brew services start mongodb-community@5.0
Use the following command to stop a mongod running as a macOS service as needed:
brew services stop mongodb-community@5.0
Run the following command to manually start MongoDB (i.e. the mongod process) as a background process:
- For Intel processors running on Mac OS X:
mongod --config /usr/local/etc/mongod.conf --fork
- For Apple M1 processors running macOS:
mongod --config /opt/homebrew/etc/mongod.conf --fork
Connect to the mongod using mongosh and issue the shutdown command as needed to stop a mongod running in the background.
The mongod.conf file created during the installation is used in both methods. This file also allows you to add your own MongoDB setup parameters.
To verify that MongoDB is running, perform one of the following:
- If you started MongoDB as a macOS service:
brew services list
You should see the service mongodb-community listed as started.
If you started MongoDB manually as a background process:
ps aux | grep -v grep | grep mongod
- You should see your
mongod
process in the output.
You can also view the log file to see the current status of your mongod
process: /usr/local/var/log/mongodb/mongo.log
.
[…] Install on macOS […]