How to install Mongodb 5 in Rocky Linux/Alma Linux 8

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

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/Alma Linux 8 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/Alma Linux 8 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-5.0.repo

Then add this content to the file

[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 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-5.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-5.0.2 mongodb-org-database-5.0.2 mongodb-org-server-5.0.2 mongodb-org-shell-5.0.2 mongodb-org-mongos-5.0.2 mongodb-org-tools-5.0.2

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:

$ mongo -version
MongoDB shell version v5.0.3
Build Info: {
    "version": "5.0.3",
    "gitVersion": "657fea5a61a74d7a79df7aff8e4bcf0bc742b748",
    "openSSLVersion": "OpenSSL 1.1.1g FIPS  21 Apr 2020",
    "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 Wed 2021-11-03 07:39:58 UTC; 1min 6s ago
     Docs: https://docs.mongodb.org/manual
  Process: 108541 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 108539 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 108538 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 108536 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 108544 (mongod)
   Memory: 64.1M
   CGroup: /system.slice/mongod.service
           └─108544 /usr/bin/mongod -f /etc/mongod.conf

Nov 03 07:39:57 ip-10-2-40-54.us-west-2.compute.internal systemd[1]: Starting MongoDB Database Server...
Nov 03 07:39:57 ip-10-2-40-54.us-west-2.compute.internal mongod[108541]: about to fork child process, waiting until server is ready for connections.
Nov 03 07:39:57 ip-10-2-40-54.us-west-2.compute.internal mongod[108541]: forked process: 108544
Nov 03 07:39:58 ip-10-2-40-54.us-west-2.compute.internal mongod[108541]: child process started successfully, parent exiting
Nov 03 07:39:58 ip-10-2-40-54.us-west-2.compute.internal 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.

$ mongo --port 27017
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d7597e5b-5f24-4586-8bd4-6f47bdc059ca") }
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 server generated these startup warnings when booting:
        2021-11-03T07:39:58.477+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-11-03T07:39:58.478+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
        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()
---
> db.version()
5.0.3
>

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/Alma Linux 8 server.
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy