How to Install and Configure MongoDB on CentOS 8

MongoDB is a NoSQL database that provides high performance, high availability, and automatic scaling. NoSQL database means that, unlike MySQL or PostgreSQL, it does not support SQL (Structured Query Language) to retrieve or manipulate the stored data.

MongoDB does not store data in tables. Instead, it stores data in a "document" structure similar to JSON (in MongoDB called BSON). MongoDB was first introduced in 2009, six years ago, and currently developed by MongoDB MongoDB Inc.

In this tutorial, we will show you how to install and configure the MongoDB 4.4 on the CentOS 8 Server. We will install MongoDB, edit some system settings to fix some error on MongoDB, create the administrator user for MongoDB, and then the MongoDB authentication.

Prerequisites

  • CentOS 8 Server
  • Root privileges
  • Understanding basic Linux/CentOS command

What will we do?

  • Add MongoDB Repository
  • Install MongoDB on CentOS 8
  • Fix Some MongoDB Error
  • Create MongoDB Admin User
  • Enable MongoDB Authentication
  • Testing

Step 1 - Add MongoDB Repository

First, log in to your server using your SSH user and password, then update all packages to the latest version using the command below.

ssh [email protected]
sudo dnf update

Now go to the '/etc/yum.repos.d' directory and create a new repository file 'mongodb-4.4.repo' using vim editor.

cd /etc/yum.repos.d/
vim mongodb-4.4.repo

Paste the following MongoDB 4.4 repository into it.

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

Save and close.

Next, check all available repositories on the CentOS system using the dnf command below.

sudo dnf repolist

Below is the result you will get.

Add MongoDB repository for CentOS 8

As a result, the MongoDB repository has been added to the CentOS 8 system.

Step 2 - Install MongoDB NoSQL Database Server

To install MongoDB NoSQL Database, run the dnf command below.

sudo dnf install -y mongodb-org

Once all installation is completed, start the MongoDB service and add it to the system boot.

systemctl start mongod
systemctl enable mongod

The MongoDB service is up and running, check it using the command below.

ss -plnt
systemctl status mongod

Below is the result you will get.

Check MongoDB Service status

As can be seen, the MongoDB service is running on default TCP port '27017'.

Step 3 - Fix MongoDB Error

At this stage, the MongoDB service is up and running on the CentOS 8 system. And for this step, we will enhance our MongoDB installation by changing some system configurations to fix some errors.

- Disable Transparent Huge Pages (THP) using Tuned

To increase MongoDB installation performance, we need to disable the THP or Transparent Huge Pages on our system. And for the CentOS system, you can disable THP through tuned profile configuration.

Create a new directory '/etc/tuned/virtual-guest-no-thp' for the custom tuned profile named as 'virtual-guest-no-thp'.

sudo mkdir -p /etc/tuned/virtual-guest-no-thp

Next, create the 'tuned.conf' configuration on the '/etc/tuned/virtual-guest-no-thp/' directory using vim editor.

vim /etc/tuned/virtual-guest-no-thp/tuned.conf

Paste the following configuration into it.

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

Save and close.

Now activate the new custom tuned profile 'virtual-guest-no-thp' using the tuned-adm command below.

sudo tuned-adm profile virtual-guest-no-thp

To verify the THP status on the Linux status, check using the following command.

cat /sys/kernel/mm/transparent_hugepage/enabled

Now make sure you get '[never]' response as below.

Disable THP using Tuned on CentOS 8

As can be seen, the Transparent Huge Pages on the CentOS system has been disabled through the Tuned profile.

- Setup UNIX Ulimit

Ulimits or User limits is used for defining how much of a system-wide resource a user may use. To improve and enhance the MongoDB performance, you need to increase the ulimit settings for the MongoDB service.

Now go to the '/etc/security/limits.d/' directory and create a new configuration 'monogd.conf' using vim editor.

cd /etc/security/limits.d/
vim mongod.conf

Paste the following configuration into it.

mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000

Save and close.

To apply the new configuration, run the command below.

sysctl -p

Now make sure you have no error, and the Ulimit configuration for MongoDB has been completed.

- Disable Warning MongoDB Cloud Monitoring (Optional)

This stage is optional. You can ignore information about the MongoDB Cloud Monitoring service when you logged in to the MongoDB shell.

To disable this warning, log in to the MongoDB shell using the "mongo" command below.

mongo

Now run the following query.

db.disableFreeMonitoring()

Type 'exit' to log out from the MongoDB shell and now reboot your server.

sudo reboot

And as a result, the warning of MongoDB Cloud Monitoring services has gone.

Setup ulimit and disable mongodb cloud monitoring

Step 4 - Create Admin User MongoDB

In this step, we will create the administrator user for MongoDB through the 'mongo' shell.

Log in to the MongoDB shell using the 'mongo' command below.

mongo

Now switch to the 'admin' database.

use admin

Then create the new user 'admin' with password 'hakasepasswordformongodbadmin' using the following query.

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

And the MongoDB user has been created, check all users on the MongoDB using the query below.

show users

Below is the result you will get.

Create Admin User MongoDB

As a result, the admin user for MongoDB has been created, type 'exit' to log out from the MongoDB shell.

Step 5 - Enable MongoDB Authentication

After creating the admin user, we will enable MongoDB authentication to prevent another user without sufficient privileges to see the data on the database.

To enable the MongoDB authentication, edit the configuration '/etc/mongod.conf' using vim editor.

vim /etc/mongod.conf

Uncomment the 'security' option and add the configuration as below.

security
    authorization: "enabled"

Save and close.

Next, restart the MongoDB service to apply the new configuration.

systemctl restart mongod

Make sure there is no error, and the MongoDB authentication has been enabled.

Enable MongoDB authentication

Step 6 - Testing

To test our installation and configuration of MongoDB authentication, you can verify through the MongoDB shell.

Log in to the MongoDB shell using the command below.

mongo

Next, switch to the database 'admin' and show all users on MongoDB using the following queries.

use admin
show users

Below is the result you will get.

MongoDB Test Authentication

As can be seen, you're unauthorized to show all users list on the database 'admin'.

Next, authenticate to the MongoDB server using the following query.

db.auth('admin', 'hakasepasswordformongodbadmin')

Now make sure that you get the response number '1', which means the authentication is successful.

Next, you can show and check all available users on your MongoDB server using the query below.

show users

And you will get the MongoDB admin that we just created on top.

MongoDB test Authentication

As a result, the installation and configuration of the MongoDB NoSQL Database Server on CentOS 8 system have been completed successfully.

Share this page:

1 Comment(s)