Home / How To / 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 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 is currently being developed by MongoDB MongoDB Inc.

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

conditions

  • CentOS 8 Server
  • Red privileges
  • Understand basic Linux / CentOS command

What should we do?

  • Add the MongoDB archive
  • Install MongoDB on CentOS 8
  • Fix any MongoDB error
  • Create MongoDB Admin User
  • Enable MongoDB authentication
  • Testing

Step 1 – Add the MongoDB archive

Log in to your server with your SSH user and password, then update all packages to the latest version with the command below.

ssh [email protected]
sudo dnf update

Now go to the directory ‘/etc/yum.repos.d’ and create a new archive file ‘mongodb-4.4.repo’ with the vim editor.

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

Paste the following MongoDB 4.4 archive 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.

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

sudo dnf repolist

Below you see the result.

Add the MongoDB archive for CentOS 8

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

Step 2 – Install MongoDB NoSQL Database Server

Run the dnf command below to install MongoDB NoSQL database.

sudo dnf install -y mongodb-org

When all installation is complete, start the MongoDB service and add it to the system boot.

systemctl start mongod
systemctl enable mongod

The MongoDB service is running, check it with the command below.

ss -plnt
systemctl status mongod

Below you see the result.

Check the status of MongoDB Service

As can be seen, the MongoDB service runs on standard TCP port ‘27017’.

Step 3 – Fix MongoDB Error

At this stage, the MongoDB service is running the CentOS 8 system. And for this step, we will improve our MongoDB installation by changing some system configurations to fix some bugs.

Disable Transparent Large Pages (THP) with Tuned

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

Create a new directory ‘/ etc / tuned / virtual-guest-no-thp’ for the custom set profile named ‘virtual-guest-no-thp’.

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

Then create the configuration ‘tuned.conf’ in the directory ‘/ etc / tuned / virtual-guest-no-thp /’ with the 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 set profile ‘virtual-guest-no-thp’ with the command tuned-adm below.

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

Check using the following command to verify THP status on Linux status.

cat /sys/kernel/mm/transparent_hugepage/enabled

Make sure you get it ‘[never]’ answers as below.

Disable THP with Tuned on CentOS 8

As can be seen, the transparent large pages on the CentOS system have been deactivated by the set profile.

– Setting UNIX Ulimit

Ulimits or user limits are used to define how much of a system-wide resource a user can use. To enhance and enhance MongoDB performance, you need to increase the ulimit settings of the MongoDB service.

Now go to the directory ‘/etc/security/limits.d/’ and create a new configuration ‘monogd.conf’ with the 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.

Run the command below to apply the new configuration.advertisement

sysctl -p

Now make sure you have no errors and that the Ulimit configuration for MongoDB is complete.

– Disable warning MongoDB Cloud Monitoring (optional)

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

To disable this alert, log in to the MongoDB shell using the “mongo” command below.

mongo

Now run the following question.

db.disableFreeMonitoring()

Write ‘outputto log out of the MongoDB shell and restart your server now.

sudo reboot

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

Set ulimit and disable mongodb cloud monitoring

Step 4 – Create Admin User MongoDB

In this step, we 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 the password ‘hakasepasswordformongodbadmin’ with the following question.

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

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

show users

Below you see the result.

Create Admin User MongoDB

As a result, the admin user of MongoDB has been created, type ‘exit’ to log out of the MongoDB shell.

Step 5 – Enable MongoDB authentication

After creating the admin user, we enable MongoDB authentication to prevent another user without sufficient privileges from viewing data in the database.

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

vim /etc/mongod.conf

Deselect the “security” option and add the configuration as below.

security
    authorization: "enabled"

Save and close.

Then restart the MongoDB service to apply the new configuration.

systemctl restart mongod

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

Enable MongoDB authentication

Step 6 – Test

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

Then switch to the ‘admin’ database and view all MongoDB users with the following questions.

use admin
show users

Below you see the result.

MongoDB Test authentication

As can be seen, you are not authorized to view all user lists in the “admin” database.

Then verify to the MongoDB server with the following question.

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

Now make sure you get the answer number ‘1’, which means that the authentication is successful.

You can then view and check all available users on your MongoDB server with the question below.

show users

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

MongoDB Test Authentication

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


Source link