General · Linux · MAC · Mysql · NoSql

Guide to install MongoDB on MAC machine

Introduction

As per wikipedia, MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License. So In this article, I am gonna guide the ways to install MongoDB on MAC machine.

Steps to install MongoDB on MAC with Homebrew

  • Install Homebrew (Homebrew is a package manager on MAC which takes care of installing most of the open source softwares)
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • Open the Terminal app and update homebrew.
brew update
  • Find MongoDB tap.
brew tap mongodb/brew
  • Install MongoDB.
brew install mongodb-community
or
brew install mongodb
  • Before you can use MongoDB, you need to create a /data/db folder because MongoDB expects this directory (/data/db) on your machine to save the data. But Apple has already depreciated this directory on Catalina/BigSur machines and created a new volume on macOS Catalina for security purposes. So, we will create the folder in System/Volumes/Data (You can choose any folder of your choice) . Use the below command for the same.
sudo mkdir -p /System/Volumes/Data/data/db
  • Make sure that the /data/db directory has the right permissions. Then, give permissions to the folder:
sudo chown -R `id -un` /System/Volumes/Data/data/db
  • (Optional) NOTE* Make sure to link the above db path if the chosen like “/tmp/data/db” (NOT THE ABOVE ONE) by using below command
mongod --dbpath /tmp/data/db
  • Starting MongoDB

The ‘mongod’ command is used to start MongoDB but for me it doesn’t work. So, the best way to start MongoDB is now via ‘brew services’ .

brew services run mongodb-community
OR
brew services run mongodb
  • Mongo Shell

If MongoDB is running, you should be able to access the Mongo shell. Use the below command:

mongo
  • Checking if MongoDB is running
brew services list
Name                  Status  User    Plist
mongodb-community     started labuser /usr/local/opt/mongodb-community/homebrew.mxcl.mongodb-community.plist
  • Stopping MongoDB
brew services stop mongodb-community

Steps to install MongoDB on MAC by downloading it manually

  • Go to the MongoDB website’s download section and download the correct version of MongoDB.
  • After downloading Mongo move the gzipped tar file (the file with the extension .tgz that you downloaded) to the folder where you want Mongo installed.
> cd Downloads
> mv mongodb-osx-x86_64-3.0.7.tgz ~/
  • Extract MongoDB from the the downloaded archive, and change the name of the directory to something more palatable.
 cd ~/ > tar -zxvf mongodb-osx-x86_64-3.0.7.tgz > mv mongodb-osx-x86_64-3.0.7 mongodb
  • Create the directory where Mongo will store data. Please follow the steps mentioned in the 1st method of installation.
  • Run the Mongo daemon, in one terminal window run ~/mongodb/bin/mongod. This will start the Mongo server.
  • Run the Mongo shell, with the Mongo daemon running in one terminal, type ~/mongodb/bin/mongo in another terminal window. This will run the Mongo shell which is an application to access data in MongoDB.

MISC :: Aliases to make these easier

alias mongod='brew services run mongodb-community'
alias mongod-status='brew services list'
alias mongod-stop='brew services stop mongodb-community'

I hope you guys will be able to install MongoDB on your MAC machine. Happy querying 🙂