How to Install MongoDB on Debian 11

Install MongoDB on Debian 11

In this article, we will have explained the necessary steps to install MongoDB on Debian 11. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

MongoDB is a free and open-source cross-platform document database. The software is characterized as a NoSQL database, a tool for storing JSON, or even a Document Database with optional schemas. It is available for Windows, macOS, and Linux Operating Systems and is supported by both 32 and 64-bit architectures.

Install MongoDB on Debian 11

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

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

Step 2. Install MongoDB on the Debian system.

Installing MongoDB on your Debian system is straightforward, All you need to do is open a terminal and run the following command to add MongoDB APT repository:

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

Then, install the required MongoDB GPG key with the following command:

curl -sSL https://www.mongodb.org/static/pgp/server-5.0.asc -o mongoserver.asc
gpg --no-default-keyring --keyring ./mongo_key_temp.gpg --import ./mongoserver.asc
gpg --no-default-keyring --keyring ./mongo_key_temp.gpg --export > ./mongoserver_key.gpg
sudo mv mongoserver_key.gpg /etc/apt/trusted.gpg.d/

Next, update the repository cache and install the MongoDB using the following command:

sudo apt update
sudo apt install mongodb-org

Once the installation is completed, let’s enable and also start the service of the Database server:

sudo systemctl enable mongod --now
sudo systemctl start mongod

Verify the MongoDB version using the following command:

mongod --version

Step 3. Secure MongoDB.

I will need to create an admin user to perform the administrative task. First, connect to the MongoDB with the following command:

mongo

Now inside MongoDB shell, connect to admin database:

use admin

Next, create an admin user and set the password:

db.createUser(
   {
     user: "mongoadmin", 
     pwd: "mongopassword", 
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   }
 )

Now you can exit MongoDB with the following:

quit()

To connect to the MongoDB shell using the admin username and password, run the following command below:

mongo --port 27017 --authenticationDatabase "admin" -u "mongoadmin" -p

That’s all you need to do to install MongoDB on Debian (Bullseye). I hope you find this quick tip helpful. For further reading MongoDB open source NoSQL database on Debian’s system, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.