How to install MongoDB 5 on Rocky Linux

In this tutorial guide, we are going to learn how to install MongoDB 5.0 on Rocky Linux.

MongoDB is an open-source NoSQL database that provides high throughput for data-driven applications. Unlike relational databases such as MySQL, Oracle, and SQL servers which store data in tables according to a rigid schema, MongoDB stores data in documents with flexible schema.

Why do we need MongoDB?

  • MongoDB makes it easy for developers to store structured or unstructured data, it uses JSON-like format to store documents. This format directly maps to native objects in most programming languages, making natural database for most developers.
  • MongoDB is built on a scale-out architecture making popular for developers developing scalable applications with evolving data schemas.
  • MongoDB is available in every major public cloud provider such as Azure, AWS, GCP making it easy for developers to deploy to any cloud provider of choice.
  • MongoDB supports rapid iterative development where developers collaborate with larger teams.
  • In MongoDB records are stored as documents in compressed BSON files.

MongoDB documents can be retrieved in JSON formats which has many benefits such as:

  • It is human readable, makes it easy to be read
  • It is a natural form to store data
  • You can nest JSON to store complex data objects.
  • Documents maps to objects in most popular programming languages.
  • Structured and unstructured information can be stored in the same document
  • JSON has a flexible and dynamic schema, so adding fields or leaving a field out is not a problem.

Prerequisites

  • Have Rocky Linux server running
  • Have a user with sudo priviliges
  • MongoDB only supports 64 bits version of Linux, so make sure you are running x64.

Install MongoDB 5 on Rocky Linux

1. Run system updates

To begin with we need to make our system repositories up to date. This will help avoid running into errors while doing the installations.

$ sudo dnf update -y 

2. Configure package management (yum)

The easy way to install Mongo is to create /etc/yum.repos.d/mongodb-org-5.0.repo file so that we can easily install using the yum repository.

$ sudo vi /etc/yum.repos.d/mongodb-org-5.0.repo

Add the following content ;

[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

3. Install MongoDB 5 on Rocky Linux

Now we can install the latest version of MongoDB with the following

$ sudo dnf install mongodb-org -y

We will see the following output

MongoDB Repository                                                                              42 kB/s |  16 kB     00:00    
Dependencies resolved.
===============================================================================================================================
 Package                                Architecture Version                                       Repository             Size
===============================================================================================================================
Installing:
 mongodb-org                            x86_64       5.0.5-1.el8                                   mongodb-org-5.0        11 k
Installing dependencies:
 cyrus-sasl                             x86_64       2.1.27-5.el8                                  baseos                 95 k
 cyrus-sasl-gssapi                      x86_64       2.1.27-5.el8                                  baseos                 49 k
 cyrus-sasl-plain                       x86_64       2.1.27-5.el8                                  baseos                 46 k
 mongodb-database-tools                 x86_64       100.5.1-1                                     mongodb-org-5.0        47 M
 mongodb-mongosh                        x86_64       1.1.9-1.el7                                   mongodb-org-5.0        44 M
 mongodb-org-database                   x86_64       5.0.5-1.el8                                   mongodb-org-5.0        11 k
 mongodb-org-database-tools-extra       x86_64       5.0.5-1.el8                                   mongodb-org-5.0        16 k
 mongodb-org-mongos                     x86_64       5.0.5-1.el8                                   mongodb-org-5.0        19 M
 mongodb-org-server                     x86_64       5.0.5-1.el8                                   mongodb-org-5.0        28 M
 mongodb-org-shell                      x86_64       5.0.5-1.el8                                   mongodb-org-5.0        15 M
 mongodb-org-tools                      x86_64       5.0.5-1.el8                                   mongodb-org-5.0        11 k
 python3-pip                            noarch       9.0.3-20.el8.rocky.0                          appstream              19 k
 python3-setuptools                     noarch       39.2.0-6.el8                                  baseos                162 k
 python36                               x86_64       3.6.8-38.module+el8.5.0+671+195e4563          appstream              18 k
Enabling module streams:
 python36                                            3.6                                                                      

Transaction Summary
===============================================================================================================================
Install  15 Packages

Total download size: 153 M
Installed size: 565 M
Is this ok [y/N]: 
.....
Installed:
  cyrus-sasl-2.1.27-5.el8.x86_64                                
  cyrus-sasl-gssapi-2.1.27-5.el8.x86_64                        
  cyrus-sasl-plain-2.1.27-5.el8.x86_64                           
  mongodb-database-tools-100.5.1-1.x86_64                      
  mongodb-mongosh-1.1.9-1.el7.x86_64                             
  mongodb-org-5.0.5-1.el8.x86_64                               
  mongodb-org-database-5.0.5-1.el8.x86_64                        
  mongodb-org-database-tools-extra-5.0.5-1.el8.x86_64          
  mongodb-org-mongos-5.0.5-1.el8.x86_64                          
  mongodb-org-server-5.0.5-1.el8.x86_64                        
  mongodb-org-shell-5.0.5-1.el8.x86_64                           
  mongodb-org-tools-5.0.5-1.el8.x86_64                         
  python3-pip-9.0.3-20.el8.rocky.0.noarch                        
  python3-setuptools-39.2.0-6.el8.noarch                       
  python36-3.6.8-38.module+el8.5.0+671+195e4563.x86_64   

Lets check the version of MongoDB we have just installed.

$ mongod --version
db version v5.0.5
Build Info: {
    "version": "5.0.5",
    "gitVersion": "d65fd89df3fc039b5c55933c0f71d647a54510ae",
    "openSSLVersion": "OpenSSL 1.1.1k  FIPS 25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

5. Configure MongoDB 5

To start using MongoDB we need to start the MongoDB.service, to do that run the following command;

$ sudo systemctl start mongod

Then after starting you need to enable it to start every time you boot up your device.

$ sudo systemctl enable mongod

Lastly, you can check the status of our MongoDB service if it is running as expected.

$ sudo 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 Sun 2022-01-30 18:15:31 UTC; 38s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 38404 (mongod)
   Memory: 66.2M
   CGroup: /system.slice/mongod.service
           └─38404 /usr/bin/mongod -f /etc/mongod.conf

Jan 30 18:15:29 rockylinux systemd[1]: Starting MongoDB Database Server...
Jan 30 18:15:29 rockylinux mongod[38401]: about to fork child process, waiting until server is ready for connections.
Jan 30 18:15:29 rockylinux mongod[38401]: forked process: 38404
Jan 30 18:15:31 rockylinux mongod[38401]: child process started successfully, parent exiting
Jan 30 18:15:31 rockylinux systemd[1]: Started MongoDB Database Server.

Our MongoDB service is now running as expected.

To start using MongoDB type on your terminal mongosh, it will show something like this output;

$ mongosh 
Current Mongosh Log ID: 61f6d61a8cda0cc1b432fec6
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.1.9
Using MongoDB:          5.0.5
Using Mongosh:          1.1.9

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:
   2022-01-30T18:15:30.984+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2022-01-30T18:15:30.984+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
------

test> 

6. MongoDB crud operations

Crud operations include creating, reading, updating, and deleting.

To check the current database you are operating on type db

$ db 
test

Here am working on the test database.

To switch database you just say use db_name and it will create and switch to the database automatically.

$ use nextgentips

Insert into MongoDB database

MongoDB provides the following methods to insert documents into a collection.

# insert one 
$ db.collection.insertOne()
# insert many
$ db.collection.insertMany()

Example

nextgen> db.nextgen.insertOne([ { Name: 'Nextgentips', Niche: 'Linux Tech Blog' }])
{
  acknowledged: true,
  insertedId: ObjectId("61f6d75e80b3d76dd79c1018")
}

In order to select documents you can use the find statement like this

db.collection.find()

Example

db.nexgen.find()

7. Uninstall MongoDB 5

If you want to uninstall MongoDB, first you will have to stop the MongoDB running instance and then remove the packages

Stop MongoDB instance

$ sudo systemctl stop mongod

Remove MongoDB packages

To completely remove the packages run the following command;

$ sudo dnf erase $(rpm -qa | grep mongodb-org)

Lastly, remove data directories

# log
$ sudo rm -r /var/log/mongodb
# lib
$ sudo rm -r /var/lib/mongodb

Conclusion.

We have learned how to install MongoDB on Rocky Linux. I am glad you have enjoyed the tutorial. Feel free to comment in case you are faced with a challenge.

About Mason Kipward

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Mason Kipward →

Leave a Reply

Your email address will not be published.