Array
University of Oregon

Install MongoDB (MacOS)

 

1. Install MongoDB

A) MacOS Catalina Instructions

  1. Open this web page.
  2. Follow the instructions to install Homebrew. This first installs the Command Line Tools for Xcode, which is required for Homebrew. The total install will take several minutes to complete, depending on the speed of your internet connection.
  3. Follow the instructions under, “Preparations (MacOS Catalina onwards)“.
  4. Skip the rest of the document.
  5. Proceed to Step 2 Start mongod, below, and continue.

B) Pre-Catalina Instructions

Follow these steps to install MongoDB Community Edition on macOS using the Homebrew package manager for MacOS.

System Requirements: macOS 10.12 (or higher).

    1. Start the Terminal app.
    2. Open the Homebrew home page. Copy the install command, paste it into the Terminal, and hit Enter.

      When prompted, enter the password for your computer.

      The first thing that gets installed is the Command Line Tools for Xcode, required for Homebrew.

      The total install will take several minutes to complete, depending on the speed of your internet connection.

    3. Install  MongoDB Homebrew Tap. Paste this command into the Terminal, and hit Enter:

      brew tap mongodb/brew

    4. Install Install MongoDB Community Edition for macOS. Paste this command into the Terminal, and hit Enter:

      brew install mongodb 

      Run these commands to verify the install succeeded:

      mongod --version
      mongo --version
    5. Create the MongoDB data directory.

      Before you start MongoDB for the first time, you must create the directory to which the mongod process will write data.

      The mongod process looks for a directory named /data/db directory.

      Creating this directory requires the sudo command to execute as the root user.

      In your terminal, enter this command:

      sudo mkdir -p /data/db

    6. Set permissions for the data directory (/data/db).

      Before running mongod for the first time, your user account must have read and write access for the directory /data/db.

      Because you created the data directory using sudo, the owner of the directory is the root user.

      To set the permissions for the data directory, you first change the owner from root to your user account, and then set the permissions.

      Here is the command to change the owner of the data directory. Be sure to change susanq’s user name to your user account on your computer (i.e., your user name on your computer):

      sudo chown yourUserName /data/db

      Then run this command to change the permissions of the data directory:

      sudo chmod u+rw /data/db

2. Start mongod — the MongoDB Server

To start the mongod program (the d stands for daemon, which means a Unix process):

Open a new terminal window.
To start the server, run this command:

mongod --config /usr/local/etc/mongod.conf

To stop the server, type this keyboard command: control-c

3. Start mongo — the MongoDB Shell

MongoDB includes a CLI (shell) called mongo. Once MongoDB is running, start the CLI:

Start mongod, as described above.
Open a second terminal window (or tab).
To start the shell, run this command: mongo
To stop the shell, type this keyboard command: control-c

4. Next Up: Learn Mongo

Next, learn how to use MongoDB.

For Further Information

Read, Install MongoDB Community Edition on macOS.

 


Skip to toolbar