How to Install MongoDB

While there are several different types of database software available, MongoDB is a popular and feature-rich alternative to other software such as MySQL. As this software is not frequently pre-installed on Linux-based hosts, you may find yourself needing to install MongoDB on your CentOS or Ubuntu server. In this guide, we will outline how to install MongoDB on your CentOS and Ubuntu machines. Please note that you will need root SSH access on your server to perform the actions in the following guide.

Topics Include:

Installing MongoDB on CentOS

Installing MongoDB on Ubuntu

Install MongoDB on your Dedicated Server and start hosting your databases today!

Installing MongoDB on CentOS

Adding MongoDB Repo

  1. By default, the MongoDB software repositories (repos) are not accessible to CentOS machines. To fix this, you will need to create a repo file that will allow your server to access the MongoDB repositories. Using the nano text editor command, you can create a new .repo file as follows:

    sudo nano /etc/yum.repos.d/mongodb-org.repo
  2. Next, go to the MongoDB website and find the version of the MongoDB server software for your particular version of CentOS as well as the gpgkey URL. Once these URLs are located, copy the link to the file as such:

    [mongodb-org-3.4]name=MongoDB Repository
    baseurl=URL_FROM_MONGODB
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc


    *Please make sure to replace URL_FROM_MONGODB with the URL you copied from the MongoDB page for your chosen version of MongoDB.*
  3. Check to make sure the repo was added successfully with the following command:

    yum repolist

    If added successfully, you should see something like this in the list:

    mongodb-org-3.2/7/x86_64         MongoDB Repository

Installing MongoDB

  1. Now with the repo installed, we can start the installation process with the following command: 

    sudo yum install mongodb-org
  2. Next, start the MongoDB service with the following command:

    sudo systemctl start mongod

Testing MongoDB

  1. To test if the installation was successful, you will need to use the tail command to search the end of the MongoDB log file:

    sudo tail /var/log/mongodb/mongod.log

    If the installation was successful, you should see that the service is listening on the database port and waiting for connections.
  2. To make sure that the MongoDB utility daemon is enabled as well, use the following command:

    systemctl is-enabled mongod; echo $?

    If the returned output is 0, that means the daemon is enabled and you can begin to connect your databases to MongoDB. If the output is 1, then the daemon is not enabled and you may need to reinstall the software. If you need help with this task, we recommend contacting our Technical Support team for direct assistance.

Installing MongoDB on Ubuntu

Adding MongoDB Repo

Unlike CentOS, Ubuntu includes MongoDB package repositories by default. However, to get the most up-to-date packages, we will still want to manually add the latest version of the MongoDB repositories. To ensure the authenticity of packages, Ubuntu requires the package to be signed with a gpg key.

  1. First, we will need to add the gpg key for MongoDB using the following command:

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

    After importing the key, you should see the following output:

    gpg: Total number processed: 1
    gpg: imported: 1  (RSA: 1)
  2. Next, we will need to add the repo to the packages list using the following command:

    echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Installing MongoDB

  1. With the Repo now added, we can now install MongoDB itself:

    sudo apt-get install -y mongodb-org
  2. Next, start the service:

    sudo systemctl start mongod
  3. To check that the service started successfully, use the following command:

    sudo systemctl status mongod
  4. Finally, enable the MongoDB utility daemon

    sudo systemctl enable mongod

Congratulations, you now know how to install MongoDB!

Use MongoDB on your InMotion Hosting Dedicated Server for high-performance database storage and management!

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!