How to Install MongoDB on macOS
In this article, I am going to discuss How to Install MongoDB on macOS. Please read our previous article where we discussed How to install MongoDB on Windows Operating System.
How to install MongoDB on macOS
MongoDB is the most popular unstructured database management system. It is used to store large amounts of data. It is a document-oriented database system. It is also a NoSQL(non-SQL) database. Now, we will see how to install MongoDB on macOS. To install MongoDB on macOS use the following steps-
Step 1- Open any web browser and go to the MongoDB Download Centre to download the MongoDB community Server by using the below link.
Here you need to select the following options to download the latest version of MongoDB.
Version – 6.0.1(current)
Platform – macOS
Package – tgz
After selecting these options click on the Download button and the downloading will start.
Note: You can select the version according to your system, here I select the 4.2.2 version for my system.
Step 2- After completing the downloading you will receive a TGZ file with extension .tgz. Now extract the TGZ file in your system (at your preferable location).
In this file, the bin folder is the most important folder which contains all the necessary executables related to MongoDB.
Step 3- Setting up Command Line interface or we can say setting up mongo Shell. To run mongo Shell in the terminal we need to set the environment variable or Path, so open the .zprofile file and set the path using the following command:
Here in the above command “/User/myBook/mongodb-macos-x86_64-4.2.2/bin” is the location where the .tgx file is extracted and “:$PATH” will run the paths present in .zprofile(other than MongoDB). Now save the file and open the terminal to read the updated path using the following command:
open -e .zprofile
monogo — nodb
Note: macOS Catalina or above will not allow you to run directly mongo or mongo — nodb. So, to run mongo you need to give permission manually that is: System Preferences > Security & Privacy > Allow Anyway. Follow the same procedure to run mongod.
Step 4 – Now open the terminal and run the following command to start the mongo Shell.
Here you will see the connection is terminated or failing because we do not have a MongoDB database server (also known as mongod).
Step 5 – Setting MongoDB Database Server(mongod). So, to run mongod first we need to create a data directory that stores all the data processed by the mongod.
Step 6 – Run mongod with its data directory path using the following command:
mongod –dbpath /data/db
sudo mongod –dbpath /data/db
Step 7 – Establishing connection. Now we connect the MongoDB server(mongod) with mongo shell. Open a terminal and run mongod. Now open another terminal and run mongo. Now this time mongo shell will run successfully.
Note: Please do not close mongod terminal if you close mongod terminal then you will lose the connection. So, this is how we install MongoDB in MacOS.
In the next article, I am going to show you How to work with MongoDB Database. In this article, I try to explain How to Install MongoDB on macOS Operating System. I hope you enjoy this How to Install MongoDB on macOS article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.