31.8 C
Texas
angeloma
Senior Writer and partner

How to install MongoDB on Debian 11

Hello, friends. Throughout this post, we will take you to learn how to install MongoDB on Debian 11.

MongoDB is the most representative of the databases known as NoSQL, an acronym for Not only SQL. It is document-oriented and multi-platform, which means that we can use it on many different systems.

In Mongo DB there are no predefined schemas, the keys and values of a document are of fixed types or sizes, in a document with a fixed schema the tasks to add or delete fields become easier and with great performance.

Data in Mongo DB is stored using BSON format structures, which is a specification similar to JSON so any modern text editor can edit it without any problems.

- Advertisement -

So MongoDB has positioned itself as one of the most solid alternatives to database managers like SQLite or MariaDB. So, let’s go for it.

Install MongoDB on Debian 11

For this post, we will use the MongoDB repository for Debian 10 which also works for this new Debian version. However, we are still waiting for the MongoDB developers to include Debian 11 in their repositories.

So, open a terminal and update the operating system completely.

sudo apt update
sudo apt upgrade

Before adding the MongoDB repository to the system, it is necessary to install a package called gnupg2.

sudo apt install gnupg2

After that, add the GPG key of the repository as follows

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

Now add the MongoDB repository to your system by running

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

Refresh APT to accept the new changes

sudo apt update

Next, install MongoDB on Debian 11 by running

sudo apt install mongodb-org
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 25 not upgraded.
Need to get 147 MB of archives.
After this operation, 464 MB of additional disk space will be used.
Do you want to continue? [Y/n]

After that, you can check the installed version using this command

mongod --version

Output:

db version v5.0.3
Build Info: {
    "version": "5.0.3",
    "gitVersion": "657fea5a61a74d7a79df7aff8e4bcf0bc742b748",
    "openSSLVersion": "OpenSSL 1.1.1k  25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian10",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Managing the MongoDB service

Once it is installed, you can manage its service just like any other service on the system.

To start MongoDB you have to run

sudo systemctl start mongod

It is usually a good idea to make it start with the system so that it is immediately available.

sudo systemctl enable --now mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

If you make changes to the MongoDB configuration then you will have to restart it to apply the changes.

sudo systemctl restart mongod

Finally, it is advisable to check the status of the service to see if everything is OK.

sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-10-09 18:51:39 CEST; 25s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 13759 (mongod)
     Memory: 67.3M
        CPU: 1.561s
     CGroup: /system.slice/mongod.service
             └─13759 /usr/bin/mongod --config /etc/mongod.conf

Oct 09 18:51:39 osradar systemd[1]: Started MongoDB Database Server.

Creating a new user for MongoDB

To access the MongoDB shell just run the command command

mongo
1.- MongoDB on Debian 11
1.- MongoDB on Debian 11

And once inside, it is recommended to create a new user different from the root.

use admin
db.createUser(
{
user: "osradar",
pwd: passwordPrompt(),
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
2.- Creating a new user for MongoDB
2.- Creating a new user for MongoDB

Copy and paste the following but change osradar to the name of the user you want.

Then you will have to define a password that has to be secure.

Conclusion

MongoDB is a tool to take into account if we want to develop applications that do not require a SQL database handler. Being open-source we can use it in many different situations with guaranteed performance and efficiency.

MongoDB website

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article