Centurion, South Africa info@code2bits.com On Request

How to run MongoDB on macOS using Homebrew

In this post, I will show you how to setup MongoDB on the macOS for the first time and start the database from the command line. The intent of this post is not to describe in detail how to perform the setup and start up process, but rather a quick reference to the commands used.


The following list defines the technologies and applications I use to run MongoDB on macOS:

  • MongoDB Installed on macOS (Link)
  • iTerm or Terminal (Link)
  • Homebrew (Link)

Run MongoDB

Before you can start MongoDB for the first time, you first need to configure MongoDB so that it knows where the data directory is. The mongod process will look for the data by default in the /data/db directory. This directory does not get created as part of the installation process, and should be created by you.

1. Configure MongoDB

The data directory should be created to allow MongoDB to write its data files to it. Use the following command:

$ sudo mkdir -p /data/db

To ensure that the user account you are currently using can stop and start MongoDB, it is important that your user has the correct permissions to access the directory. You can use the following command to change the ownership of the directory to be associated with your user you are currently logged into:

$ sudo chown $USER /data/db

2. Start MongoDB

To start MongoDB, you can simply run the following command:

$ mongod

In the event that you decided to make use of a different directory as your data directory, you need to specify the directory path to the mongod process. You can start MongoDB in the following manner by specifying a different directory:

$ mongod --dbpath "/new/db/path"

3. Stop & Start MongoDB as Background Service

It is possible to stop and start MongoDB as a background service on macOS. You can make use of Homebrew to list all running services for the current user (or root) and filter out for the name mongodb. Use the following command:

$ brew services list | grep mongodb

Homebrew can be used to start the mongodb service immediately and register it to launch at login (or boot). Use the following command:

$ brew services start mongodb

Homebrew can be used to stop the mongodb service immediately and unregister it from launching at login (or boot). Use the following command:

$ brew services stop mongodb

Note: If you make use of the Homebrew service to stop and start MongoDB, the mongod configuration is stored at /usr/local/etc/mongod.conf. The DB Path within this configuration is specified as /usr/local/var/mongodb and not the default /data/db.

4. Access MongoDB Shell

The mongo shell is an interactive interface used to query and update data as well as perform administrative operations. Note that you must first start the MongoDB database before you access the Mongo shell. You can start the mongo shell by making use of the following command:

$ mongo


Congratulations. You have now successfully configured the data directory for MongoDB. Furthermore you have also learned how to start the MongoDB database from the command line, how to make use of Homebrew to list, stop and start the MongoDB process as a background service for macOS. Then lastly you have also learned how to access the Mongo shell used to query and update data within the MongoDB database.