In this tutorial we will cover the steps to install MongoDB on Rocky Linux.
Introduction on MongoDB
- MongoDB is a document-oriented database, not a relational one.
- The primary reason for moving away from the relational model is to make scaling out easier, but there are some other advantages as well. A document-oriented database replaces the concept of a “row” with a more flexible model, the “document.”
- By allowing embedded documents and arrays, the document-oriented approach makes it possible to represent complex hierarchical relationships with a single record. This fits naturally into the way developers in modern object-oriented languages think about their data.
- MongoDB is a general-purpose database, so aside from creating, reading, updating, and deleting data, it provides most of the features you would expect from a database management system and many others that set it apart.
- Performance is a driving objective for MongoDB, and has shaped much of its design. It uses opportunistic locking in its WiredTiger storage engine to maximize concurrency and throughput. It uses as much RAM as it can as its cache and attempts to automatically choose the correct indexes for queries.
Lab environment
I have created two separate Virtual Machines running on Oracle Virtual Box. One of these VMs will act as a Server while the other will act as a Client.
OS: Rocky Linux release 8.4 (Green Obsidian)
Kernel: 4.18.0-305.10.2.el8_4.x86_64
MongoDB Version: 5.0
Server's IP Address: 10.100.20.181
Client's IP Address: 10.100.20.200
Step 1: Create the repository
Create a mongoDB repository file:
[root@rockylinux ~]# vim /etc/yum.repos.d/mongodb-org-5.0.repo
In the above, file copy and paste the below content and save.
[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
Update the repository:
[root@rockylinux ~]# yum update -y
In your repository list, you can now see the new mongoDB repo:
[root@rockylinux ~]# yum repolist
Output:
Step 2: Install MongoDB packages
To install the latest stable release of MongoDB, run the following command:
[root@rockylinux ~]# yum install -y mongodb-org
Alternatively, to install a specific release of MongoDB, specify the version number for each packages:
[root@rockylinux ~]# yum install -y mongodb-org-5.0.1 mongodb-org-database-5.0.1 mongodb-org-server-5.0.1 mongodb-org-shell-5.0.1 mongodb-org-mongos-5.0.1 mongodb-org-tools-5.0.1
To prevent the unintended upgrades with the upgrade of yum, we need to pin the package. To pin, Add the following package in exclude directive to /etc/yum.conf
:
[root@rockylinux ~]# cat >> /etc/yum.conf << EOF [root@rockylinux ~]# For pin these package: exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools EOF
By default, MongoDB runs using the mongod user account and uses the following default directories,
/var/lib/mongo (the data directory)
/var/log/mongodb (the log directory)
And the below configuation file.
/etc/mongod.conf
You can see the any find of changes, error, important message in /var/log/mongodb/mongod.log
file.
Step 3: Run MongoDB Instance
To Start MongoDB service:
[root@rockylinux ~]# systemctl start mongod
Verify that MongoDB has started successfully:
[root@rockylinux ~]# systemctl status mongod
You can check the output:
If you want to run MongoDB service start automatically at boot time, issue the following command:
[root@rockylinux ~]# systemctl enable mongod
Step 4: Connecting to MongoDB instance
Within the localhost
and default port 27017
, you can run the mongosh
command line tool, the MongoDB shell, to connect the instance:
[root@rockylinux ~]# mongosh
This is similar to:
[root@rockylinux ~]# mongosh "mongodb://localhost:27017"
The output will be like that:
Step 5: Enable Remote connection
By default, MongoDB is not allowed any remote connection. To enable it add associative remote clients IP as bindIP
in the /etc/mongod.conf
file:
[root@rockylinux ~]# vim /etc/mongod.conf
Find the below section:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
And turned it into the following section:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,10.100.20.181 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
Here, 10.100.20.181
is the IP address of MongoDB Server. Restart the service to take the effect:
[root@rockylinux ~]# systemctl restart mongod
Now check the remote connection from the client node:
[root@rockylinux ~]# mongosh "mongodb://10.100.20.181:27017"
Bind mongodb instance on a non-default port
You may choose to use a different port number for your mongodb instance. We will change the default port number form 27017
to 28015
(or any other as needed):
[root@rockylinux ~]# vim /etc/mongod.conf
[root@rockylinux ~]# network interfaces
net:
port: 27017
Change the upper section into the below one:
[root@rockylinux ~]# network interfaces
net:
port: 28015
Restart the service to activate the changes:
[root@rockylinux ~]# systemctl restart mongod
Step 6: Configure Firewall
Next we will allow the port to firewall, since we are using firewalld in our Rocky Linux setup so we will use the same to allow port 28015
:
[root@rockylinux ~]# firewall-cmd --permanent --add-port=28015/tcp [root@rockylinux ~]# firewall-cmd --reload
Step 7: Configure SELinux
Next we will also allow mongodb port on the SELinux
[root@rockylinux ~]# semanage port -a -t mongod_port_t -p tcp 28015
Check the connection from the client node:
[root@rockylinux ~]# mongosh "mongodb://10.100.20.181:28015"
Step 8: Uninstall MongoDB
This process will completely remove the MongoDB packages, the configuration files, the data directory and the log directory. So, before perform this step please backup all of your data.
Stop the mongodb process by issuing the following command:
[root@rockylinux ~]# systemctl disable mongod [root@rockylinux ~]# systemctl stop mongod
Remove Packages:
[root@rockylinux ~]# yum erase $(rpm -qa | grep mongodb-org)
Remove Data Directories:
[root@rockylinux ~]# rm -r /var/log/mongodb [root@rockylinux ~]# rm -r /var/lib/mongo
Summary
In this tutorial, we learned how to obtain the MongoDB software. We also discussed the version numbers, how to install and run MongoDB, and how to install and run its prerequisites on Rocky Linux. Next, we enabled firewall and SELinux to establish a connection to a database.