Install MongoDB

Learn via video courses
Topics Covered

Overview

MongoDB is a popular open-source document-oriented database system. It is frequently used in modern web apps and is made to manage huge amounts of data. To use MongoDB for your projects, you first need to install MongoDB on your system. Here we will be discussing how to install MongoDB on various operating systems.

How to Install MongoDB?

There are two ways in which you can use MongoDB. One is using MongoDB Atlas, which is a cloud-based platform and requires no installation, and the other one is a self-managed platform.

There are two editions available for the self-managed platform, i.e., Community and Enterprise. The Community edition is free and suitable for learning and development purposes, while the Enterprise version is paid and offers many services.

How to Install MongoDB on Windows?

Let us see how to install MongoDB on Windows. You need to follow the steps mentioned below:

  1. You need to download the .msi installer from the MongoDB website.

    • Select the version of MongoDB you want to install from the Version dropdown.
    • Then, you need to choose Windows in the Platform dropdown.
    • Then you need to choose msi from the Package dropdown.
    • Then click on the Download button.
  2. Now, you need to run the installer. For that, go to the directory where the .msi file is downloaded, and double-click the .msi file to start installing MongoDB.

  3. Now, you need to select the setup type.

    • There are two types for setup, Custom type and Complete(this one is suggested for most of the users). The Custom type lets you decide the installation locations as well as which executables are to be installed. In the Complete setup type, all of the MongoDB components like MongoDB server, Compass, and shell are installed on the default locations.
  4. From MongoDB 4.0+, you have the option to select whether you want to install MongoDB as a Windows service or only if you want to install the binaries.

  5. If you want to install MongoDB as a Windows service(when MongoDB is installed as a Windows service, MongoDB starts on its own as a background process when your Windows boots up), then follow these steps:

    • Firstly, you need to choose the Install MongoDB as a Service option.

    install mongodb as a service option

    • Then you have to select between:
      • Run the service as a Network Service user, which is the default option.
        • This is a default Windows user account that comes with Windows.
      • Run the service as a local or domain user. If a local user account exists, then you need to put a period(i.e. .) in front of the Account Domain and then fill in the Account name as well as the Account password. If a domain user exists, then you need to mention the Domain Name, the Account Name, and the Account Password.
    • Then you need to fill in the Service Name where by default, MongoDB is the service name.
    • Choose the Data Directory, which is the same as --dbpath. The installer will automatically generate the directory and grant the service user access to it if it does not already exist.
    • Directory of logs. Enter the Log directory, which is the same as the --logpath. The installer will automatically generate the directory if it doesn't already exist and grant the service user access to it.
    • Now you have an optional step which is to Install MongoDB Compass by selecting Install MongoDB Compass.
    • Then you need to click install, and your MongoDB will be installed as a Windows service.
  6. If you do not want to install MongoDB as a Windows service, then unselect that option and follow the following steps:

    • Click on Next and review the configuration.
    • If you want to make any changes, you can click on Back.
    • Now you have an optional step, which is to Install MongoDB Compass by selecting Install MongoDB Compass.
    • Click on install.
  7. Now you need to wait for the installer to complete the installation process, and once it is done, you need to click on the Finish button.

  8. While installing the MongoDB Community 6.0+ you need to install mongosh manually.

  9. Add the MongoDB bin directory to your PATH environment variable.

    • MongoDB includes a set of command-line tools that you can use to interact with the database.
    • To use these tools from any directory in the command prompt, you need to add the MongoDB bin directory to your PATH environment variable.
    • To do this, open the Start menu and search for "Environment Variables".
    • Click the "Edit the system environment variables" button.
    • In the System Properties window, click the "Environment Variables" button.
    • Under "System Variables", find the "Path" variable and click the "Edit" button.
    • Click the "New" button to add a new entry to the PATH variable.
    • Enter the path to the MongoDB bin directory. By default, this is located at C:\Program Files\MongoDB\Server\{version}\bin, where {version} is the version number of MongoDB that you have installed.
    • Click "OK" to close all windows.
    • To confirm that the MongoDB bin directory has been added to your PATH variable, open a new command prompt window and type mongo. This should launch the MongoDB shell.
    • If the MongoDB shell launches successfully, you have successfully added the MongoDB bin directory to your PATH variable.

Installing MongoDB on MacOS

We will be seeing how to install MongoDB on MacOS by making use of Homebrew which is a third-party package manager.

You need to ensure that you have Xcode command line tools installed. For that, you have to execute the following command on the Terminal of your macOS:

Then you need to install the brew package.

Once you have installed the homebrew package, you need to follow the following steps to install MongoDB on macOS:

  1. Execute the below-mentioned command to download the Homebrew formula for MongoDB:

  2. Execute the following command to update Homebrew and the existing formulae:

  3. Now execute the command mentioned below in the terminal of your macOS to install MongoDB on macOS:

    Here we are installing the 6.0 version of MongoDB, you can install whichever version you want, and also you can maintain different versions in parallel.

By executing these steps, the below-mentioned binaries are included:

  • The mongod server
  • The mongos sharded cluster query router.
  • The MongoDB Shell, mongosh.

Installing MongoDB on Linux

MongoDB is available for different Linux-based OS like Ubuntu, Red Hat, Debian, etc.

Here we will be seeing how to install MongoDB on Ubuntu.

It is important to note that we have to use the mongodb-org package, which is the official package developed and supported by MongoDB Inc. Ubuntu provides a mongodb package, but MongoDB Inc. does not maintain it, and the mongodb package must be uninstalled first to avoid any inconsistencies.

Follow the steps mentioned below to install MongoDB on Linux:

  1. Execute the following command on the terminal to install gnupg in case it is not already installed

  2. Now run the following command on the terminal to import the public GPG key of MongoDB from https://pgp.mongodb.com/server-6.0.asc

  3. Now you need to create a list file /etc/apt/sources.list.d/mongodb-org-6.0.list for MongoDB and keep in mind that it should be according to the version of your Ubuntu OS

  4. Execute the following command to update the package list

  5. Now, you have to install MongoDB. Either you can install a specific version or the latest stable one.

    • To install the latest stable version

    • To install a specific version, keep in mind that you then have to mention the specific version for every component, or else only the mentioned package will have the specific version, and the rest will have the updated one.

    Now you have to remember that when you run the apt-get command, it does not matter if you have installed a particular version of MongoDB. It will get updated regardless. Now in case you do not want to automatically change the version, all you have to do is pin the packages at the version they have been installed at.

FAQs

Q: Can you install multiple versions of MongoDB on Windows?

A: On the same Windows system, various MongoDB versions can be installed. However, you must make sure that every MongoDB version has its own data folder, log file, and installation in a different directory. By doing so, conflicts between MongoDB versions will be avoided, and the autonomous operation of each version will be guaranteed.

Q: Can MongoDB be used with macOS Catalina or later versions?

A: MacOS Catalina and subsequent versions are compatible with MongoDB. However, there are a few considerations to make while setting up and using MongoDB on certain macOS versions. Security features like System Integrity Protection(SIP) and Gatekeeper are introduced, which sometimes lead to problems in running MongoDB. To tackle the issues generated by SIP you might need to disable it while running or installing MongoDB. If there is some issue due to Gatekeeper then you have to right-click in the MongoDB app and choose the Open option from the context menu.

Conclusion

In this MongoDB install article we have covered the following points:

  • MongoDB is available on a cloud platform as well as a self-managed platform.
  • The self-managed platform of MongoDB has two editions, i.e. Community and Enterprise.
  • In MongoDB 4.0+ on Windows, you can install MongoDB as a Service.
  • When MongoDB is installed as a Windows service, MongoDB starts automatically.
  • When MongoDB is installed on macOS, these binaries are included mongod server, mongos sharded cluster query router, MongoDB Shell, mongosh.