mongodb centos8

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

In MongoDB, data is stored in flexible JSON-like documents, with fields varying with the document. It does not require a predefined schema and the data structure can change over time.

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

Installing MongoDB

MongoDB is not available in the CentOS 8 core repository. We will enable the official MongoDB repository and install the package.

At the time of this writing, the latest version of MongoDB available from the official MongoDB repository is version 4.2. Before starting the installation, please visit the MongoDB documentation at Installing on Red Hat section of the MongoDB documentation and check to see if a new version is available.

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

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

1
sudo nano /etc/yum.repos.d/mongodb-org.repo
1
2
3
4
5
6
[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

Install the mongodb-org metadata package.

1
sudo dnf install mongodb-org

During the installation process, you will be prompted to import the MongoDB GPG key. Type y and click Enter.

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

  • mongodb-org-server - The mongod daemon and the corresponding initialization scripts and configuration.
  • mongodb-org-mongos - The mongos daemon.
  • mongodb-org-shell - The mongo shell, which is MongoDB’s interactive JavaScript interface for performing administrative tasks from the command line.
  • mongodb-org-tools - Contains several MongoDB tools for importing and exporting data, statistics, and other programs.

After installation is complete, enable and start the MongoDB service at.

1
sudo systemctl enable mongod --now

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

1
mongo

Run the following command to display the MongoDB version.

1
db.version()

The output looks like this.

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

In most cases, the default configuration settings will be sufficient. However, for production environments, we recommend uncommenting the security section and enabling authorization, as follows.

1
2
security:
  authorization: enabled

The authorization option enables Role-Based Access Control (RBAC), which manages user access to database resources and operations. If this option is disabled, each user will have access to any database and perform any operation.

After changing the MongoDB configuration file, restart the mongod service.

1
sudo systemctl restart mongod

For more information on MongoDB configuration options, visit the Profile Options documentation page.

Creating a MongoDB administrative user

If MongoDB authentication is enabled, you need to create an administrative user that can access and manage your MongoDB instance.

First, access the MongoDB Shell using the following command.

1
mongo

Type the following command to connect to the admin database.

1
use admin
1
switched to db admin

Use the userAdminAnyDatabase role to create a new user named mongoAdmin.

1
db.createUser(
1
2
3
4
5
6
7
8
9
Successfully added user: {
    "user" : "mongoAdmin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

You can name MongoDB administrative users as needed.

To exit the mongo shell.

1
quit()

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

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

Now, print the user with the following command.

1
show users
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
    "_id" : "admin.mongoAdmin",
    "user" : "mongoAdmin",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}

Conclusion

We have shown you how to install and configure MongoDB 4.2 on CentOS 8 server.

Please check the MongoDB 4.2 manual for more information on this topic.