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.

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

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: "P@ssword@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', '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.

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.