CAll Us: (+20) 01022593271 Submit Ticket   Login
How To Install MongoDB on Red Hat or CentOS

How To Install MongoDB on Red Hat or CentOS


MongoDB is a document-oriented database that is free and open-source. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure. Instead, it uses JSON-like documents with dynamic schemas. Unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to setup a new database with an updated schema.

This tutorial guides you through installing MongoDB Community Edition on a CentOS 7 server.

Step 1 – Adding the MongoDB Repository

The mongodb-org package does not exist within the default repositories for CentOS. However, MongoDB maintains a dedicated repository. Let’s add it to our server.

With the vi editor, create a .repo file for yum, the package management utility for CentOS:

sudo vi /etc/yum.repos.d/mongodb-org.repo


Then, visit the Install on Red Hat section of MongoDB’s documentation and add the repository information for the latest stable release to the file:/etc/yum.repos.d/mongodb-org.repo

name=MongoDB Repository


Save and close the file.

Before we move on, we should verify that the MongoDB repository exists within the yum utility. The repolist command displays a list of enabled repositories:

yum repolist

With the MongoDB Repository in place, let’s proceed with the installation.

Step 2 – Installing MongoDB

We can install the mongodb-org package from the third-party repository using the yum utility.

sudo yum install mongodb-org


There are two Is this ok [y/N]: prompts. The first one permits the installation of the MongoDB packages and the second one imports a GPG key. The publisher of MongoDB signs their software and yum uses a key to confirm the integrity of the downloaded packages. At each prompt, type Y and then press the ENTER key.

Next, start the MongoDB service with the systemctl utility:

sudo systemctl start mongod


Although we will not use them in this tutorial, you can also change the state of the MongoDB service with the reload and stop commands.

The reload command requests that the mongod process reads the configuration file, /etc/mongod.conf, and applies any changes without requiring a restart.

sudo systemctl reload mongod


The stop command halts all running mongod processes.

sudo systemctl stop mongod


The systemctl utility did not provide a result after executing the start command, but we can check that the service started by viewing the end of the mongod.log file with the tail command:

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


Output. . .
[initandlisten] waiting for connections on port 27017

An output of waiting for a connection confirms that MongoDB has started successfully and we can access the database server with the MongoDB Shell:

About the Author