How to install and configure MongoDB 5 on Debian 11

In this guide we are going to learn how to install MongoDB 5.0 Community Edition on an Debian 11 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 Debian 11 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. Starting and enabling MongoDB service
  4. Connecting to Mongodb and Executing some test commands
  5. 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 apt update
sudo apt upgrade -y

# 2. Installing MongoDB Community Edition

The Mongodb packages are not available in the default Debian repos. To add the repo, do the following:

Import the public key used by the package management system. From a terminal, issue the following command to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-5.0.asc:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

The operation should respond with an OK.

However, if you receive an error indicating that gnupg is not installed, you can install gnupg and its required libraries using the following command.

sudo apt-get install gnupg

Create the list file using the command for your version of Debian:

echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Issue the following command to reload the local package database:

sudo apt update

You can install either the latest stable version of MongoDB or a specific version of MongoDB

sudo apt install -y mongodb-org

To install a specific release, you must specify each component package individually along with the version number, as in the following example:

sudo apt-get install -y mongodb-org=5.0.4 mongodb-org-database=5.0.4 mongodb-org-server=5.0.4 mongodb-org-shell=5.0.4 mongodb-org-mongos=5.0.4 mongodb-org-tools=5.0.4

If you only install mongodb-org=5.0.4 and do not include the component packages, the latest version of each MongoDB package will be installed regardless of what version you specified.

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

$ mongo -version
MongoDB shell version v5.0.4
Build Info: {
    "version": "5.0.4",
    "gitVersion": "62a84ede3cc9a334e8bc82160714df71e7d3a29e",
    "openSSLVersion": "OpenSSL 1.1.1k  25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian10",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

# 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 (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-11-22 18:32:39 UTC; 11s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 8461 (mongod)
     Memory: 63.7M
        CPU: 820ms
     CGroup: /system.slice/mongod.service
             └─8461 /usr/bin/mongod --config /etc/mongod.conf

Nov 22 18:32:39 ip-10-2-40-167 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.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0b3f99c9-5fdc-448c-b313-05a287d29eac") }
MongoDB server version: 5.0.4
================
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.
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-22T18:32:39.769+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-11-22T18:32:40.639+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-11-22T18:32:40.639+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.4
>

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("619be2de1892ce4ee1459101"), "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 apt remove <meta charset="utf-8">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 Debian Server.

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