In this guide, we will cover the steps to install MongoDB 4 on CentOS 8.

MongoDB is a cross-platform, document-oriented and general-purpose database program. Classified as a NoSQL database program, MongoDB stores data in JSON format. It is free, open source, and it comes with lots of cool features, such as file storage, data replication, Ad-hoc queries, and load balancing and many others. Companies like Adobe, Facebook, Google, eBay or  Coinbase have already incorporated MongoDB in their applications.

– Add MongoDB Repository

MongoDB is not present in the CentOS 8 default repository, therefore we will add it manually. First, create a repository file, using the following command:

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

Paste the configuration below and save the file:

name=MongoDB Repository

– Install MongoDB in CentOS 8

Once the repository was enabled, install MongoDB using dnf command:

# dnf install mongodb-org

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

# systemctl start mongod
# sudo systemctl enable mongod

Verify the status of MongoDB, running the command:

# systemctl status mongod

netstat utility can be also used to check that Mongod service is listening:

# netstat -pnltu

– Access MongoDB Shell

At this stage, you can access MongoDB’s shell by simply issuing the command:

# mongo


Creating MongoDBan Admin User

To create an admin user with elevated privileges to perform elevated tasks is always a good advice. To create an Admin user, access MongoDB’s shell:

# mongo

Then, switch to the database admin by running:

> use admin

Create a new MongoDB user by running the code below:

user: "mongod_admin",
pwd: "[email protected]@2019",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

You should see the following output if successful:

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

In  order to list MongoDB users created, run:

> show users


Configuring Authentication for MongoDB

For security purposes, we need to create authentication for the admin user we just created so as to prevent other users from running commands without authorization. We will enable authentication 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"

Save and exit the configuration file.

Reload the system and restart MongoDB for the changes to come into effect:

# systemctl daemon-reload
# systemctl restart mongod

If you will try at this point to list the users without authentication, you will get the error: “uncaught exception: Error: command usersInfo requires authentication:”.

To authenticate, pass the credentials as shown:

> db.auth('mongod_admin', '[email protected]@2019')

Now you may run any command, we could try listing the users once more, for example:

> show users

Now everything goes well since the authentication credentials were provided.

To exit the database engine run the command:

> exit