Cyberithub

How to Install and Use MongoDB on RHEL/CentOS 7/8 [Easy Steps]

Advertisements

In this article, I will take you through the steps to install and use MongoDB on RHEL/CentOS 7/8. According to official documentation, MongoDB is a scalable, flexible NoSQL document database platform designed to overcome the relational databases approach and the limitations of other NoSQL solutions. MongoDB is well known for its horizontal scaling and load balancing capabilities, which has given application developers an unprecedented level of flexibility and scalability. MongoDB provides developers with a number of useful out-of-the-box capabilities, whether you need to run privately on site or in the public cloud. We will see some more features about this on later articles. Presently, we will see the steps to install and use MongoDB on RHEL/CentOS 7/8.

How to Install and Use MongoDB on RHEL/CentOS 7/8 [Easy Steps]

How to Install and Use MongoDB on RHEL/CentOS 7/8

Also Read: Step by Step Guide to Install MongoDB on Ubuntu 20.04 LTS

Advertisements

Step 1: Prerequisites

a) You should have a running RHEL/CentOS 7/8 Server.

b) You should have sudo or root access to run privileged commands.

Advertisements

c) You should have yum utility available in your Server.

 

Step 2: Configure YUM Repo

MongoDB packages will not be available directly from RHEL/CentOS repo. You need to configure YUM repository to download and install MongoDB packages from there. This can be done by creating a repo file called mongodb-org-5.0.repo under /etc/yum.repos.d directory and adding below given contents. Then Save and exit the file using :wq!

Advertisements
[root@localhost ~]# vi /etc/yum.repos.d/mongodb-org-5.0.repo
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc

 

Step 3: Install MongoDB

Now you need to download and install MongoDB packages along with its required dependencies from newly configured repository using yum install -y mongodb-org command as shown below.

[root@localhost ~]# yum install -y mongodb-org
CentOS-8 - AppStream 4.3 kB/s | 4.3 kB 00:01
CentOS-8 - Base 5.5 kB/s | 3.9 kB 00:00
CentOS-8 - Extras 3.4 kB/s | 1.5 kB 00:00
MongoDB Repository 10 kB/s | 12 kB 00:01
Dependencies resolved.
=============================================================================================================================================================
Package Architecture Version Repository Size
=============================================================================================================================================================
Installing:
mongodb-org x86_64 5.0.3-1.el8 mongodb-org-5.0 11 k
Installing dependencies:
mongodb-database-tools x86_64 100.5.1-1 mongodb-org-5.0 47 M
mongodb-mongosh x86_64 1.1.2-1.el7 mongodb-org-5.0 43 M
mongodb-org-database x86_64 5.0.3-1.el8 mongodb-org-5.0 11 k

 

Step 4: Start MongoDB Service

Then start the service using systemctl start mongod command. All the MongoDB configuration can be done through mongod.conf file which is usually stored under /etc directory path. By default, MongoDB runs on Port 27017. After starting the service, you can check its status by using systemctl status mongod command as shown below.

Advertisements
[root@localhost ~]# systemctl start mongod
[root@localhost ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-11-07 03:49:39 EST; 2s ago
Docs: https://docs.mongodb.org/manual
Process: 21205 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 21199 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 21186 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 21127 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 21367 (mongod)
Memory: 127.9M
CGroup: /system.slice/mongod.service
└─21367 /usr/bin/mongod -f /etc/mongod.conf

 

Step 5: Start Mongosh 

Once the service is started and running fine, you can start the Mongosh session to run MongoDB queries. You just need to run mongosh command to start the session.

[root@localhost ~]# mongosh
Current Mongosh Log ID: 6187936b26ee805bc01dd9fa
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB: 5.0.3
Using Mongosh: 1.1.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
The server generated these startup warnings when booting:
2021-11-07T03:49:39.112-05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-11-07T03:49:39.113-05:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
------

test>

 

Step 6: Create a Database

After successfully setting up the Server, you can now create your first Database. Here we are creating a sample database called hellodb using use hellodb query. Then we are checking all the information about this database using show dbs query.

test> use hellodb
switched to db hellodb
hellodb> show dbs
admin 41 kB
config 12.3 kB
local 41 kB
hellodb>

 

Step 7: Remove MongoDB

Once you are done using MongoDB then you can also uninstall it by first stopping mongod daemon using systemctl stop mongod command. Check and verify the service status by using systemctl status mongod command.

[root@localhost ~]# systemctl stop mongod
[root@localhost ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sun 2021-11-07 03:57:34 EST; 7s ago
Docs: https://docs.mongodb.org/manual
Process: 21205 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 21199 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 21186 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 21127 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 21367 (code=exited, status=0/SUCCESS)

Then remove MongoDB Packages by using yum erase $(rpm -qa | grep mongodb-org) -y command.

[root@localhost ~]# yum erase $(rpm -qa | grep mongodb-org) -y
Modular dependency problems:

Problem 1: conflicting requests
- nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
Problem 2: conflicting requests
- nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
Dependencies resolved.
=============================================================================================================================================================
Package Architecture Version Repository Size
=============================================================================================================================================================
Removing:
mongodb-org x86_64 5.0.3-1.el8 @mongodb-org-5.0 0
mongodb-org-database x86_64 5.0.3-1.el8 @mongodb-org-5.0 0
mongodb-org-database-tools-extra x86_64 5.0.3-1.el8 @mongodb-org-5.0 15 k
mongodb-org-mongos x86_64 5.0.3-1.el8 @mongodb-org-5.0 71 M

Finally, you need to remove MongoDB databases and log files using rm -rf /var/log/mongodb and rm -rf /var/lib/mongo command as shown below.

[root@localhost ~]# rm -rf /var/log/mongodb
[root@localhost ~]# rm -rf /var/lib/mongo

Leave a Comment