Steps to Install MongoDB on Mac Using Homebrew

What is MongoDB MongoDB is an open-source document database that belongs to a family of databases called NoSQL – not only SQL...

Written by Vikas Soni · 1 min read >

What is MongoDB

MongoDB is an open-source document database that belongs to a family of databases called NoSQL – not only SQL and provides high performance, high availability, and automatic scaling. Its document-oriented data model makes it easier to split up data across multiple servers.

In MongoDB, records are documents that behave a lot like JSON objects in JavaScript but use a variant called Binary JSON (BSON) that accommodates more data types. The document is a data structure composed of field and value pairs. Values in documents can be looked up by their field’s key and can be easily added/updated anytime which makes Mongo extremely flexible.

This is different than SQL databases, Relational databases, like PostgreSQL, MySQL and, where fields correspond to columns in a table and individual records correspond to rows.

How to install MongoDB on MAC OS X

Install and update Homebrew – a package manager

  • Open a new terminal and check if Homebrew is already installed on your computer
  • Homebrew is a package manager which is used to install other software/packages on Mac It is like apt-get in Linux OS
  • Run command in your terminal to check if Homebrew is already installed: brew -v
  • If you are not able to install it, try Homebrew Installation by running the command
  • /usr/bin/ruby -e "$(curl -fsSL"
  • Brew update and check the updated version using brew -v
brew -v

//If not installed
sudo /usr/bin/ruby -e "$(curl -fsSL"

//update latest version
brew -v

Install and Run MongoDB with Homebrew

  • After updating Homebrew install MongoDB by running the command: brew install mongodb
  • Now create a Mongo data directory, This directory is where MongoDB stores database files, using the command:
  • sudo mkdir -p /data/db
  • Make sure that the /data/db directory has the right permissions, run the chown command to give the right access:
  • sudo chown -R `id -un` /data/db
brew install mongodb
sudo mkdir -p /data/db
sudo chown -R `id -un` /data/db

Run MongoD and Mongo

  • To Start a Mongo process first run the Mongo daemon, in one of your terminal windows run mongod. This should start the Mongo server.
  • Now Run the Mongo shell, with the Mongo daemon running in one terminal, type mongo in another terminal window. This will run the Mongo shell which is an application to access data in MongoDB.
  • Great !! MongoDB is up and running now
  • To stop type ctrl+c in mongo daemon terminal
mongod //in one of your terminal
mongo //in another terminal

Set Mongo as a service on Mac

  • Most importantly to run mongo as a service first stop mongo and mongod by using ctrl+c
  • To run Mongo daemon as a service run command: brew services start mongodb
  • After successfully run the command, Now restart the computer to set mongo as a service
brew services start mongodb //to run mongo as a service on mac

I am sure this article helps you to install MongoDB on Mac OS using Homebrew. Step by Step points will help you to install and understand the process within minutes. I would like to hear your thoughts. Happy learning…

One Reply to “Steps to Install MongoDB on Mac Using Homebrew”

Leave a Reply

Your email address will not be published. Required fields are marked *