How To Install MongoDB 4 In CentOS 8

How to install MongoDB 4 in CentOS 8

[Updated on Date: 2021-01-16] MongoDB is a popular document-based and general-purpose NoSQL database engine that stores data in JSON format. The MongoDB is free and open-source and ships with a set of cool and nifty features such as file storage, data replication, Ad-hoc queries, and load balancing just to mention a few. Some of the blue-chip companies that have incorporated MongoDB in their applications include Adobe, Facebook, Google, eBay, and Coinbase. In this article, we are going to learn you How To Install MongoDB 4 In CentOS 8. You can visit the packages available in Eldernode to purchase a CentOS VPS server.

Tutorial Install MongoDB 4 In CentOS 8

In this article, you will learn How to install MongoDB 4 in CentOS 8.  Join us to check the details.

Recommended Article: Tutorial Install Innotop to Monitor MySQL on CentOS

How To Add MongoDB Repository

Since MongoDB is not present in the CentOS 8 default repository, you need to add it manually. So firstly, create a repository file as shown.

vi /etc/yum.repos.d/mongodb.repo  

Paste the configuration below and save the file.

[mongodb-org-4.2]  name=MongoDB Repository  baseurl=$releasever/mongodb-org/development/x86_64/  gpgcheck=1  enabled=1  gpgkey=

Install MongoDB in CentOS 8 | CentOS 7

Once the repository enabled, you can install MongoDB using the following dnf command:

dnf install mongodb-org

Then, start and enable MongoDB to start on boot by running the commands below:

systemctl start mongod  
sudo systemctl enable mongod

Use the following command to verify the status of MongoDB:

systemctl status mongod  

And also, to confirm that indeed Mongod service is listening:

netstat -pnltu




How To Access MongoDB Shell

It is time to access MongoDB’s shell by simply issuing the command:


The output will be similar to the below screenshot




How To Create MongoDB Admin User

Let’s now switch gears and create an Admin user. It’s always good advice to create an admin user with elevated privileges to perform elevated tasks. To do so, first access MongoDB’s shell:


To switch to the database admin, type the command below.

> use admin  

Now, create a new MongoDB user by running the code below.

> db.createUser(   {   user: "mongod_admin",   pwd: "P@ssword@2019",   roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]   }  

You will get the output below when all is ok.

Successfully added user: {  	"user" : "mongod_admin",  	"roles" : [  		{  			"role" : "userAdminAnyDatabase",  			"db" : "admin"  		}  	]  }    



Recommended Article: How To Install MongoDB 4 In CentOS 8

Next, run the following command to list MongoDB users created.

show users  

How To Configure Authentication for MongoDB

As you see, all users can access the shell and execute any commands, which is not recommended at all for security purposes. So you need to create authentication for the admin user we just created so as to prevent other users from running commands without authorization.

To enable authentication to edit the /lib/systemd/system/mongod.service file, under the [Service] section, locate and edit the Environment parameter as shown:

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




Now, you can save and exit the configuration file.

For the changes to come into effect, reload the system and restart MongoDB.

systemctl daemon-reload
systemctl restart mongod

If you now try listing the users without authentication, you should get an error as shown:




To authenticate, simply pass the credentials as shown.

> db.auth('mongod_admin', 'P@ssword@2019')  

Now you can run any command after that. Let’s try listing the users once more:

> show users

This time, all went well since the authentication credentials were provided.




Run the command below to exit the database engine:



In this article, you succeeded to learn How to install MongoDB 4 in CentOS 8, and from now on we hope you are comfortable installing MongoDB4 on your CentOS 8 system and getting started with a few necessary steps. You can refer to the articles How to install MongoDB 4 on Debian 10 and How to install MongoDB on Windows.

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

View More Posts
Marilyn Bisson
Content Writer
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

10 thoughts on “How To Install MongoDB 4 In CentOS 8

    1. You can manage the MongoDB service by running the following command:
      sudo systemctl status mongod
      Note: When you see ‘’inactive’’ in the output, It means MongoDB service is not running. To ensure that MongoDB service is automatically starting on system boot, you must see the ‘’enable’’ in output.

    1. – In this situation, run the following command and then the second one (start MongoDB)
      sudo systemctl daemon-reload
      sudo systemctl start mongod

    1. You can use the following command to remove all packages you have installed.

      sudo yum erase $(rpm -qa | grep mongodb-org)

      And also to remove MongoDB database and log file run the commands below:
      sudo rm -r /var/log/mongodb
      sudo rm -r /var/lib/mongo

    1. Unfortunately it is not possible. The process you have done removed the configuration, and all databases. So this is not reversible. Next time, ensure back up all of your configuration and data before proceeding.

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way


7 days a week, 24 hours a day