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:

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

sudo nano /etc/yum.repos.d/mongodb-org.repo

/etc/yum.repos.d/mongodb-org.repo

[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

Copy

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

Install the mongodb-org meta-package:

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.

Once the installation is completed, enable and start the MongoDB service:

sudo systemctl enable mongod --now

To verify the installation, connect to the MongoDB database server and print the server version:

mongo

Run the following command to display the MongoDB version:

db.version()

Copy

The output will look like something like this:

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.conf

security:
  authorization: enabled

Copy

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:

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:

mongo

Type the following command to connect to the admin database:

use admin
switched to db admin

Create a new user named mongoAdmin with the userAdminAnyDatabase role:

db.createUser(
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

You can name the administrative MongoDB user as you want.

Exit the mongo shell with:

quit()

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

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

Now, print the users with:

show users
{
	"_id" : "admin.mongoAdmin",
	"user" : "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.