How to install and configure MongoDB 6 on Rocky Linux/Alma Linux 9

In this guide we are going to learn how to install MongoDB 5.0 Community Edition on a Rocky Linux 9.

MongoDB is a cross-platform document-oriented NoSQL database program that uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

MongoDB was built for people building internet and business applications who need to evolve quickly and scale elegantly. Companies and development teams of all sizes use MongoDB for a wide variety of reasons.

Instead of storing data in tables of rows or columns like SQL databases, each record in a MongoDB database is a document described in BSON, a binary representation of the data. Applications can then retrieve this information in a JSON format.

Here’s a simple JSON document describing a historical figure.

{
  "_id": 1,
  "name": {
    "first": "Ada",
    "last": "Lovelace"
  },
  "title": "The First Programmer",
  "interests": ["mathematics", "programming"]
}

# Prerequisites

To follow along, ensure you have the following:

  • An up to date Rocky Linux 9 server
  • Knowledge of Linux Terminal
  • Access to the internet
  • Root access to the server or User with Sudo access

# Table of Content

  1. Ensuring the server is up to date
  2. Instaling MongoDB
  3. Allowing MongoDB port on firewall
  4. Starting and enabling MongoDB service
  5. Connecting to Mongodb and Executing some test commands
  6. Uninstall MongoDB Community Edition

# 1. Ensuring the server is up to date

Before proceeding, let us make sure that our serve repos and packages are in the latest versions by updating using this command:

sudo dnf -y update

# 2. Installing Mongodb

The Mongodb repos are not available in Rocky Linux 9 by default. Let us add the repo by creating the file /etc/yum.repos.d/mongodb-org-5.0.repo and adding the repo content. Use this command:

sudo vim /etc/yum.repos.d/mongodb-org-6.0.repo

Then add this content to the file

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

Update Yum cache index:

sudo dnf clean all
sudo dnf makecache

Now list available YUM repositories to confirm that our mongodb repo was added:

$ sudo dnf repolist
...
mongodb-org-6.0                        MongoDB Repository
...

To install the latest stable version of MongoDB, issue the following command:

sudo dnf install -y mongodb-org

Alternatively, to install a specific release of MongoDB, specify each component package individually and append the version number to the package name, as in the following example:

sudo yum install -y mongodb-org-6.0.0 mongodb-org-database-6.0.0 mongodb-org-server-6.0.0 mongodb-mongosh-6.0.0 mongodb-org-mongos-6.0.0 mongodb-org-tools-6.0.0

You can specify any available version of MongoDB. However yum upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the following exclude directive to your /etc/yum.conf file:

exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

You can check the version of MongoDB installed using the following command:

$ mongod -version
db version v6.0.0
Build Info: {
    "version": "6.0.0",
    "gitVersion": "e61bf27c2f6a83fed36e5a13c008a32d563babe2",
    "openSSLVersion": "OpenSSL 1.1.1k  25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

# 3. Allowing MongoDB Port on the firewall

If you have an active firewalld service on your server and would like MongoDB service to be accessible over the network, allow port 27017/tcp:

sudo firewall-cmd --add-port=27017/tcp --permanent
sudo firewall-cmd --reload

You can also limit access based on source address

sudo firewall-cmd --permanent --add-rich-rule "rule family="ipv4" \
source address="10.1.0.0/16" port protocol="tcp" port="27017" accept

# 4. Starting and Enabling MongoDB Service

Once the service has been installed, it will not be started by default. Start the mongodb service using this command:

sudo systemctl start mongod

Confirm that the service is up and running by checking its status:

$ 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 2022-08-12 05:38:45 UTC; 5s ago
       Docs: https://docs.mongodb.org/manual
    Process: 273611 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 273612 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 273613 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 273614 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 273616 (mongod)
     Memory: 78.1M
        CPU: 680ms
     CGroup: /system.slice/mongod.service
             └─273616 /usr/bin/mongod -f /etc/mongod.conf

Aug 12 05:38:44 local-kip-rocky9-srv systemd[1]: Starting MongoDB Database Server...
Aug 12 05:38:44 local-kip-rocky9-srv mongod[273614]: about to fork child process, waiting until server is ready for connections.
Aug 12 05:38:44 local-kip-rocky9-srv mongod[273616]: forked process: 273616
Aug 12 05:38:45 local-kip-rocky9-srv mongod[273614]: child process started successfully, parent exiting
Aug 12 05:38:45 local-kip-rocky9-srv systemd[1]: Started MongoDB Database Server.

Our mongodb service is up and running. Let us enable it to start on boot using this command:

sudo systemctl enable mongod

# 5. Connecting to Mongodb and Executing some test commands

Use the mongo command to connect to the mongo shell.

$ mongosh --port 27017
Current Mongosh Log ID:	62f5e7801846d267c9b3cf78
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.4
Using MongoDB:		6.0.0
Using Mongosh:		1.5.4

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-08-12T05:38:45.444+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2022-08-12T05:38:45.445+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2022-08-12T05:38:45.445+00:00: vm.max_map_count is too low
------

------
   Enable MongoDB's free cloud-based monitoring service, which will then receive and display
   metrics about your deployment (disk utilization, CPU, operation statistics, etc).

   The monitoring data will be available on a MongoDB website with a unique URL accessible to you
   and anyone you share the URL with. MongoDB may use this information to make product
   improvements and to suggest MongoDB products and deployment options to you.

   To enable free monitoring, run the following command: db.enableFreeMonitoring()
   To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------

test> db.version()
6.0.0
test>

Let us execute some test commands to confirm its proper workings:

> use citizix_db;
switched to db citizix_db
> db.users.save({
...     name: "etowett",
...     location: "Arctic Vault"
... });
WriteResult({ "nInserted" : 1 })
> db.users.find();
{ "_id" : ObjectId("61823fcb42aa0052d5722c32"), "name" : "etowett", "location" : "Arctic Vault" }
>

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs.

This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

The following section guides you through the necessary steps.

# Stop MongoDB

Stop the mongod process by issuing the following command:

sudo service mongod stop

# Remove Packages.

Remove any MongoDB packages that you had previously installed.

sudo zypper remove $(rpm -qa | grep mongodb-org)

# Remove Data Directories.

Remove MongoDB databases and log files.

sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongo

# Conclusion

In this guide, we managed to Install and do some basic operations with MongoDB on our Rocky Linux 9 server.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy