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: "[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 liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

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.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

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