How to install and configure MongoDB on CentOS 8

How to install and configure MongoDB on CentOS 8

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

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

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

prerequisites

  • CentOS 8 server
  • Root privilege
  • Understand basic Linux/CentOS commands

What will we do?

  • Add MongoDB repository
  • Install MongoDB on CentOS 8
  • Fix some MongoDB errors
  • Create MongoDB administrator user
  • Enable MongoDB authentication
  • testing

Step 1-add MongoDB repository

First, log in to the server with an SSH user and password, and then use the following command to update all software packages to the latest version.

ssh [email protected]sudo dnf update

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

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 Repositorybaseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

Save and close.

Next, use the following dnf command to check all available repositories on the CentOS system.

sudo dnf repolist

Here are the results you will get.

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

Step 2-install MongoDB NoSQL database server

To install the MongoDB NoSQL database, run the following dnf command.

sudo dnf install -y mongodb-org

After completing all the installations, start the MongoDB service and add it to the system boot.

systemctl start mongodsystemctl enable mongod

MongoDB service is up and running, please use the following command to check.

ss -plntsystemctl status mongod

Here are the results you will get.

Check MongoDB service status

It can be seen that the MongoDB service runs on the default TCP port “27017”.

Step 3-fix MongoDB errors

At this stage, the MongoDB service has been started and running on the CentOS 8 system. For this step, we will enhance the MongoDB installation by changing some system configurations to fix some errors.

-Use “Adjusted” to disable transparent huge pages (THP)

In order to improve the installation performance of MongoDB, we need to disable THP or “transparent huge pages” on the system. For CentOS systems, you can disable THP by adjusting the configuration file configuration.

Create a new directory “/etc/tuned/virtual-guest-no-thp” for the custom tuning profile named “virtual-guest-no-thp”.

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

Next, use the vim editor to create the’tuned.conf’ configuration in the’/etc/tuned/virtual-guest-no-thp/’ directory.

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, use the following tuned-adm command to activate the new custom adjusted profile’virtual-guest-no-thp’.

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

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

cat /sys/kernel/mm/transparent_hugepage/enabled

Now make sure you get ‘[never]’ The response is as follows.

Use Tuned to disable THP on CentOS 8

It can be seen that the “transparent huge page” on the CentOS system has been disabled through the “adjustment” configuration file.

-Set UNIX Ulimit

Ulimits or User limit is used to define how many system-wide resources a user can use. In order to improve and enhance the performance of MongoDB, you need to increase the ulimit setting of the MongoDB service.

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

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

Paste the following configuration into it.

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

Save and close.

To apply the new configuration, run the following command.

sysctl -p

Now make sure that there are no errors and the Ulimit configuration of MongoDB is complete.

-Disable warning MongoDB cloud monitoring (optional)

This stage is optional. When logging in to MongoDB Shell, you can ignore the information about the MongoDB Cloud Monitoring service.

To disable this warning, log into the MongoDB Shell using the “mongo” command below.

mongo

Now run the following query.

db.disableFreeMonitoring()

enter”ExportTo exit the MongoDB Shell, restart the server now.

sudo reboot

As a result, the warning of the MongoDB Cloud Monitoring service disappeared.

Set ulimit and disable mongodb cloud monitoring

Step 4-create admin user MongoDB

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

Log in to MongoDB Shell using the “mongo” command below.

mongo

Now switch to the “admin” database.

use admin

Then use the following query to create a new user “admin” with a password of “hakasepasswordformongodbadmin”.

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

And MongoDB users have been created, please use the following query to check all users on MongoDB.

show users

Here are the results you will get.

Create admin user MongoDB

As a result, an administrator user of MongoDB has been created, type “exit” to log out from MongoDB Shell.

Step 5-enable MongoDB authentication

After creating an administrator user, we will enable MongoDB authentication to prevent another user who does not have sufficient privileges from viewing the data in the database.

To enable MongoDB authentication, use the vim editor to edit the configuration’/etc/mongod.conf’.

vim /etc/mongod.conf

Uncomment the “Security” option and add the following configuration.

security    authorization: "enabled"

Save and close.

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

systemctl restart mongod

Make sure there are no errors and MongoDB authentication is enabled.

Enable MongoDB authentication

Step 6-test

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

Log in to the MongoDB Shell using the following command.

mongo

Next, switch to the database “admin” and use the following query to display all users on MongoDB.

use adminshow users

Here are the results you will get.

MongoDB test authentication

As you can see, you do not have the right to display all user lists on the database “admin”.

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

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

Now make sure you get a reply number ‘1’, Which means that the authentication is successful.

Next, you can use the following query to display and check all available users on the MongoDB server.

show users

You will get the MongoDB administrator we just created.

MongoDB test authentication

As a result, the installation and configuration of the MongoDB NoSQL database server on the CentOS 8 system has been successfully completed.

Sidebar