How to install MongoDB?

MongoDB is available for both Cloud and self managed platform.

MongoDB Atlas is the fully managed and automated cloud based database service available for AWS, Azure, and Google Cloud. Free Atlas database is available for 512mb to 5gb of storage using shared cluster.

For self manage, MongoDB offers two editions, Community and Enterprise.

We will start with various editions of mongodb and then how to install them.

The MongDB Installation might require knowledge of servers and development on some operating systems.


MongoDB Editions

MongoDB comes with two editions, Community and Enterprise. Community is the open-source version with great feature, but Enterprise comes with advance features and support.

MongoDB Community Vs Enterprise

MongoDB Community Vs Enterprise
Community Enterprise
Price Free Paid
Role based Access Controls Yes Yes
In Memory Storage Engine No Yes
Encrypted Storage Engine No Yes
Advance Support No Yes
Advance Security No Yes
Latest Features No Yes

MongoDB Compass

Also install MongoDB Compass, a free GUI for MongoDB. It can handle both in premises and cloud databases. We can check, update, delate and take backup, upload bulk data using Compass.


Install MongoDB on MacOS

MongoDB is supported in MacOS 10.11 and above versions.

MongoDB 4.2 Community Editions supports only macOS 10.12 or later. We are installing MongoDB 4.2 in MacOS 10.14 (Mojave).

Install MongoDB using Homebrew.

Install Homebrew brew package manager for MacOS. By default MacOS doesn't include brew package. Here is step by step guide to install MongoDB using brew.

  1. brew tap mongodb/brew
  2. brew install mongodb-community@4.2

Run MongoDB Community edition

Run

brew services start mongodb-community@4.2

Stop

brew services stop mongodb-community@4.2

Run MongoDb in background

mongod --config /usr/local/etc/mongod.conf --fork

Start MongoDb

mongo

Install MongoDB using .tgz Tarball

Here is the step by step guide to install MongoDB Community edition on MacOS using .tgz Tarball.

  1. Download MongoDB from official site, i.e. MongoDB 4.2 .tgz
  2. Extract files from archive. Double click on .tgz file. The extracted version would end with .tar.
  3. Copy binaries in /bin directory to usr/local/bin. Press Cmd + shift + g , and type /usr/local/bin/
  4. Create a directory to store MongoDB data.
    mkdir -p /data/db
  5. Check permissions of directory.
    sudo chown -R `id -un` /data/db
  6. Run MongoDB demon.
    ~/mongodb/bin/mongod
  7. In another terminal window, run Mongo shell.
    ~/mongodb/bin/mongo
  8. To exit mongo shell, type quit() and to stop MongoDB demon, press ctrl+c

For MacOS Catalina and above

With MacOS Catalina update, Apple secure their file system. That's the reason MongoDB will not start in MacOS Catalina and above MacOS like MacOS Big Sur etc.

To start MongoDB in Catalina, we need to change default directory path of MongoDB. Use following steps to do this.

  1. Open Terminal window.
  2. Create a new directory in user.
  3. Type sudo mongod --dbpath ~/MongoDB
  4. Type mongo to run MongoDB.
sudo mongod --dbpath ~/MongoDB
MongoDB in MacOS Catalina
Run MongoDB in MacOS Catalina

Install MongoDB on windows

Here is a step by step guide to install MongoDb on windows. You need Windows 10 / 8.1 / 8 / 7 or Windows Server 2008 R2 / 2012 / 2012 R2 / 2016 / 2019.

  1. Download MongoDB MSI Installer.
  2. Run MongoDB MSi Installer.
  3. Follow installation steps.
  4. Choose MongoDB as windows services or just install binaries.
  5. Install MongoDB Compass.
  6. Create a directory data/db in C drive, or default drive of OS.
  7. Start MongoDB.

To start mongodb server in windows os, we have to type mongod in cmd. Some times this command will not work, so we have to setup environment variables.

Setup Environments Variables

  1. Right click on My Computer and choose Properties
  2. Click Advanced System setting in left
  3. For windows 10 and 11, Goto Control panel and click System and Security and then System and then choose Advanced system setting on right menu and then choose Environment Variables at bottom.
  4. Choose Path in System variables and click edit.
  5. Copy the path of bin in new line and then press ok.
  6. Close all windows and run mongod in cmd

Install MongoDB on Linux

To install MongoDB on Linux, please check list of supported Linux Distributions.

Linux OS supporting MongoDB

  1. Red Hat
  2. Ununtu
  3. Debian
  4. SUSE
  5. AMAZON

Install MongoDB on Ubuntu

  1. Import public key using package management.
    wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

    For error gnupg is not installed, try this

    sudo apt-get install gnupg
    wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
  2. echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
  3. Reload local package database
    sudo apt-get update
  4. Install MongoDBpackage
    sudo apt-get install -y mongodb-org
  5. Run MongoDB