How to install MongoDB on macOS using Homebrew

In an earlier article, I wrote about how to install the MongoDB community edition on Ubuntu operating system. Today, you'll learn to install the MongoDB community edition on macOS Catalina and higher.

Creating Data Folder

Before you install and use MongoDB, you must create a data/db folder on your computer for storing MongoDB data.

Before macOS Catalina, you can create this folder in the user's root directory with the following command:

$ sudo mkdir -p /data/db

# adjust permission
$ sudo chown -R `id -un` /data/db

If you are on macOS Catalina or Big Sur (or any future release), you can not use the root folder for this purpose. macOS Catalina runs in a read-only system volume, separate from other files on the system.

Apple created a secondary volume on Catalina that you need to use for storing the MongoDB data folder:

$ sudo mkdir -p /System/Volumes/Data/data/db

# adjust permission
$ sudo chown -R `id -un` /System/Volumes/Data/data/db

Installing MongoDB

You can install the MongoDB community edition with Homebrew. If Homebrew is not already installed, execute the following command first:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now update Homebrew to the latest version:

$ brew update

Next, tap the MongoDB formulae into Homebrew:

$ brew tap mongodb/brew

Finally, execute the following command to install the MongoDB community edition:

$ brew install mongodb-community

That's it. MongoDB is now installed on your macOS computer.

Managing MongoDB Service

Use the brew services command to manage MongoDB service on your macOS computer.

First of all, install brew services by tapping homebrew/services:

$ brew tap homebrew/services

To start the MongoDB service, you use the following command:

$ brew services start mongodb-community

The above command will start MongoDB as a background service. Here's what you will see on the terminal:

==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Note: You can also use the run command instead of start. The start command will configure MongoDB to automatically start when you log into your Macbook. If you do not want to run MongoDB all the time, use the run command instead.

To check the current status of the MongoDB service, issue the following command:

$ brew services list

The above command will output something like the below:

Name              Status  User       Plist
mongodb-community started forestudio /Users/forestudio/Library/LaunchAgents/homebrew.mxcl.mongodb-community.plist
redis             started forestudio /Users/forestudio/Library/LaunchAgents/homebrew.mxcl.redis.plist

You can stop the service anytime by typing:

$ brew services stop mongodb-community

If you want to restart the service, use the following command:

$ brew services restart mongodb-community

Creating Aliases

You can also create aliases for managing the MongoDB service. Just add the following to ~/.zshrc:

alias mongod-start="brew services run mongodb-community"
alias mongod-status="brew services list"
alias mongod-stop="brew services stop mongodb-community"

Next, source the ~/.zshrc file to load aliases:

$ source ~/.zshrc

Now you can use aliases to manage your MongoDB service:

# Start MongoDB
$ mongod-start

# Check status
$ mongod-status

# Stop MongoDB
$ mongod-stop

Conclusion

That's all for this article. We learned how to install the MongoDB community edition on macOS Catalina and higher.

We use the Homebrew package manager to install MongoDB binaries and manage MongoDB service.

If you enjoy reading this article, don't forget to share it with your friends and followers.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.