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. It’s free and opensource 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 tutorial, you will learn how to install MongoDB on CentOS 8.

Step 1: Add MongoDB Repository

Since MongoDB is not present in the CentOS 8 default repository, we are going 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.

name=MongoDB Repository

Step 2: Install MongoDB in CentOS 8

Having enabled the repository, the next step will be to install MongoDB using the following dnf command.

# dnf install mongodb-org

Next, start and enable MongoDB to start on boot by running the commands below.

# systemctl start mongod
# sudo systemctl enable mongod

To verify the status of MongoDB, run:

# systemctl status mongod

Alternatively, you can use the netstat utility to confirm that indeed Mongod service is listening.

# netstat -pnltu
Verify MongoDB Port

Verify MongoDB Port

Great! we have confirmed that MongoDB is up and running.

Step 3: Access MongoDB Shell

You can now access MongoDB’s shell by simply issuing the command:

# mongo

You should get output similar output as shown in the screenshot below.

Connect MongoDB Shell

Connect MongoDB Shell

Creating MongoDBan 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:

# mongo

Next, switch to the database admin by running.

> 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" } ]

If successful you should get the output below.

Successfully added user: {
	"user" : "mongod_admin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
Create Admin User in MongoDB

Create Admin User in MongoDB

To list MongoDB users created, run.

> show users
List MongoDB Users

List MongoDB Users

Configuring Authentication for MongoDB

As it is, all users can access the shell and execute any commands, which is not recommended at all for security purposes. With that in mind, we 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 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"
Enable Authentication in Mongodb

Enable Authentication in Mongodb

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.

Check MongoDB Authentication

Check MongoDB Authentication

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.

Verify MongoDB User Authentication

Verify MongoDB User Authentication

To exit the database engine run.

> exit

And that’s all for today. We hope that by now you are comfortable installing MongoDB4 on your CentOS 8 system and getting started with a few necessary steps.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

James Kiarie

This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.