MongoDB is a document-based NoSQL database application. Unlike MySQL, it allows data to be stored differently in different documents.

It allows for different fields in different documents, and the data structure is not permanently fixed.

In this tutorial, learn how to install MongoDB on CentOS 8.

How to install MongoDB on CentOS 8.


Installing MongoDB on CentOS 8

Step 1: Add the MongoDB Software Repository

By default, MongoDB is not available in the official CentOS repositories. To add the MongoDB repositories, open a terminal window, and create a MongoDB repository configuration file:

sudo nano /etc/yum.repos.d/mongodb-org-4.2.repo

In the newly created repo configuration file, enter the following:


name=MongoDB Repository





Save the file (Ctrl+o) and exit (Ctrl+x).

Note: At the time this article was written, MongoDB 4.2 was the latest version. Please check the MongoDB developer page for the latest version.

Step 2: Install MongoDB Software

Install MongoDB on CentOS 8 with the following command:

sudo yum install –y mongodb-org

screenshot of Installing MongoDB software on CentOS 8

Step 3: Start the MongoDB Service

Start the MongoDB service by entering the following command:

sudo systemctl start mongod

If you receive an error that the unit is not found, run the following command, then try the previous command again:

sudo systemctl daemon-reload

If you’re using MongoDB as a permanent feature, you can set it to run at boot with the following command:

sudo systemctl enable mongod

To check whether the MongoDB service is running, use the following command:

sudo systemctl status mongod

Checking the MongoDB service status is running

Set Up and Configure MongoDB

Create MongoDB Admin User

Start by opening the Mongo shell for use. Enter the following command:


The prompt should change to a simple angle bracket.


Next, switch to the admin user account:

use admin

Switch to admin user in Mongo.

Next, create an administrator user account for the Mongo database:

 user: "mdbadmin",
 pwd: "password",
 roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

The system should respond with the following:

Create an administrator user account for Mongo database.

Note:  Replace mdbadmin with an actual administrator username you want to use. Also, replace password with a unique strong, and secure password.

Next, display the list of users:

show users

The system should display details about the username just created:

Display user details.

Exit the Mongo shell by entering the following:


Remember, the alphanumeric userId will be different from this example.

Configure MongoDB Authentication

By default, any user can perform any function in MongoDB. This will require users to have proper credentials to perform actions.

Step 1: Turn on Authentication

Start by editing the following file:

sudo nano /lib/systemd/system/mongod.service

Find the following line:

Environment="OPTIONS=--f /etc/mongod.conf"

Add the --auth option as follows:

Environment="OPTIONS= --auth -f /etc/mongod.conf"

Turning on Mongo authentication

Save the file (Ctrl+o) and exit (Ctrl+x).

Step 2: Reload the Services to Apply Changes

Reload the mongod.service:

sudo systemctl ––system daemon-reload
sudo systemctl restart mongod

Step 3: Test Mongo User Authentication

Switch to the Mongo shell and use the admin user to list all users:

use admin
show users

An error message should display:

Mongo error message requiring authentication

Next, use the following command to authenticate with the credentials created in Part 2:

db.auth(‘mdbadmin’, ‘password’)

The system should respond with the number 1:


Providing Mongo authentication

Now, try running the show users command again:

show users

Replace mdbadmin and password with the actual username and password you created. The system should display the same user information as before in Part 2.


You should now have a working installation of MongoDB on your CentOS 8 system. Also, you should have a secure administrator account to prevent unauthorized access.

Next, learn how to create a database in MongoDB.

Next you should also read