MongoDB is a popular open-source document database that is widely used in modern web applications. It is a document-oriented NoSQL database designed for high-volume data storage. In this article, we will walk you through the step-by-step process of installing MongoDB 5 on Debian 11 Bullseye.

Prerequisites

Before we begin with the installation, make sure you have a Debian 11 Bullseye server up and running. Additionally, ensure that you have root access or a user with sudo privileges. Let’s get started!

Updating the System and Installing Dependencies

First, we need to update the system and install the required dependencies for MongoDB. Open a terminal or SSH into your Debian server and run the following commands:

sudo apt update
sudo apt install dirmngr gnupg apt-transport-https software-properties-common ca-certificates curl

Adding the MongoDB GPG Key

Next, we need to add the MongoDB GPG key to our system. This key is used to verify the authenticity of the MongoDB packages. Run the following command to add the GPG key:

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

Enabling the MongoDB Repository

After adding the GPG key, we can enable the MongoDB repository. This repository contains the MongoDB packages for Debian. Run the following command to enable the repository:

sudo add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main'

Installing MongoDB

Now that we have added the repository, we can proceed with the installation of MongoDB. Run the following commands to update the package list and install MongoDB:

sudo apt update
sudo apt install mongodb-org

Starting MongoDB Service

Once the installation is complete, we can start the MongoDB service and enable it to start automatically after rebooting the system. Run the following commands to start and enable the MongoDB service:

sudo systemctl start mongod
sudo systemctl enable mongod

To check the status of the MongoDB service, use the following command:

sudo systemctl status mongod

If everything is set up correctly, you should see an output similar to the following:

● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-08-19 15:21:41 EDT; 1min 3s ago
Docs: https://docs.mongodb.org/manual
Main PID: 18003 (mongod)
Memory: 77.9M
CPU: 1.373s
CGroup: /system.slice/mongod.service
└─18003 /usr/bin/mongod --config /etc/mongod.conf

Verifying the Installation

To verify whether the installation has completed successfully, we can use the MongoDB shell. Run the following command:

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

If MongoDB is installed correctly, you should see output similar to the following:

MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a27e1b61-8661-451c-a155-10118ef1b887") }
MongoDB server version: 5.0.2
{ "authInfo" : { "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }

Creating an Administrative MongoDB User

To secure your MongoDB installation, it is recommended to create an administrative user. This user will have privileges to manage the database. Follow the steps below to create an administrative user:

  1. Access the MongoDB Shell by running the following command:
mongo
  1. Switch to the admin database:
use admin
  1. Create a new user and set the password for the user using the following command:
db.createUser({ user: "mongoAdmin", pwd: "SecurePassword", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

Make sure to replace “SecurePassword” with a strong password of your choice.

  1. Exit the MongoDB shell:
quit()

Testing the Administrative User

To test the changes and verify that the administrative user was created successfully, you can access the MongoDB shell using the created user. Run the following command:

mongo -u mongoAdmin -p --authenticationDatabase admin

You will be prompted to enter the password for the mongoAdmin user. After entering the password, you should see the MongoDB shell prompt.

To list the users and see if the created user is listed, use the following command:

show users

The output should display the details of the mongoAdmin user:

{
"_id" : "admin.mongoAdmin",
"userId" : UUID("773c7b28-9950-47b1-8b7d-19bb4e24ecab"),
"user" : "mongoAdmin",
"db" : "admin",
"roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ],
"mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ]
}

Congratulations! You have successfully installed MongoDB 5 on Debian 11 Bullseye and created an administrative user.

Conclusion

In this article, we have walked through the process of installing MongoDB 5 on Debian 11 Bullseye. We started by updating the system and installing the necessary dependencies. Then, we added the MongoDB GPG key and enabled the MongoDB repository. After that, we installed MongoDB and started the MongoDB service.

We also verified the installation by connecting to the MongoDB shell and created an administrative user to secure the database. Finally, we tested the administrative user by accessing the MongoDB shell using the created user.

MongoDB is a powerful and flexible database system that can handle high-volume data storage. With its document-oriented approach and scalability, it is a popular choice for modern web applications.

If you are looking for reliable and high-performance cloud hosting solutions, consider Shape.host. With their Linux SSD VPS services, you can enjoy the benefits of fast and secure hosting for your MongoDB and other applications.