How To Install MongoDB 4.4 on RHEL 8 | CentOS 8

por | 14 marzo, 2023

In this guide, we will cover the steps to install MongoDB 4.4 on RHEL 8 / CentOS 8. MongoDB is an open source NoSQL database system written in C++ designed to ensure scalability, high performance, and availability.

MongoDB common use case is storage and management of Big Data-sized collections of literal documents like text documents, email messages, XML documents, and many others.

Install MongoDB 4.4 on RHEL 8 / CentOS 8

MongoDB 4 is available on MongoDB yum repository. Add the repository to your RHEL 8 server by running below commands:

Once the repo has been added, install mongodb-org package.

sudo tee /etc/yum.repos.d/mongodb-org-4.repo<<EOF
[mongodb-org-4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOF
sudo yum install mongodb-org

Hit the y key to start installation of MongoDB 4.x on CentOS 8 | RHEL 8.

MongoDB Repository                                                                                                                                                    22 kB/s | 9.7 kB     00:00
Dependencies resolved.
=====================================================================================================================================================================================================
 Package                                               Architecture                Version                                                     Repository                                       Size
=====================================================================================================================================================================================================
Installing:
 mongodb-org                                           x86_64                      4.4.1-1.el8                                                 mongodb-org-4                                    10 k
Installing dependencies:
 cyrus-sasl                                            x86_64                      2.1.27-1.el8                                                rhel-8-baseos-rhui-rpms                          96 k
 cyrus-sasl-gssapi                                     x86_64                      2.1.27-1.el8                                                rhel-8-baseos-rhui-rpms                          49 k
 cyrus-sasl-plain                                      x86_64                      2.1.27-1.el8                                                rhel-8-baseos-rhui-rpms                          47 k
 mongodb-database-tools                                x86_64                      100.2.0-1                                                   mongodb-org-4                                    55 M
 mongodb-org-database-tools-extra                      x86_64                      4.4.1-1.el8                                                 mongodb-org-4                                    20 k
 mongodb-org-mongos                                    x86_64                      4.4.1-1.el8                                                 mongodb-org-4                                    22 M
 mongodb-org-server                                    x86_64                      4.4.1-1.el8                                                 mongodb-org-4                                    28 M
 mongodb-org-shell                                     x86_64                      4.4.1-1.el8                                                 mongodb-org-4                                    18 M
 mongodb-org-tools                                     x86_64                      4.4.1-1.el8                                                 mongodb-org-4                                    10 k
 python2                                               x86_64                      2.7.17-2.module+el8.3.0+7681+f1f02ded                       rhel-8-appstream-rhui-rpms                      109 k
 python2-libs                                          x86_64                      2.7.17-2.module+el8.3.0+7681+f1f02ded                       rhel-8-appstream-rhui-rpms                      6.0 M
 python2-pip-wheel                                     noarch                      9.0.3-18.module+el8.3.0+7707+eb4bba01                       rhel-8-appstream-rhui-rpms                      1.0 M
 python2-setuptools-wheel                              noarch                      39.0.1-12.module+el8.3.0+7075+8484f0d0                      rhel-8-appstream-rhui-rpms                      287 k
Installing weak dependencies:
 python2-pip                                           noarch                      9.0.3-18.module+el8.3.0+7707+eb4bba01                       rhel-8-appstream-rhui-rpms                      1.7 M
 python2-setuptools                                    noarch                      39.0.1-12.module+el8.3.0+7075+8484f0d0                      rhel-8-appstream-rhui-rpms                      642 k
Enabling module streams:
 python27                                                                          2.7

Transaction Summary
=====================================================================================================================================================================================================
Install  16 Packages

Total download size: 133 M
Installed size: 420 M
Is this ok [y/N]: y

Also accept importation of GPG key:

warning: /var/cache/dnf/mongodb-org-4-cef71e585db45e10/packages/mongodb-database-tools-100.2.0.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID 90cfb1f5: NOKEY
MongoDB Repository                                                                                                                                                   4.9 kB/s | 1.6 kB     00:00
Importing GPG key 0x90CFB1F5:
 Userid     : "MongoDB 4.4 Release Signing Key <[email protected]>"
 Fingerprint: 2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5
 From       : https://www.mongodb.org/static/pgp/server-4.4.asc
Is this ok [y/N]: y

The installation of the above package will install the following dependency packages:

mongodb-org-server – This provides MongoDB daemon mongod
mongodb-org-mongos – This is a MongoDB Shard daemon
mongodb-org-shell – This provides a shell to MongoDB
mongodb-org-tools – MongoDB tools used for export, dump, import e.t.c

You can verify installation of above packages, e.g.

$ rpm -qi mongodb-org-server
Name        : mongodb-org-server
Version     : 4.4.8
Release     : 1.el8
Architecture: x86_64
Install Date: Fri 20 Aug 2021 06:23:37 PM UTC
Group       : Applications/Databases
Size        : 83082993
License     : SSPL
Signature   : RSA/SHA1, Wed 04 Aug 2021 09:09:09 PM UTC, Key ID 656408e390cfb1f5
Source RPM  : mongodb-org-4.4.8-1.el8.src.rpm
Build Date  : Sun 01 Aug 2021 07:16:40 PM UTC
Build Host  : ip-10-122-58-203.ec2.internal
Relocations : /usr /var /etc
URL         : http://www.mongodb.org
Summary     : MongoDB database server

Configure MongoDB on RHEL 8 / CentOS 8

When the packages are installed, you can start customizing and configuring MongoDB before starting the service.

Label MongoDB port

If you have SELinux in enforcing mode, you may need to label port 27017

sudo semanage port -a -t mongod_port_t -p tcp 27017

Allow mongo port on the firewall

If you have firewalld running on your server and would like MongoDB service to be accessible over the network, open it on the firewall:

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

You can also limit access based on source address:

sudo firewall-cmd --permanent --add-rich-rule "rule family="ipv4" \
source address="10.10.20.0/24" port protocol="tcp" port="27017" accept"

Use secondary disk for MongoDB data – Optional

You can always use a dedicated disk / virtual disk to store MongoDB data. This can be configured like below

Step 1: Partition secondary disk for MongoDB data:

# lsblk  | grep vdb
vdb    252:16   0  50G  0 disk

Step 2: Create a GPT partition table for the secondary disk, it can be more than onde disk

sudo parted -s -a optimal -- /dev/vdb mklabel gpt
sudo parted -s -a optimal -- /dev/vdb mkpart primary 0% 100%
sudo parted -s -- /dev/vdb align-check optimal 1

Step 3: Create LVM volume, this will make it easy to extend the partition

sudo pvcreate  /dev/vdb1
sudo vgcreate vg11 /dev/vdb1
sudo lvcreate -n data -l 100%FREE vg11

Step 4: Create XFS filesystem on the Logical Volume created

sudo mkfs.xfs /dev/mapper/vg11-data

Step 5: Create a mount point and mount the partition

echo "/dev/mapper/vg11-data /data xfs defaults 0 0" | sudo tee -a /etc/fstab
sudo mkdir /data
sudo mount -a

Step 6: Create a folder for MongoDB data

sudo mkdir /data/mongo
sudo chown -R mongod:mongod /data/mongo
sudo chmod -R 775 /data/mongo

Step 7: Confirm that the partition mount was successful:

df -hT | grep  /data/mongo

Step 8: Change MongoDB data store location on /etc/mongod.conf

storage:
dbPath: /data/mongo 
journal:
  enabled: true

Start MongoDB Service

When all is set, start and set mongod service to start on boot.

sudo systemctl enable --now mongod

If the service was started successfully, it should show running status.

$ systemctl status mongod.service 
 ● mongod.service - MongoDB Database Server
    Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
    Active: active (running) since Sat 2019-01-05 12:12:28 EAT; 4s ago
      Docs: https://docs.mongodb.org/manual
   Process: 2163 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
   Process: 2161 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
   Process: 2160 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
   Process: 2158 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
  Main PID: 2166 (mongod)
    Memory: 70.1M
    CGroup: /system.slice/mongod.service
            └─2166 /usr/bin/mongod -f /etc/mongod.conf
 Jan 05 12:12:27 localhost.localdomain systemd[1]: Starting MongoDB Database Server…
 Jan 05 12:12:27 localhost.localdomain mongod[2163]: about to fork child process, waiting until server is ready for connections.
 Jan 05 12:12:27 localhost.localdomain mongod[2163]: forked process: 2166
 Jan 05 12:12:28 localhost.localdomain mongod[2163]: child process started successfully, parent exiting
 Jan 05 12:12:28 localhost.localdomain systemd[1]: Started MongoDB Database Server.

For Authentication, check our guide on How to configure MongoDB 4 authentication.