How to Install MongoDB on RHEL 8 / CentOS 8

MongoDB is a scalable, Open source, high performance, and document-oriented NoSQL database. It is developed and supported by 10gen. NoSQL stats that MongoDB doesn’t use Tables and rows.

MongoDB provide better performance as compared to other databases because it saves the data in JASON like documents along with dynamic schema.

In this guide, we will cover MongoDB Community Edition Installation on RHEL 8 and CentOS 8 system. Without any further ado, let’s deep dive into the installation steps.

1) Enable MongoDB 4.4 Yum Repository

MongoDB package and its dependencies are not available in RHEL 8 and CentOS 8 package repositories. So, to install mongodb with yum or dnf command, create the following repository file with beneath contents.

$ sudo vi /etc/yum.repos.d/mongodb-org-4.repo

[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 the file.

2) Install MongoDB package with dnf / yum command

To install MongoDB and its dependent packages use the below dnf command.

$ sudo yum install -y mongodb-org

Install-mongodb4-rhel-centos-dnf-command

Above command will install the followings MongoDB Packages

Mongodb-org-package-component

Following files and directory will be created once the MongoDB packages are installed.

  • /etc/mongod.conf — Configuration file of MongoDB [ By default localhost IP  (127.0.0.1) is bind IP and 27017 is the default port ]
  • /var/lib/mongo  — Data directory of MongoDB
  •  /var/log/mongodb/mongod.log  — Log file of MongoDB

3) Start and Enable the MongoDB Service

Note: During the demonstration, I have kept the SELinux status as permissive. Run beneath command to set selinux as permissive.

$ getenforce
Enforcing
$ sudo setenforce 0
$ sudo sed -i s/^SELINUX=.*$/SELINUX=permissive/ /etc/selinux/config

Run the beneath commands to start and enable the mongodb service across reboot.

$ sudo systemctl start mongod
$ sudo systemctl enable mongod
$ sudo systemctl status mongod

Output of above command,

Start-enable-mongod-service-rhel

In case OS firewall is enabled and running then open the MongoDB port ‘27017‘ using below firewalld-cmd command.

$ sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
$ sudo firewall-cmd --reload

4) Connect to MongoDB from Terminal

Type the command ‘mongo’ from the terminal to connect MongoDB

$ mongo

After connecting to the Mongo shell, we will get following,

Mongo-Shell-RHEL-CentOS

Above confirms that mongodb installation is completed as we are able to connect mongo shell.

Uninstall / Remove MongoDB

Run the beneath commands one after the another from the console to remove MongoDB completely.

$ sudo systemctl stop mongod
$ sudo yum erase $(rpm -qa | grep mongodb-org)
$ sudo rm -rf /var/log/mongodb
$ sudo rm -rf /var/lib/mongo

Reference:  https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

1 thought on “How to Install MongoDB on RHEL 8 / CentOS 8”

Leave a Comment