How to Install MongoDB on CentOS 8

By 

Published on

4 min read

Install MongoDB on CentOS 8

MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.

In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and data structure can be changed over time.

This tutorial explains how to install and configure MongoDB Community Edition on a CentOS 8 server.

Installing MongoDB

MongoDB is not available in CentOS 8 core repositories. We’ll enable the official MongoDB repository and install the packages.

At the time of writing this article, the latest version of MongoDB available from the official MongoDB repositories is version 4.2. Before starting with the installation, visit the Install on Red Hat section of MongoDB’s documentation and check if there is a new release available.

Perform the following steps as root or user with sudo privileges to install MongoDB on a CentOS 8 system:

  1. Enable the MongoDB repository by creating a new repository file named mongodb-org.repo inside the /etc/yum.repos.d/ directory:

    Terminal
    sudo nano /etc/yum.repos.d/mongodb-org.repo
    /etc/yum.repos.d/mongodb-org.repoini
    [mongodb-org-4.2]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

    If you want to install an older version of MongoDB, replace each instance of 4.2 with your preferred version.

  2. Install the mongodb-org meta-package:

    Terminal
    sudo dnf install mongodb-org

    During the installation you will prompted you to import the MongoDB GPG key. Type y and hit Enter.

    The following packages will be installed on your system as a part of the mongodb-org package:

    • mongodb-org-server - The mongod daemon, and corresponding init scripts and configurations.
    • mongodb-org-mongos - The mongos daemon.
    • mongodb-org-shell - The mongo shell, an interactive JavaScript interface to MongoDB, used to perform administrative tasks thought the command line.
    • mongodb-org-tools - Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.
  3. Once the installation is completed, enable and start the MongoDB service:

    Terminal
    sudo systemctl enable mongod --now
  4. To verify the installation, connect to the MongoDB database server and print the server version:

    Terminal
    mongo

    Run the following command to display the MongoDB version:

    json
    db.version()

    The output will look like something like this:

    output
    4.2.3

Configuring MongoDB

The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML format.

The default configuration settings are sufficient in most cases. However, for production environments, we recommend uncommenting the security section and enabling authorization as shown below:

/etc/mongod.confyaml
security:
  authorization: enabled

The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled, each user will have access to any database and execute any action.

After making changes to the MongoDB configuration file, restart the mongod service:

Terminal
sudo systemctl restart mongod

For more information about the MongoDB configuration options visit, the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, you’ll need to create an administrative user that can access and manage the MongoDB instance.

First, access the MongoDB shell with:

Terminal
mongo

Type the following command to connect to the admin database:

Terminal
use admin
output
switched to db admin

Create a new user named mongoAdmin with the userAdminAnyDatabase role:

Terminal
db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
output
Successfully added user: {
	 : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
Info
You can name the administrative MongoDB user as you want.

Exit the mongo shell with:

Terminal
quit()

To test the changes, access the mongo shell using the administrative user you have previously created:

Terminal
mongo -u mongoAdmin -p --authenticationDatabase admin
output
MongoDB shell version v4.2.3
Enter password:
Terminal
use admin
output
switched to db admin

Now, print the users with:

Terminal
show users
output
{
	"_id" : "admin.mongoAdmin",
	 : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

Conclusion

We’ve shown you how to install and configure MongoDB 4.2 on your CentOS 8 server.

Consult The MongoDB 4.2 Manual for more information on this topic.

If you hit a problem or have feedback, leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page