How to install Mongodb 5 on Fedora 34/35

In this guide we are going to learn how to install MongoDB 5.0 Community Edition on a Fedora 34/35 server.

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.

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 Fedora 34/35 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

MongoDB is not available in the default Fedora 34/35 repos. For this install, we will download the .rpm files directly from the MongoDB repository. Downloads are organized by Red Hat / CentOS version (e.g. 8), then MongoDB release version (e.g. 5.0), then architecture (e.g. x86_64).

Let us download the MongoDB server using this command:

curl -LO https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.3-1.el8.x86_64.rpm

Then install with dnf:

sudo dnf  install -y ./mongodb-org-server-5.0.3-1.el8.x86_64.rpm

Let us also install the MongoDB shell and install using these commands:

curl -LO https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/5.0/x86_64/RPMS/mongodb-org-shell-5.0.3-1.el8.x86_64.rpm

Then install:

sudo dnf install -y ./mongodb-org-shell-5.0.3-1.el8.x86_64.rpm

Finally let us download and install the Mongosh.

curl -LO https://repo.mongodb.org/yum/redhat/8Server/mongodb-org/5.0/x86_64/RPMS/mongodb-mongosh-1.1.1.el8.x86_64.rpm

Then install

sudo dnf install -y ./mongodb-mongosh-1.1.1.el8.x86_64.rpm

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.1l  FIPS 24 Aug 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

We can also check the version of the Mongosh:

$ mongosh -version
1.1.1

# 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 14:31:03 UTC; 21min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 74348 (mongod)
     Memory: 61.5M
        CPU: 4.278s
     CGroup: /system.slice/mongod.service
             └─74348 /usr/bin/mongod -f /etc/mongod.conf

Nov 03 14:31:02 new-cloud systemd[1]: Starting MongoDB Database Server...
Nov 03 14:31:02 new-cloud mongod[74346]: about to fork child process, waiting until server is ready for connections.
Nov 03 14:31:02 new-cloud mongod[74348]: forked process: 74348
Nov 03 14:31:03 new-cloud mongod[74346]: child process started successfully, parent exiting
Nov 03 14:31:03 new-cloud 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("3c093393-8154-44d4-97b5-7b02ddebd7bd") }
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-03T14:31:02.695+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-11-03T14:31:03.626+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        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("6182a27289aefc6a3f30d05a"), "name" : "etowett", "location" : "Arctic Vault" }

# 6. Uninstall MongoDB Community Edition

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 Fedora 34/35 Server.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy