web-development top banner

How to Install MongoDB on a Mac

Read it in 8 Mins

Last updated on
13rd Apr, 2022
Published
06th Jul, 2021
Views
9,078
How to Install MongoDB on a Mac

MongoDB is one of the most popular unstructured database management systems that can store a high volume of data. It is a document-oriented database system that belongs to the family of NoSQL (non-SQL). Here the data and records are stored as documents that behave more like JSON objects. Documents are a combination of key-value pairs that form the basic unit of data in MongoDB. This database system came into action in mid-2000.

What is NoSQL and why should we use NoSQL?

NoSQL stands for Not Only SQL or non-SQL and is an unstructured database that helps store and retrieve data. In the year 1998, Carl Strozz introduced NoSQL. It models the data by means other than the tabular relations. It means such databases do not have a fixed schema, but are intended explicitly for the distributed data that demands humongous data storage. We use NoSQL databases for real-time web apps, mobile apps, big data, etc. Websites like Google, Twitter, Amazon, Facebook, Instagram, etc., collect terabytes of data every day.

Earlier, web applications were simple and did not generate such huge amounts of data. But with the advent of big companies like Facebook, Google, Amazon, etc., huge volumes of data are generated, because of which NoSQL databases have become popular. Traditional RDBMS (like SQL) uses simple queries to store and retrieve textual data. But NoSQL database management systems embrace a wide range of file systems storing structured, unstructured, semi-structured, and polymorphic data.

Features of NoSQL

  • NoSQL databases do not follow the relational model. 
  • They are schema-free, or they do not follow any specific schema. 
  • NoSQL renders heterogeneous data structures (graph, tree, column family, key-value pair, document, etc.) on the same domain. 
  • Data is not stored flat in rows and columns (table). 
  • NoSQL does not demand data normalization and object-relational mapping. 
  • NoSQL does not demand setting up complex concepts like joins, referential integrity, ACID properties, etc. 

Who should use MongoDB?

Developers who want to deal with structured, semi-structured, or unstructured data need to use MongoDB for their applications. Those who are into Big data analysis can also use MongoDB. Again, if an application's data needs agility, scaling, and high performance, MongoDB is the best solution.  

It supports a broad spectrum of use cases, from real-time exploratory and predictive analytics to parallel data processing. MongoDB can provide high-performance data storage even when spread across multiple servers.

Prerequisites

Software Requirement:

  • macOS 10.13 or later 
  • MongoDB 4.4 Community Edition (we will show the download procedure later) 
  • Install Xcode Command-Line Tools: Homebrew demands to install the Xcode command-line tool from Apple's Xcode before using it. To install Xcode, you have to run the following command in your macOS Terminal: 
  •  xcode-select --install 
  • Homebrew package manager: By default, macOS does not incorporate the Homebrew package. You can install Homebrew using the documentation given on their official website (https://brew.sh/#install). 

 Hardware Requirement:

  • Intel Processor / Apple M1 Processor 
  • 4 GB RAM preferred 

Installation Steps

Install Manually without Brew

Step 1: Let us now download MongoDB. For this, open your web browser and type: google.com

Step 2: From Google search, type: MongoDB and hopefully, the first link the search throws up would be the MongoDB link. From here, we have two ways of installing MongoDB. Follow these steps to install using the macOS terminal. 

How to Install MongoDB on a Mac

Step 3: Go to mongodb-community Select the version, platform, and package. Make sure you choose macOS as the platform and 'tgz' as the file format and click the download button.

How to Install MongoDB on a Mac

Step 4: Once the tgz file gets downloaded, go to the macOS terminal to extract it.
Step 5: Mostly, your MongoDB will get downloaded in the Downloads folder. For this, type the following command in the terminal:

cd Downloads/ 
ls 
tar xzf mongodb-osx-ssl-x86_64-4.4.tgz 


Step 6: Now, we have to move the MongoDB folder to our local binary storage. 

sudo mv mongodb-osx-ssl-x86_64-4.4 /usr/local/mongodb

  • This will ask for your system password. Provide the password. 
  • You can change the directory to /usr/local/mongodb and see whether all the files exist or not using the ls command. Note that this step is optional. To change the directory, type the command 
cd /usr/local/mongodb 

How to Install MongoDB on a Mac

  • Next, you have to create the db folder. By default, MongoDB writes or stores the data in the folder called data/db. The command for this will be 

sudo mkdir -p /data/db 

  • The -p flag will allow us to create the directory structure. Now, to check whether this path and directory have been created or not, we use the command: 

cd /data/db

  • To check whether we are on the right directory or not, just type the command: pwd

 

  • For changing the permission, you need to know your username first. To know your username, type the command: whoami

Now change the permission of this directory. To do this, the command is: sudo chown <user-name> /data/db 

  • Finally, you are eligible to directly run the mongo process.  

Install using Brew –

If you want to install MongoDB through Homebrew manually, follow these steps – 

Step 1: Homebrew helps in installing and managing applications on MacOS. If you haven't downloaded or installed Homebrew, then click the link (https://github.com/mongodb/homebrew-brew) to download the official Homebrew formula for MongoDB, by running the command in your macOS Terminal:  

brew update 
brew tap mongodb/brew 

Step 2: Once the Homebrew package resides in your system, you can download MongoDB using brew. 

Step 3: Type the following command in your macOS Terminal: 

brew install mongodb-community@version-number

Step 4: This installation will add the following binaries: 

  • The mongod server 
  • The mongo shell 
  • The mongos sharded cluster query router 

Step 5: The installation will take a few seconds. Once done, you can create a directory to store MongoDB data using the following command. 

sudo mkdir -p /data/db 

Step 6: Now, you have to note that your data directory should have the appropriate permissions. To do this, execute the command: 

sudo chown -R `id -un` /data/db 

Step 7: This will ensure that the data directory is ready and has all the proper permissions. 

Step 8: Apart from that, the MongoDB installation will produce the following files and directories at the locations given below – 


Intel Processors Apple M1  
Log directory/usr/local/var/log/mongodb/opt/homebrew/var/log/mongodb
Configuration file/usr/local/etc/mongod.conf/opt/homebrew/etc/mongod.conf
Data directory/usr/local/var/mongodb/opt/homebrew/var/mongodb

Step 9: Let us now run the MongoDB community Edition. You can use the brew command to run MongoDB as a macOS. A manual procedure is needed to run MongoDB services on macOS. To execute MongoDB daemon, which resides by the name mongod (process), use the following command: 

brew services start mongodb-community 
macOS will run this process as a macOS service. 

Step 10: For stopping a mongod process running as a macOS service, apply the following command: 

brew services stop mongodb-community

Step 11: For running MongoDB in the background manually and listening for connections on a given port, use the following command - 

For Mac systems with Intel processors: mongod --config /usr/local/etc/mongod.conf --fork 
For Mac systems with Apple M1 processors: mongod --config /opt/homebrew/etc/mongod.conf –fork 

Step 12: Next, verify your MongoDB version. To do this, type the following command: mongo –version 

Step 13: The command line will display the installed version of MongoDB on your Mac system. Developers recommend using the newest version of libraries and software whenever feasible. It will keep you away from any compatibility issues with client-side applications. 

Step14: You can view the installation list by typing the command: mongodb 

Step15: Use the command mongod --config /usr/local/etc/mongod.conf to start the MongoDB 

Step 16: To connect to mongodb service, type the command: mongo 

Step17: Use the ‘show dbs command to see all databases. 

You can learn more about the working of MongoDB and become an expert NoSQL database administrator by joining the course mongodb-administrator. This course covers features of MongoDB 4.0 and future releases. 

Uninstall MongoDB on macOS X –

Uninstalling MongoDB from your system will entirely remove MongoDB along with its associated files. 

  • Before uninstalling MongoDB, check whether any mongo service is running by using the command: launchctl list | grep mongo 

  • If any running process exists before uninstallation, you should stop or kill it. To kill all the processes related to mongod, use the command:  pkill -f mongod 

  • The command to uninstall MongoDB from your system is: 
    • If installed via brew: brew uninstall mongodb-community

  • Or, if installed manually you can simply delete the folder: rm -rf <mongodb_folder> 
  • If you have a separate folder for the database, use the command to remove that database directory: rm -rf /data/db 

MongoDB is the leading NoSQL, document-based, open-source database system. It is a cross-platform system - licensed under the Server-Side Public License (SSPL). Due to its broad spectrum of features and benefits, it became popular very quickly. Hopefully, this article has helped you understand the basics of installing MongoDB in your Apple system.  

In this article, we have walked you through the two ways to install MongoDB in a macOS. Also, this article explicitly talked about installing MongoDB in Apple systems with Intel processors and with Apple M1 processors. So, you can navigate this article as per your system. You can learn more about MongoDB installation and join the course from 
mongodb-administrator.

Profile

Gaurav Kr. Roy

Author

Mr. Gaurav is a cybersecurity engineer, developer, researcher, and Book-Author who did his B.S.-Cybersecurity from EC-Council University & Masters from LPU. He is an India Book of Record holder, Guest speaker with 7+ years of experience in IT.