How To Install MongoDB 4 In CentOS 8

How to install MongoDB 4 in CentOS 8

MongoDB is a popular document-based and general-purpose NoSQL database engine that stores data in JSON format. To experience a real installation, buy your perfect CentOS VPS.

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.



How To 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.



1- 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=


2- How To  Install MongoDB in CentOS 8

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

MongoDB Port verification

3- 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

The sell of MongoDB connection


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: "[email protected]@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" 		} 	] }  

How to crete admin user in MongoDB

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

> show users  The list of MongodB 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" 

How to Enable Authentication in Mongodb

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.

MongoDB Authentication checking

To authenticate, simply pass the credentials as shown.

> db.auth('mongod_admin', '[email protected]@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.

MongoDB User Authentication verification


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. In case you prefer to work with Windows or Debian, find your considered article.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.
  • Alison Khouth
    Alison Khouth
    7 days ago

    I need to undo uninstalling MogoDB.

    • Dani
      4 days ago

      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.

  • Miguel Goodman
    Miguel Goodman
    7 days ago

    good tutorial.i wanna remove packages and data. thanks

    • Dani
      4 days ago

      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

  • Steve Salle
    Steve Salle
    7 days ago

    is it possible to stop MongoDB?

    • Dani
      4 days ago

      Yes it is. Stop the MongoDB process by running the command below:
      sudo systemctl stop mongod

  • Billy Barrett
    Billy Barrett
    7 days ago

    thank you. After starting the MongoDB service, I received an error that the unit is not found.

    • Dani
      4 days ago

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

  • Dean Ferland
    Dean Ferland
    7 days ago

    What if we need to manage its service?

    • Dani
      4 days ago

      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.


Leave Your Comment

Your email address will not be published.

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