How To Install MongoDB On CentOS 8 / RHEL 8

THIS DOCUMENT IS ALSO AVAILABLE FOR

MongoDB is an open-source, one of the most popular document-oriented databases (NoSQL). In MongoDB, data is stored JSON-like documents with dynamic schemas unlike in tables and rows in a relational database.

MongoDB is designed with both scalability and developer agility in mind. It is available for Windows, Linux, OS X, Solaris, and FreeBSD.

In this post, we will see how to install MongoDB on CentOS 8 / RHEL 8 .

MongoDB Editions

MongoDB Inc releases two server editions of MongoDB for it’s end-users.

  1. MongoDB Enterprise Server

  2. MongoDB Community Server

Here, we will see how to install the MongoDB community edition. MongoDB community server is released under Server Side Public License (SSPL).

Add MongoDB repository

MongoDB Inc provides a dedicated software repository to download and install the MongoDB database.

Add the MongoDB repository on your system.

cat << EOF > /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF

Install MongoDB

Once you have added MongoDB repository, use the dnf command to install MongoDB.

dnf install -y mongodb-org
mongob-org (Meta Package that installs below components)
mongodb-org-server Server Package
mongodb-org-mongos Shared Daemon
mongodb-org-shell Command Line Interface
mongodb-org-tools MongoDB Tools (Import, Export, Restore, Dump and other tools)

SELinux

To run MongoDB with SELinux enabled (enforcing mode), follow the instruction to customize the SELinux policy for MongoDB. Skip the below steps, in case you have disabled SELinux on your system.

Install the checkpolicy package with dnf command.

dnf install -y checkpolicy

Create an SELinux policy file for allowing the MongoDB process to access the cgroup to determine the available memory on your system.

cat > mongodb_cgroup_memory.te <<EOF
module mongodb_cgroup_memory 1.0;

require {
    type cgroup_t;
    type mongod_t;
    class dir search;
    class file { getattr open read };
}

#============= mongod_t ==============
allow mongod_t cgroup_t:dir search;
allow mongod_t cgroup_t:file { getattr open read };
EOF

Compile and load our custom SELinux policy module using the below commands.

checkmodule -M -m -o mongodb_cgroup_memory.mod mongodb_cgroup_memory.te

semodule_package -o mongodb_cgroup_memory.pp -m mongodb_cgroup_memory.mod

semodule -i mongodb_cgroup_memory.pp

Post-Installation Steps

Ulimit Settings

By default, most of the systems have a limit set for the number of processes, open files, memory, etc. a particular user (application fid) can have, and these limits may cause issues in MongoDB operation.

MongoDB now provides recommended limit settings in the systemd unit file. So, we don’t need to set the limits manually.

Enable Access Control

If you take relational databases like MySQL or PostgreSQL, etc. comes with at least some level of authentication for which users need to authenticate themselves to perform database activities.

But, in the case of MongoDB, it is not the case. All users can access any database and perform any operations without authentication.

To resolve this issue, we need to enable access control by editing MongoDB’s configuration file.

vi /etc/mongod.conf

Add the below lines.

security:
  authorization: enabled

After enabling the access control, you can go ahead and create an admin user for daily operation.

Disable Huge Pages

Transparent huge pages often create performance issues for database workloads. So, MongoDB recommends the huge pages be disabled for best performance.

Create a directory place a tuned profile file to disable huge pages.

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

Create a file.

vi /etc/tuned/virtual-guest-no-thp/tuned.conf

Place the below lines into the able file.

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

Enable our newly created profile.

tuned-adm profile virtual-guest-no-thp

Start MongoDB service

To start MongoDB service, run:

systemctl start mongod

To check the status MongoDB service, run:

systemctl status mongod
Output:

 mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-02-13 10:02:47 EST; 992ms ago
     Docs: https://docs.mongodb.org/manual
  Process: 6896 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 6893 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 6892 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 6890 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 6898 (mongod)
   Memory: 68.2M
   CGroup: /system.slice/mongod.service
           └─6898 /usr/bin/mongod -f /etc/mongod.conf

Feb 13 10:02:46 centos8.itzgeek.local systemd[1]: Starting MongoDB Database Server...
Feb 13 10:02:46 centos8.itzgeek.local mongod[6896]: about to fork child process, waiting until server is ready for connections.
Feb 13 10:02:46 centos8.itzgeek.local mongod[6896]: forked process: 6898
Feb 13 10:02:47 centos8.itzgeek.local mongod[6896]: child process started successfully, parent exiting
Feb 13 10:02:47 centos8.itzgeek.local systemd[1]: Started MongoDB Database Server.

Check the MongoDB version.

mongod --version
Output:

db version v4.2.3
git version: 6874650b362138df74be53d366bbefc321ea32d4
OpenSSL version: OpenSSL 1.1.1 FIPS  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel80
    distarch: x86_64
    target_arch: x86_64

Use netstat command to check whether the MongoDB is listening on port 27017.

netstat -antup | grep -i 27017
Output:

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      6898/mongod

READ: netstat command not found on CentOS 7 / RHEL 7 – Quick Fix

Access MongoDB

Connect to MongoDB shell by entering the following command.

mongo
Output:

MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1f628ff6-80e0-4aef-949d-0f66ebc30852") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
>

Conclusion

That’s all. I hope, by now, you have MongoDB on your CentOS 8 / RHEL 8 system. Please share your feedback in the comments section.

Previous
Next
comments powered by Disqus