MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling. MongoDB is developed by MongoDB Inc. and is published under a combination of the GNU Affero General Public License and the Apache License.

Key features of MongoDB includes High Performance, Rich Query Language, High Availability, Horizontal Scalability, and Support for Multiple Storage Engines.

In this guide, we will explain how to install MongoDB on a CentOS 8 server.

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can easily deploy your own cloud servers.

Once you have signed up, log into your Cloudwafer Client Area and deploy your Cloudwafer cloud server.

Updating System Packages
It is always recommended that you update the system to the latest packages before beginning any major installations. This is done with the command below:

sudo yum update

Next, we need to add the MongoDB Repository as MongoDB does not exist within the default repositories for CentOS.

Step One: Configure repository
Create an /etc/yum.repos.d/mongodb-enterprise-4.2.repo file so that you can install MongoDB enterprise directly using yum:

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

Enter the following:

[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

Ensure to save before closing the file.

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

We can verify that the MongoDB repository that we just created exists within the yum utility of our system using the repolist command which displays a list of enabled repositories:

sudo yum repolist

Step Two: Install MongoDB
Now that we have verified that the MongoDB repository has been added, we can now proceed to install MongoDB:

sudo yum install mongodb-org

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 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 to importing and exporting data, statistics, as well as other utilities.

There are two confirmation prompts to finalize the installation. The first one allows the installation of the MongoDB packages while the second one imports a the MongoDB GPG key. Type Y at both times to confirm.

Next, start and enable the MongoDB service to start on boot using the following commands:

sudo systemctl start mongod
sudo systemctl enable mongod

Note: MongoDB needs a folder to store the database. Create a /data/db/ directory with the command below:

sudo mkdir -p /data/db/

Step Three:Verifying Installation
After our installation, we can verify that everything was successfully done by connecting to the MongoDB database server using the mongo tool and print the server version:

 mongo

This command will take you to the MongoDB shell. It will also display the MongoDB version:

Step Four: Configuring MongoDB
After verifying our installation, we can configure our MongoDB instance by editing the /etc/mongod.conf configuration file.

In most cases, the default configuration settings are adequate in most cases,nevertheless, for production environments we recommend uncommenting the security section and enabling authorization as shown bellow:

 sudo nano /etc/mongod.conf

security:
    authorization: enabled

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

sudo systemctl restart mongod

To read about the configuration options available in MongoDB 4.2 visit the Configuration File Options documentation page.

Step Five: Create Administrative MongoDB User
In the last step, we enabled the MongoDB authentication, hence, we can create one administrative MongoDB user that we will use to access and manage our MongoDB instance. Type the command below to access the mongo shell:

mongo

Type the following command to connect to the admin database:

 use admin

Create a new user named mongoAdmin with the userAdminAnyDatabase role:

 db.createUser(
     {
          user: "mongoAdmin", 
          pwd: "cloudwaferlabs123", 
          roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
        }
      )

Exit the mongo shell with:

quit()

To test the changes we just made, access the mongo shell using the admin user we recently created:

mongo -u mongoAdmin -p --authenticationDatabase admin

use admin

Next, print the users with:

show users

We have successfully installed and configured MongoDB 4.2 on our CentOS 8 server.