How To Install MongoDB 5 on Mac

By bhagwatchouhan
How To Install MongoDB 5 on Mac

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents. MongoDB is a document-oriented, open-source, and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to install and set up the MongoDB 5 Community Server, MongoDB Compass, and MongoDB Shell on macOS Catalina. The steps should be the same for other versions of macOS operating systems including Big Sur and Monterey.

Notes: MongoDB Community Edition supports macOS Mojave or later. This tutorial does not cover the steps to install and manage MongoDB using Homebrew. You may also follow How To Install MongoDB 5 on Ubuntu 20.04 LTS and How To Install MongoDB 5 on Windows.

 

Download MongoDB

We can download MongoDB Community Edition for macOS from the Official Website. It provides the options to download MongoDB as shown in Fig 1

Install MongoDB, Compass, Shell on macOS Catalina - Download Options

Fig 1

The default download location on macOS is ~/Downloads. The downloaded file name should be similar to mongodb-macos-x86_64-5.0.3.tgz.

 

Install MongoDB

In this step, we will install MongoDB to our preferred directory. We simply need to extract the tgz file downloaded in the previous step. In my case, I have extracted it to /usr/local/mongodb using the commands as shown below.

# CD to Downloads
cd ~/Downloads
# Extract tar xzf mongodb-macos-x86_64-5.0.3.tgz
# Install sudo mv mongodb-macos-x86_64-5.0.3 /usr/local/mongodb

You can also refer to Fig 2 for the commands used by me to install MongoDB.

Install MongoDB, Compass, Shell on macOS Catalina - Install

Fig 2

Before starting MongoDB, we must also create the data directory of MongoDB to store the database files using the commands as shown below.

# Create Directory
sudo mkdir -p /System/Volumes/Data/data/db
# Change Permissions sudo chown -R `id -un` /System/Volumes/Data/data/db

Make sure to use ` instead of ', else you will get the error chown: id -un: illegal user name.

Now run the MongoDB process using the commands as shown below.

# CD to MongoDB Installation
cd /usr/local/mongodb/bin
# Run MongoDB Daemon ./mongod

It will ask for system permissions as shown in Fig 3.

Install MongoDB, Compass, Shell on macOS Catalina - Permissions

Fig 3

Now click the Option - Open general pane for me as highlighted in Fig 3. It will open General Pane as shown in Fig 4.

Install MongoDB, Compass, Shell on macOS Catalina - General Pane

Fig 4

Click the option Allow Anyway to allow mongod to run. While giving the permissions, it might get killed on the first run. We can again execute the mongod command to start the MongoDB daemon as shown in Fig 5. It will also fail, complaining about the data directory as shown in Fig 5.

Install MongoDB, Compass, Shell on macOS Catalina - Data Directory Error

Fig 5

Now run mongod by providing the data directory option as shown below.

# CD to MongoDB Installation
cd /usr/local/mongodb/bin
# Run MongoDB Daemon ./mongod --dbpath /System/Volumes/Data/data/db

This is all about installing and starting the MongoDB daemon. We can kill the process using Ctrl + C. Now create the shortcut to run MongoDB from the terminal as shown below.

# Create Alias
alias mongod="/usr/local/mongodb/bin/mongod --dbpath /System/Volumes/Data/data/db"
# Start MongoDB mongod

Now keep running the MongoDB server, open another terminal and follow the same steps to run the Mongo Shell by giving it appropriate permissions. Also, create the alias of Mongo Shell as shown below.

# Create Alias
alias mongo="/usr/local/mongodb/bin/mongo"
# Start Shell
mongo

The shell shows deprecation warnings as shown in Fig 6 since, from MongoDB 5 onwards, MongoDB Shell or mongosh is the preferred way to access MongoDB from the terminal.

Install MongoDB, Compass, Shell on macOS Catalina - Shell

Fig 6

The terminal does not remember the aliases created by us on closing and re-opening it. We can add the aliases to ~/.zshrc to persist them.

 

Install MongoDB Shell

In the previous step, we have installed the MongoDB database server by downloading it from the official website and also configured the alias to start the server. In this step, we will install MongoDB Shell to access the database and perform operations from the terminal. Now download MongoDB Shell from the Official Website. It provides the download options as shown in Fig 7.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Shell - Download Options

Fig 7

Now install mongosh by following the same steps as we did for MongoDB using the commands shown below.

# CD to Downloads
cd ~/Downloads
# Extract sudo unzip mongosh-1.1.1-darwin-x64.zip
# Install sudo mv mongosh-1.1.1-darwin-x64 /usr/local/mongosh

Also, add an alias by updating the ~/.zshrc as shown below.

# Update ~/.zshrc
sudo nano ~/.zshrc
# Add Alias alias mongod="/usr/local/mongodb/bin/mongod --dbpath /System/Volumes/Data/data/db" alias mongo="/usr/local/mongodb/bin/mongo" alias mongosh="/usr/local/mongosh/bin/mongosh"

Save and exit the editor using the commands Ctrl + o -> Enter and Ctrl + x.

 

Enable Authentication

In this step, we will add the default admin user with root privileges and also enable Access Control to allow authenticated users. Start MongoDB server and MongoDB Shell using the aliases created by us in the previous steps.

Now list the databases as shown below.

# List Databases
show databases
# Output admin 41 kB config 12.3 kB local 73.7 kB

We can see that MongoDB has already created three databases i.e. admin, config, and local. Now we will add the user admin with root role to the admin database using the commands as shown below.

# Switch to admin database
use admin
# Create the admin user with root role db.createUser( { user: "admin", pwd: "<password>", roles: [ "root" ] } )
# Output { ok: 1 }

This is how we can add admin users to the admin database and assign a role to the user. Now stop the server and mongosh.

Next, we will create the configuration file and update mongod alias to use the configuration file.

# Create Configuration File
sudo nano /usr/local/mongodb/mongodb.conf
# Update The File storage: dbPath: "/System/Volumes/Data/data/db" security: authorization: enabled
# Save and exit the editor by pressing Ctrl + O -> Enter, Ctrl + X

Also, update the alias by updating the ~/.zshrc as shown below.

# Update ~/.zshrc
sudo nano ~/.zshrc
# Add Alias alias mongod="/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf" alias mongo="/usr/local/mongodb/bin/mongo" alias mongosh="/usr/local/mongosh/bin/mongosh" # Save and exit the editor by pressing Ctrl + O -> Enter, Ctrl + X

After enabling the authorization, we can connect to MongoDB via MongoDB Shell by executing it with options as shown below. Also, start MongoDB daemon. We have to specify the authentication database to allow database users to log in via shell.

# MongoDB Shell
mongosh --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"
Install MongoDB, Compass, Shell on macOS Catalina - Authentication

Fig 8

This is how we can enable authentication and allow only valid users to access the database.

 

Install MongoDB Compass

MongoDB Compass is the database tool provided by the MongoDB team. It's the official GUI tool to manage MongoDB databases. We can use MongoDB Compass to structure the document and also perform the database operations including querying, indexing, document validation, etc. In this step, we will install MongoDB Compass on macOS. Open the official Download Page to view the download options as shown in Fig 9.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Compass - Download Options

Fig 9

Download the appropriate version and double click the downloaded image to start installing MongoDB Compass on mac. Now double-click the downloaded image. The system will mount it and shows options to install MongoDB Compass as shown in Fig 10.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Compass - Install

Fig 10

Now drag-and-drop the MongoDB Compass Icon to the Applications icon to install it. It will complete the installation by adding the application to the Applications directory. Also, unmount the image by right-clicking and clicking the Eject Option as shown in Fig 11.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Compass - Eject

Fig 11

This completes the installation of MongoDB Compass on macOS. Now launch it by executing it from the Applications folder. Complete the getting started guide and update and accept the privacy settings. The default screen should be similar to Fig 12.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Compass

Fig 12

Now click the option Fill in connection fields individually to connect with the MongoDB using the admin user added by us in the previous steps. The connection screen should be similar to Fig 13.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Compass - Connect

Fig 13

Fill the form by providing the login credentials and clicking the Connect Button. It will connect to the MongoDB database server and shows the default screen as shown in Fig 14.

Install MongoDB, Compass, Shell on macOS Catalina - MongoDB Compass - Default

Fig 14

 

Summary

This tutorial provided all the steps required to install MongoDB 5 on macOS Catalina and later versions. It also provided the steps to install MongoDB Shell and MongoDB Compass and to connect them to the Database Server by enabling the authentication.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan