How to install Mongodb 5 in Opensuse Leap 15.3

In this guide we are going to learn how to install MongoDB 5.0 Community Edition on an OpenSUSE Leap 15.3 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.

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 OpenSUSE Leap 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 server repos and packages are in the latest versions by updating using this command:

sudo zypper ref
sudo zypper up -y

# 2. Installing MongoDB Community Edition

The Mongodb repos are not available in OpenSUSE Leap repos by default. To add the repo, do the following:

Import the MongoDB public key.

sudo rpm --import https://www.mongodb.org/static/pgp/server-5.0.asc

Add the repository so that you can install MongoDB. 

sudo zypper addrepo --gpgcheck "https://repo.mongodb.org/zypper/suse/15/mongodb-org/5.0/x86_64/" mongodb

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

sudo zypper -n install mongodb-org

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 zypper install 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 zypper upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the packages by running the following command:

sudo zypper addlock 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

Previous versions of MongoDB packages use a different repository location. Refer to the version of the documentation appropriate for your MongoDB version.

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.1d  10 Sep 2019",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "suse15",
        "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 Thu 2021-11-04 15:19:22 UTC; 6s ago
       Docs: https://docs.mongodb.org/manual
    Process: 7088 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 7090 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 7091 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
    Process: 7092 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
   Main PID: 7107 (mongod)
     CGroup: /system.slice/mongod.service
             └─7107 /usr/bin/mongod -f /etc/mongod.conf

Nov 04 15:19:21 ip-10-2-40-60 systemd[1]: Starting MongoDB Database Server...
Nov 04 15:19:21 ip-10-2-40-60 mongod[7092]: about to fork child process, waiting until server is ready for connections.
Nov 04 15:19:21 ip-10-2-40-60 mongod[7107]: forked process: 7107
Nov 04 15:19:22 ip-10-2-40-60 mongod[7092]: child process started successfully, parent exiting
Nov 04 15:19:22 ip-10-2-40-60 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("5d73c2e5-bedc-4f87-b1e3-6413336f3848") }
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-04T15:19:22.512+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-11-04T15:19:22.512+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("6183fa50c4eb614f02efac0b"), "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 OpenSUSE Leap server

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