How To Install MongoDB on Centos 8

MongoDB is an open source and document-based NoSQL database designed to efficiently process large amounts of data. Stores data as Binary JSON documents (BSON). MongoDB provides ACID support in 4.0 and later versions.

In order to achieve high performance in MongoDB, some features commonly found in RDBMS systems are not available in MongoDB. MongoDB does not have table, row and column concepts. It has a dynamic schema structure. So documents can have different schemes, which means that the scheme can change as the application evolves.

A single MongoDB instance can host multiple databases. Each database is a collection set. Collections are similar to the concept of tables in relational databases; however, these are schematic. There can be more than one document in a collection. We can think of the document as a row in relational databases.

We will install MongoDB on CentOS 8 server.

Install MongoDB on Centos 8

For the installation, we create the “/etc/yum.repos.d/mongodb-org-4.2.repo” repo file with the vim command and add the following lines into the file, save and exit:

We install the mongodb-org package:

The mongodb-org package we have installed consists of “mongos, server, shell and tools” packages.

mongodb-org-server: It includes the Mongod program, and the init command.

mongodb-org-mongos: It includes the Mongos program.

mongodb-org-shell: Includes Mongo shell program.

mongodb-org-tools: Contains mongo commands; mongodump, mongorestore, mongoexport, mongoimport, mongostat, mongotop etc.

We start the mongod(MongoDB service), and enable it to start automatically when the server is started:

Is mongodb running?

We can check Mongod service status as follows:

The MongoDB configuration file is “/etc/mongod.conf” by default. It runs on port 27017 at 127.0.0.1 with default settings.

With the mongo command, we connect to the database with default settings:

With the show dbs command, we can view the existing databases:

MongoDB has admin, local and config databases by default.

admin database in MongoDB

The root database for administrative operations. If a user is added to the admin database, the user automatically inherits the permissions of all databases. There are also some service-wide commands that can only be run from the admin database, such as listing all databases or shutting down the service.

local database in MongoDB

Contains information of MongoDB service in replication processes. It is also not copied during replication processes.

config  database in MongoDB

Stores Sharded cluster information.

By default, the authentication setting is set to disable when MongoDB service is installed. This is the reason for the warning you see in the command output below.
“WARNING: Access control is not enabled for the database.”

We will not receive this warning when we create an authorized user in the database, enable the authentication setting in the default configuration file “/etc/mongod.conf” and restart the service.

MongoDB recommends disabling Transparent HugePages for better performance. This setting is enabled by default on most Linux distributions. This is the reason for the warning you see in the command output below. “WARNING: / sys / kernel / mm / transparent_hugepage / enabled is ‘always’.”

After creating users in the database, we will change this setting as recommended.

Create User in MongoDB and MongoDB Database Operations

We connect to the admin database with the following command and create an authorized user in the entire cluster:

Create admin user in MongoDB:

Example command output is as follows:

We exit the database with the exit command and add the following line to the file “/etc/mongod.conf”:

The final version of the /etc/mongod.conf file is as follows:

We are restarting the Mongod service:

Restart Mongod Service:

When we connect to the database with the following command, we will no longer see the authentication warning:
Let’s create a new database and collection and add records with the following commands:

use customer
It connects to a database named customer, it creates if there is no database with that name. If we leave the database without creating an object, the database will be deleted.

db
It shows the database to which we are connected.

db.people.insert ()
In the customer database, it adds a record to the collection named people, and creates it if there is no collection.

db.people.find ()
Returns all records in the people collection.

show dbs
Returns the current database list.

show collections
Returns the list of collections in the database we connect to.

Disable Transparent HugePages

Transparent HugePages (THP) is a memory management system that is enabled by default in most Linux operating systems. In order for MongoDB to run better on Linux, THP must be disabled.

Let’s create a service file that will disable THP before the mongodb service starts every time the server is started.

Reload systemd:

We start the service and enable it to be enable every time the server is started.

Check Transparent HugePages

We can see that the THP setting is [never] with the following command:

Sample output is as follows:

There are other ways to disable Transparent HugePages. You can find it on the MongoDB page.

In this article, I talked about basic MongoDB installation. You can find more detailed information in the article below.

How To Install Mongodb Sharded Cluster with Keyfile Access Control on Red Hat or CentOS

Zekiye AYDEMİR
Author: Zekiye AYDEMİR

Leave a Reply

Your email address will not be published. Required fields are marked *