A tutorial on installing and configuring MongoDB on CentOS 8

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

In MongoDB, data is stored in a flexible JSON-like document where fields can vary from document to document. Databases do not require a predefined chart and data structures can change over time.

In this tutorial, we’ll cover the process of installing and configuring the latest version of MongoDB Community Edition on a CentOS 8 server.

Install MongoDB on CentOS 8

MongoDB is not available in CentOS 8 repositories. Therefore, we will enable the official MongoDB repository and install the packages.

At the time of writing, the most recent version of MongoDB is version 4.2. Before starting the installation, head over to the Red Hat installation page of MongoDB documentation and check if a new version is available.

Before proceeding with this tutorial, make sure you are logged in as root or user with sudo privileges.

  1. Enable the MongoDB repository by creating a new repository file called mongodb-org.repo In the manual /etc/yum.repos.d/:
    sudo nano /etc/yum.repos.d/mongodb-org.repo

    Copy and paste the following composition

    name=MongoDB Repository

    To install an older version of MongoDB, just replace it 4.2 With the version you choose

  2. Install the driver package mongodb-org :
    sudo dnf install mongodb-org

    During installation, you will be prompted to import MongoDB GPG key. Type y And press Enter.

  3. The following packages will be installed on your system as part of the packages mongodb-org:
    • mongodb-org-servermongod Daemon and appropriate script initialization.
    • mongodb-org-mongos – A demon mongos.
    • mongodb-org-shell Mongo shell is MongoDB’s JavaScript interface. This shell is used to perform administrative tasks via the command line.
    • mongodb-org-tools It contains many MongoDB tools for importing and exporting data, statistics, and other utilities.
  4. After the installation completes, run the MongoDB daemon and enable it to start up by typing:
    sudo systemctl enable mongod --now
  5. Check if the installation finished successfully or not, we will contact the MongoDB database server with the tool mongo And see the server version in action:

    Once in the MongoDB shell, type the following command which will display the MongoDB version:


Configure MongoDB on CentOS 8

You can configure the MongoDB version by editing the configuration file /etc/mongod.conf Written in YAML.

See also  Guide for installing Odoo 12 on CentOS 7

The default configuration settings should be suitable for most users. However, for a production environment, it is advised to slightly modify the Security section and enable authorization as shown below:

sudo nano /etc/mongod.conf
  authorization: enabled

Selection authorization Make it possible Role-based access control (RBAC) that governs user access to database resources and processes. If this option is disabled, then every user will have access to all databases and take any action.

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

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.0, visit the configuration documentation page.

Create MongoDB admin user

If MongoDB authentication is enabled, create a MongoDB admin user that will be used to access and manage the MongoDB instance.

First, access the mongo shell by:


Once in the MongoDB shell, type the following command to connect to the database admin:

use admin
switched to db admin

Use the following command to create a new user named mongoAdmin With a role userAdminAnyDatabase:

    user: "mongoAdmin", 
    pwd: "GantiDenganPassword", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"

You can rename MongoDB admin user as you wish.

Exit the mongo shell by typing:


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

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
switched to db admin

Now try to view the user with the command:

show users
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
	"mechanisms" : [

You can also try to access mongo shell without arguments (just type mongo) And see if you can create a user list using the same command as above.

See also  Yum commands fail with error "No module named yum" in RHEL 7


You learned how to install and configure MongoDB 4.2 on your CentOS 8 server.


Source link

Originally posted 2020-11-18 08:54:14.

About wahyuway

Check Also

How to install and configure Anaconda on CentOS 7

How to install and configure Anaconda on CentOS 7

Anaconda is the most popular machine learning and data science tool used in large scale …

Leave a Reply