10 Steps to a Production-Ready MongoDB on RHEL Using Binaries
In this blog, we demonstrate how to set up a production-ready MongoDB installation on RHEL Linux using binaries. We will guide you through creating a custom user to manage the MongoDB cluster and exporting binaries as environmental variables for easy access to MongoDB utilities. Follow these steps to streamline your MongoDB setup and management process.
1. Create a custom group to manage the MongoDB cluster:-
groupadd mongodb_grp
2. Create a custom user to manage the MongoDB cluster and assign them to a group:-
useradd mongodb -g mongodb_grp
passwd mongodb
3. Create the data directory, setup folder, and log folder.
Note: You can create all these folders in your preferred path.
[root@localhost /]# mkdir mongodb
[root@localhost /]#
[root@localhost /]# cd mongodb/
[root@localhost mongodb]#
[root@localhost mongodb]# mkdir data
[root@localhost mongodb]#
[root@localhost mongodb]# mkdir log
[root@localhost mongodb]#
[root@localhost mongodb]# mkdir setup
[root@localhost mongodb]#
[root@localhost mongodb]# ll
total 0
drwxr-xr-x. 2 root root 6 May 19 10:29 data
drwxr-xr-x. 2 root root 6 May 19 10:29 log
drwxr-xr-x. 2 root root 6 May 19 10:29 setup
[root@localhost mongodb]#
4. Download and Extract the MongoDB server binaries.
Link :- https://www.mongodb.com/try/download/community
[root@localhost mongodb]# cd setup/
[root@localhost setup]#
[root@localhost setup]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.9.tgz
--2024-05-19 10:44:42-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.9.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 18.66.57.100, 18.66.57.20, 18.66.57.36, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|18.66.57.100|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 85411761 (81M) [application/gzip]
Saving to: ‘mongodb-linux-x86_64-rhel70-7.0.9.tgz’
100%[===================================================================================================================>] 85,411,761 12.3MB/s in 17s
2024-05-19 10:45:03 (4.83 MB/s) - ‘mongodb-linux-x86_64-rhel70-7.0.9.tgz’ saved [85411761/85411761]
[root@localhost setup]# ll
total 83412
-rw-r--r--. 1 root root 85411761 Apr 24 10:59 mongodb-linux-x86_64-rhel70-7.0.9.tgz
[root@localhost setup]#
[root@localhost setup]# tar -xvf mongodb-linux-x86_64-rhel70-7.0.9.tgz
mongodb-linux-x86_64-rhel70-7.0.9/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-7.0.9/MPL-2
mongodb-linux-x86_64-rhel70-7.0.9/README
mongodb-linux-x86_64-rhel70-7.0.9/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-7.0.9/bin/install_compass
mongodb-linux-x86_64-rhel70-7.0.9/bin/mongod
mongodb-linux-x86_64-rhel70-7.0.9/bin/mongos
[root@localhost setup]# ll
total 83412
drwxr-xr-x. 3 root root 100 May 19 10:45 mongodb-linux-x86_64-rhel70-7.0.9
-rw-r--r--. 1 root root 85411761 Apr 24 10:59 mongodb-linux-x86_64-rhel70-7.0.9.tgz
[root@localhost setup]#
[root@localhost setup]# cd mongodb-linux-x86_64-rhel70-7.0.9/
[root@localhost mongodb-linux-x86_64-rhel70-7.0.9]#
[root@localhost mongodb-linux-x86_64-rhel70-7.0.9]# ll
total 176
drwxr-xr-x. 2 root root 57 May 19 10:45 bin
-rw-r--r--. 1 root root 30608 Apr 24 08:50 LICENSE-Community.txt
-rw-r--r--. 1 root root 16726 Apr 24 08:50 MPL-2
-rw-r--r--. 1 root root 1978 Apr 24 08:50 README
-rw-r--r--. 1 root root 122512 Apr 24 08:50 THIRD-PARTY-NOTICES
[root@localhost mongodb-linux-x86_64-rhel70-7.0.9]# cd bin/
[root@localhost bin]#
[root@localhost bin]# ll
total 305688
-rwxr-xr-x. 1 root root 15195 Apr 24 08:50 install_compass
-rwxr-xr-x. 1 root root 182821600 Apr 24 10:26 mongod
-rwxr-xr-x. 1 root root 130180760 Apr 24 10:24 mongos
[root@localhost bin]#
5. Download and install the MongoDB shell.
Link:- https://www.mongodb.com/try/download/shell
[root@localhost setup]# wget https://downloads.mongodb.com/compass/mongodb-mongosh-2.2.6.x86_64.rpm
--2024-05-19 10:55:47-- https://downloads.mongodb.com/compass/mongodb-mongosh-2.2.6.x86_64.rpm
Resolving downloads.mongodb.com (downloads.mongodb.com)... 108.158.61.128, 108.158.61.98, 108.158.61.127, ...
Connecting to downloads.mongodb.com (downloads.mongodb.com)|108.158.61.128|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 58502916 (56M) [application/octet-stream]
Saving to: ‘mongodb-mongosh-2.2.6.x86_64.rpm’
100%[===================================================================================================================>] 58,502,916 11.5MB/s in 6.3s
2024-05-19 10:55:54 (8.84 MB/s) - ‘mongodb-mongosh-2.2.6.x86_64.rpm’ saved [58502916/58502916]
[root@localhost setup]# ll
total 140544
drwxr-xr-x. 3 root root 100 May 19 10:45 mongodb-linux-x86_64-rhel70-7.0.9
-rw-r--r--. 1 root root 85411761 Apr 24 10:59 mongodb-linux-x86_64-rhel70-7.0.9.tgz
-rw-r--r--. 1 root root 58502916 May 16 13:50 mongodb-mongosh-2.2.6.x86_64.rpm
[root@localhost setup]#
[root@localhost setup]# rpm -ivh mongodb-mongosh-2.2.6.x86_64.rpm
warning: mongodb-mongosh-2.2.6.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID d361cb16: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mongodb-mongosh-2.2.6-1.el8 ################################# [100%]
[root@localhost setup]#
6. Download and extract MongoDB tools binaries for your system
Link:- https://www.mongodb.com/try/download/database-tools
[root@localhost setup]# wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.9.4.tgz
--2024-05-19 11:04:06-- https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.9.4.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 18.66.57.61, 18.66.57.100, 18.66.57.36, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|18.66.57.61|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 61520536 (59M) [binary/octet-stream]
Saving to: ‘mongodb-database-tools-rhel70-x86_64-100.9.4.tgz’
100%[===================================================================================================================>] 61,520,536 12.1MB/s in 6.6s
2024-05-19 11:04:13 (8.94 MB/s) - ‘mongodb-database-tools-rhel70-x86_64-100.9.4.tgz’ saved [61520536/61520536]
[root@localhost setup]#
[root@localhost setup]# ll
total 200624
-rw-r--r--. 1 root root 61520536 Dec 7 08:19 mongodb-database-tools-rhel70-x86_64-100.9.4.tgz
drwxr-xr-x. 3 root root 100 May 19 10:45 mongodb-linux-x86_64-rhel70-7.0.9
-rw-r--r--. 1 root root 85411761 Apr 24 10:59 mongodb-linux-x86_64-rhel70-7.0.9.tgz
-rw-r--r--. 1 root root 58502916 May 16 13:50 mongodb-mongosh-2.2.6.x86_64.rpm
[root@localhost setup]#
[root@localhost setup]# tar -xvf mongodb-database-tools-rhel70-x86_64-100.9.4.tgz
mongodb-database-tools-rhel70-x86_64-100.9.4/LICENSE.md
mongodb-database-tools-rhel70-x86_64-100.9.4/README.md
mongodb-database-tools-rhel70-x86_64-100.9.4/THIRD-PARTY-NOTICES
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/bsondump
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongodump
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongoexport
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongofiles
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongoimport
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongorestore
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongostat
mongodb-database-tools-rhel70-x86_64-100.9.4/bin/mongotop
[root@localhost setup]#
[root@localhost setup]# ll
total 200624
drwxr-xr-x. 3 root root 79 May 19 11:09 mongodb-database-tools-rhel70-x86_64-100.9.4
-rw-r--r--. 1 root root 61520536 Dec 7 08:19 mongodb-database-tools-rhel70-x86_64-100.9.4.tgz
drwxr-xr-x. 3 root root 100 May 19 10:45 mongodb-linux-x86_64-rhel70-7.0.9
-rw-r--r--. 1 root root 85411761 Apr 24 10:59 mongodb-linux-x86_64-rhel70-7.0.9.tgz
-rw-r--r--. 1 root root 58502916 May 16 13:50 mongodb-mongosh-2.2.6.x86_64.rpm
[root@localhost setup]#
[root@localhost setup]# cd mongodb-database-tools-rhel70-x86_64-100.9.4
[root@localhost mongodb-database-tools-rhel70-x86_64-100.9.4]#
[root@localhost mongodb-database-tools-rhel70-x86_64-100.9.4]# ll
total 192
drwxr-xr-x. 2 root root 148 May 19 11:09 bin
-rw-r--r--. 1 root root 554 Dec 7 08:17 LICENSE.md
-rw-r--r--. 1 root root 3118 Dec 7 08:17 README.md
-rw-r--r--. 1 root root 184821 Dec 7 08:17 THIRD-PARTY-NOTICES
[root@localhost mongodb-database-tools-rhel70-x86_64-100.9.4]#
[root@localhost mongodb-database-tools-rhel70-x86_64-100.9.4]# cd bin/
[root@localhost bin]#
[root@localhost bin]# ll
total 123268
-rwxr-xr-x. 1 root root 13736336 Dec 7 08:17 bsondump
-rwxr-xr-x. 1 root root 16181400 Dec 7 08:17 mongodump
-rwxr-xr-x. 1 root root 15872400 Dec 7 08:17 mongoexport
-rwxr-xr-x. 1 root root 16720960 Dec 7 08:17 mongofiles
-rwxr-xr-x. 1 root root 16123928 Dec 7 08:17 mongoimport
-rwxr-xr-x. 1 root root 16512440 Dec 7 08:17 mongorestore
-rwxr-xr-x. 1 root root 15741584 Dec 7 08:17 mongostat
-rwxr-xr-x. 1 root root 15317064 Dec 7 08:17 mongotop
[root@localhost bin]#
6. Create the MongoDB configuration file in the /etc directory.
[root@localhost bin]# cd /etc/
[root@localhost etc]#
[root@localhost etc]# mkdir mongosetup
[root@localhost etc]#
[root@localhost etc]# cd mongosetup
[root@localhost mongosetup]#
[root@localhost mongosetup]# vi mongodb.conf
[root@localhost mongosetup]#
[root@localhost mongosetup]# ll
total 4
-rw-r--r--. 1 root root 240 May 19 11:18 mongodb.conf
[root@localhost mongosetup]#
configuration file:-
systemLog:
destination: file
path: /mongodb/log/mongod.log
logAppend: true
storage:
dbPath: /mongodb/data
directoryPerDB: true
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27017
Note: To authorize access to the database, add the following parameter to the configuration file and restart the cluster. Before performing this task, ensure you have created a root role user for your access in the database
security:
authorization: enabled
7. Export the MongoDB binaries as environmental variables to access the utilities through the command line.
[root@localhost profile.d]# cd /etc/profile.d/
[root@localhost profile.d]#
[root@localhost profile.d]# cd /
[root@localhost /]#
[root@localhost /]#
[root@localhost /]# cd /etc/profile.d/
[root@localhost profile.d]#
[root@localhost profile.d]# vi mongodb.sh
[root@localhost profile.d]#
[root@localhost profile.d]# cat mongodb.sh
export PATH=/mongodb/setup/mongodb-linux-x86_64-rhel70-7.0.9/bin:$PATH
export PATH=/mongodb/setup/mongodb-database-tools-rhel70-x86_64-100.9.4/bin:$PATH
[root@localhost profile.d]#
Note: Adjust the file permissions according to your requirements for accessing the utilities. Here, we set permissions for the file owner only.
[root@localhost profile.d]# chmod 750 mongodb.sh
[root@localhost profile.d]#
[root@localhost profile.d]# ll mongodb.sh
-rwxr-x---. 1 mongodb mongodb_grp 154 May 19 11:28 mongodb.sh
[root@localhost profile.d]#
8. Change the ownership of the files and folders to your specific user.
[root@localhost /]# chown mongodb:mongodb_grp -R /mongodb
[root@localhost /]#
[root@localhost /]# ll mongodb/
total 0
drwxr-xr-x. 2 mongodb mongodb_grp 6 May 19 10:29 data
drwxr-xr-x. 2 mongodb mongodb_grp 6 May 19 10:29 log
drwxr-xr-x. 4 mongodb mongodb_grp 240 May 19 11:09 setup
[root@localhost etc]# chown mongodb:mongodb_grp -R /etc/mongosetup/
[root@localhost etc]#
[root@localhost etc]# ll /etc/mongosetup/
total 4
-rw-r--r--. 1 mongodb mongodb_grp 240 May 19 11:18 mongodb.conf
[root@localhost etc]#
[root@localhost profile.d]# chown mongodb:mongodb_grp -R /etc/profile.d/mongodb.sh
[root@localhost profile.d]#
[root@localhost profile.d]# ll /etc/profile.d/mongodb.sh
-rwx------. 1 mongodb mongodb_grp 154 May 19 11:28 /etc/profile.d/mongodb.sh
[root@localhost profile.d]#
9. Start the mongod service using your specific user account
[root@localhost /]# su - mongodb
Last login: Sun May 19 11:46:54 PDT 2024 on pts/1
[mongodb@localhost ~]$
[mongodb@localhost ~]$
[mongodb@localhost ~]$ mongod -f /etc/mongosetup/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 59884
child process started successfully, parent exiting
[mongodb@localhost ~]$
Note: If the fork process encounters an error, reassign ownership recursively to all files and folders.
[mongodb@localhost ~]$ ps -ef | grep mongo
mongodb 59884 1 1 12:03 ? 00:00:04 mongod -f /etc/mongosetup/mongodb.conf
root 60160 9553 0 12:07 pts/1 00:00:00 su - mongodb
mongodb 60161 60160 0 12:07 pts/1 00:00:00 -bash
mongodb 60211 60161 0 12:07 pts/1 00:00:00 ps -ef
mongodb 60212 60161 0 12:07 pts/1 00:00:00 grep --color=auto mongo
[mongodb@localhost ~]$
10. Log in to the database by mongo shell
[mongodb@localhost ~]$ mongosh
Current Mongosh Log ID: 664a4e1e0bf665446ba26a12
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.6
Using MongoDB: 7.0.9
Using Mongosh: 2.2.6
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-05-19T12:03:08.179-07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-05-19T12:03:08.179-07:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
2024-05-19T12:03:08.179-07:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary version
2024-05-19T12:03:08.179-07:00: Soft rlimits for open file descriptors too low
------
Deprecation warnings:
- Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
test>
test> show dbs
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
test>
Command to stop mongodb cluster:-
[mongodb@localhost ~]$ mongod -f /etc/mongosetup/mongodb.conf --shutdown
Killing process with pid: 3659
[mongodb@localhost ~]$
In conclusion, setting up MongoDB on RHEL Linux using binaries is a straightforward process when broken down into manageable steps. By following this guide, you’ve created a production-ready MongoDB installation, established a custom user for cluster management, and configured environmental variables for easy access to MongoDB utilities. This setup ensures efficient management and scalability for your MongoDB deployments, paving the way for robust database performance.