How to Install MongoDB on Debian 11

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

How to install MongoDB on Debian 11. MongoDb or Mongo is a most popular open source NoSQL database. It does not rely upon a traditional table based relational database structure. It uses JSON based dynamic schemas which are editable anytime.

In this guide you are going to learn how to install and setup MongoDB on your Debian 11 server. You will also learn to configure remote connection to your Mongo database.

This setup is tested on Google Cloud, so it should work fine on other VPS, cloud servers running Debian 11.


  • A Debian 11 server with sudo access.

Initial Server Setup

Start by updating the server packages to the latest available.

sudo apt update
sudo apt dist-upgrade -y

Now you can proceed to install MongoDB.

Install MongoDB

Here we will install MongoDB Community Edition with LTS using the apt package managed. The current latest version of MongoDB at the time of this article is 5.0.5.

You may need to install gnupg for importing the key.

 sudo apt install gnupg

Import the public key using the following command.

wget -qO - | sudo apt-key add -

Add the MongoDB repository to the sources list. We will use the buster’s repo because by the time of writing this article, the MongoDB Community Edition does not have a separate repository for Debian Bullseye.

The Buster repository is in active development and compatible with Bullseye.

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

Update the packages and install MongoDB.

sudo apt update
sudo apt install -y mongodb-org

Once the installation is completed enable MongoDB to start at system startup.

sudo systemctl enable mongod

Start MongoDB server.

sudo service mongod start

You can view the status using the following command.

sudo service mongod status

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-02-18 09:11:56 UTC; 3m ago
   Main PID: 1942 (mongod)
     Memory: 164.5M
     CGroup: /system.slice/mongod.service
             └─1942 /usr/bin/mongod --config /etc/mongod.conf

Feb 18 09:11:56 staging systemd[1]: Started MongoDB Database Server.

Configure MongoDB

Now we can secure MongoDB, configure MongoDB to accept remote connections and also create a new database.

Secure MongoDB

Edit MongoDB config file.

sudo nano /etc/mongod.conf

Scroll down to the security section #security and uncomment it and enable authorization. The final edit should look as below.

  authorization: enabled

Enable Remote Connections

To enable remote connections you need to edit the same file and add your internal or private IP to the network interfaces. Your configuration should look like the one below.

  port: 27017

Replace with your IP address.

Open firewall if any for the port 27017.

Restart MongoDB.

sudo systemctl restart mongod

Confirm if MongoDB is allowing remote connections using the following command.

sudo lsof -i | grep mongo

You should receive an output similar to the one below.

mongod 1942 mongodb 11u IPv4 31550 0t0 TCP instance_name.c.project_id.internal:27017 (LISTEN)
mongod 1942 mongodb 12u IPv4 31551 0t0 TCP localhost:27017 (LISTEN)

Create MongoDB Admin User

Connect to MongoDB shell using mongosh command.


Change to admin database.

use admin

Create admin user with all privileges and setup password.

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

Enter password when prompted.

Enter exit to exit the shell.

Now you can use the following connection string to connect to MongoDB.


Prepare yourself for a role working as an Information Technology Professional with Linux operating system


Now you have learned how to install and setup MongoDB on Debian 11 and also configure remote connections.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

1 Comment

  1. Thanks for posting this article. When I am following your steps I got this error: E: Unable to locate package mongodb-org. It shows after the command: sudo apt install -y mongodb-org

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.