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.
- MongoDB Enterprise Server
- 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.
MongoDB 4.4
cat << EOF > /etc/yum.repos.d/mongodb.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOF
MongoDB 4.2
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, first create an admin user for daily operation.
Then, we need to enable access control by editing MongoDB’s configuration file.
vi /etc/mongod.conf
Add the below lines.
security:
authorization: enabled
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 Mon 2020-08-10 13:10:15 EDT; 11s ago Docs: https://docs.mongodb.org/manual Process: 3147 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS) Process: 3145 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 3143 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SU> Process: 3141 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 3149 (mongod) Memory: 61.1M CGroup: /system.slice/mongod.service └─3149 /usr/bin/mongod -f /etc/mongod.conf Aug 10 13:10:14 centos8.itzgeek.local systemd[1]: Starting MongoDB Database Server... Aug 10 13:10:14 centos8.itzgeek.local mongod[3147]: about to fork child process, waiting until serve> Aug 10 13:10:14 centos8.itzgeek.local mongod[3147]: forked process: 3149 Aug 10 13:10:15 centos8.itzgeek.local mongod[3147]: child process started successfully, parent exiti> Aug 10 13:10:15 centos8.itzgeek.local systemd[1]: Started MongoDB Database Server.
Check the MongoDB version.
mongod --version
Output: db version v4.4.0 Build Info: { "version": "4.4.0", "gitVersion": "563487e100c4215e2dce98d0af2a6a5a2d67c5cf", "openSSLVersion": "OpenSSL 1.1.1c FIPS 28 May 2019", "modules": [], "allocator": "tcmalloc", "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
Access MongoDB
Connect to MongoDB shell by entering the following command.
mongo
Output: MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("3ecdceaa-5050-41a3-96e3-8f821362523b") } MongoDB server version: 4.4.0 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums https://community.mongodb.com >
Conclusion
That’s all. I hope, by now, you have MongoDB on your CentOS 8 / RHEL 8 system.