On this short guide, we will learn how to install MongoDB version 5.0 Community Edition on Rocky Linux 8 operating system.
Introduction
Currently, the use of MongoDB in various applications has shown very rapid growth. Various applications that have adopted MongoDB include: fintech applications, various kinds of mobile apps, to heavy duty enterprise applications. In this article, we will discuss how to install MongoDB version 5.0 on the Rocky Linux 8 operating system.
MongoDB 5.0 Installation On Rocky Linux 8
There are any MongoDB Community Edition distribution for any platform, For Linux, OS X, and Windows. For Linux distribution, it has been provided MongoDB for various Linux systems, namely : Red Hat, SUSE, Amazon, Ubuntu and Debian.
Prerequisite
Before we go to MongoDB installation process on Rocky Linux 8, we need to prepare our environment as mentioned below :
- Rocky Linux 8 System with sufficient disk space (minimum of 10 GB)
- Non-root user with sudo privileges
The installation process will be consisting of several steps as mentioned below :
A more detailed explanation will be described in the following sub-chapters.
1. Enabling MongoDB repository
By default, MongoDB is not exist in the Rocky Linux 8 default repository, so we need to add it manually to the system. To perform this task, we will create a new file and using vi text editor as shown below.
$ sudo vi /etc/yum.repos.d/mongodb-org-5.0.repo
Output :
[ramansah@otodiginet ~]$ sudo vi /etc/yum.repos.d/mongodb-org-5.0.repo [sudo] password for ramansah: [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
2. Installing MongoDB
On this stage, we will install MongoDB 5.0 community edition by executing following command line :
$ sudo dnf -y update $ sudo vi /etc/yum.repos.d/mongodb-org-5.0.repo
Output :
[ramansah@otodiginet ~]$ sudo dnf install -y mongodb-org MongoDB Repository 28 kB/s | 11 kB 00:00 Dependencies resolved. =============================================================================================================== Package Architecture Version Repository Size =============================================================================================================== Installing: mongodb-org x86_64 5.0.3-1.el8 mongodb-org-5.0 11 k Installing dependencies: mongodb-database-tools x86_64 100.5.0-1 mongodb-org-5.0 47 M mongodb-mongosh x86_64 1.1.0-1.el7 mongodb-org-5.0 43 M mongodb-org-database x86_64 5.0.3-1.el8 mongodb-org-5.0 11 k mongodb-org-database-tools-extra x86_64 5.0.3-1.el8 mongodb-org-5.0 16 k mongodb-org-mongos x86_64 5.0.3-1.el8 mongodb-org-5.0 19 M mongodb-org-server x86_64 5.0.3-1.el8 mongodb-org-5.0 28 M mongodb-org-shell x86_64 5.0.3-1.el8 mongodb-org-5.0 15 M mongodb-org-tools x86_64 5.0.3-1.el8 mongodb-org-5.0 10 k Transaction Summary =============================================================================================================== Install 9 Packages Total download size: 152 M Installed size: 556 M Downloading Packages: (1/9): mongodb-org-5.0.3-1.el8.x86_64.rpm 24 kB/s | 11 kB 00:00 (2/9): mongodb-org-database-5.0.3-1.el8.x86_64.rpm 28 kB/s | 11 kB 00:00 (3/9): mongodb-org-database-tools-extra-5.0.3-1.el8.x86_64.rpm 49 kB/s | 16 kB 00:00 (4/9): mongodb-org-mongos-5.0.3-1.el8.x86_64.rpm 1.2 MB/s | 19 MB 00:16 (5/9): mongodb-mongosh-1.1.0.el8.x86_64.rpm 1.2 MB/s | 43 MB 00:35 (6/9): mongodb-org-server-5.0.3-1.el8.x86_64.rpm 1.1 MB/s | 28 MB 00:24 (7/9): mongodb-org-tools-5.0.3-1.el8.x86_64.rpm 25 kB/s | 10 kB 00:00 (8/9): mongodb-org-shell-5.0.3-1.el8.x86_64.rpm 1.3 MB/s | 15 MB 00:11 (9/9): mongodb-database-tools-100.5.0.x86_64.rpm 950 kB/s | 47 MB 00:50 --------------------------------------------------------------------------------------------------------------- Total 3.0 MB/s | 152 MB 00:50 warning: /var/cache/dnf/mongodb-org-5.0-85529b2afce6c228/packages/mongodb-database-tools-100.5.0.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID e2c63c11: NOKEY MongoDB Repository 1.4 kB/s | 1.6 kB 00:01 Importing GPG key 0xE2C63C11: Userid : "MongoDB 5.0 Release Signing Key <packaging@mongodb.com>" Fingerprint: F567 9A22 2C64 7C87 527C 2F8C B00A 0BD1 E2C6 3C11 From : https://www.mongodb.org/static/pgp/server-5.0.asc Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : mongodb-org-database-tools-extra-5.0.3-1.el8.x86_64 1/9 Installing : mongodb-org-shell-5.0.3-1.el8.x86_64 2/9 Installed: mongodb-database-tools-100.5.0-1.x86_64 mongodb-mongosh-1.1.0-1.el7.x86_64 mongodb-org-5.0.3-1.el8.x86_64 mongodb-org-database-5.0.3-1.el8.x86_64 mongodb-org-database-tools-extra-5.0.3-1.el8.x86_64 mongodb-org-mongos-5.0.3-1.el8.x86_64 mongodb-org-server-5.0.3-1.el8.x86_64 mongodb-org-shell-5.0.3-1.el8.x86_64 mongodb-org-tools-5.0.3-1.el8.x86_64 Complete!
3. Starting MongoDB Service
Once the installation is complete, then we will start MongoDB service by submitting following command line :
$ sudo systemctl start mongod $ sudo systemctl enable mongod $ sudo systemctl status mongod
Output :
[ramansah@otodiginet ~]$ sudo systemctl start mongod [ramansah@otodiginet ~]$ sudo systemctl enable mongod [ramansah@otodiginet ~]$ sudo systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2021-10-08 08:07:25 WIB; 13s ago Docs: https://docs.mongodb.org/manual Main PID: 48539 (mongod) Memory: 65.0M CGroup: /system.slice/mongod.service └─48539 /usr/bin/mongod -f /etc/mongod.conf Oct 08 08:07:24 otodiginet systemd[1]: Starting MongoDB Database Server... Oct 08 08:07:24 otodiginet mongod[48536]: about to fork child process, waiting until server is ready for conne> Oct 08 08:07:24 otodiginet mongod[48536]: forked process: 48539 Oct 08 08:07:25 otodiginet mongod[48536]: child process started successfully, parent exiting Oct 08 08:07:25 otodiginet systemd[1]: Started MongoDB Database Server.
4. Interacting With MongoDB
To interact with MongoDB, it provides a shell interface that allows us to access MongoDB shell. To perform this task we will run the following command from any terminal session on the server.
$ mongo
Output :
[ramansah@otodiginet ~]$ mongo MongoDB shell version v5.0.3 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("d73f147f-0587-4c8a-8950-27ae63682efb") } MongoDB server version: 5.0.3 ================ Warning: the "mongo" shell has been superseded by "mongosh", which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in an upcoming release. We recommend you begin using "mongosh". For installation instructions, see https://docs.mongodb.com/mongodb-shell/install/ ================ 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 --- ---
The shell command mongo has been replaced by mongosh since MongoDB verison 5.0.x. We will use the new shell as showing below.
$ mongosh
Output :
[ramansah@otodiginet ~]$ mongosh Current Mongosh Log ID: 615fa16408a1f7461833aba9 Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000 Using MongoDB: 5.0.3 Using Mongosh: 1.1.0 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: 2021-10-08T08:07:25.899+07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted 2021-10-08T08:07:25.899+07:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' ------ Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded. You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.
4.1 Using MongoDB
At this stage, we will try to use default database (admin) and create a new user. To perform this task, we will run the following command lines.
test> use admin switched to db admin admin> show dbs admin 41 kB config 61.4 kB local 41 kB admin> show collections system.version admin> admin> db.createUser( ... { ..... user: "digimbadmin", ..... pwd: "passwordkoe", ..... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] ..... } ... ) { ok: 1 } admin> show users [ { _id: 'admin.digimbadmin', userId: UUID("b295dc6b-660a-4196-8443-64b5e34d9555"), user: 'digimbadmin', db: 'admin', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ], mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ] } ] admin>
Conclusion
In this short tutorion, we have learnt how to install and configur MongoDB 5.0 on Rocky Linux 8. We also discussed how to start, enable, and check the status of the MongoDB service as well as using the Mongo Shell interface to run commands to interact with a MongoDB database. I hope this article will be helpful.