The popular document-based database man­age­ment system MongoDB can be installed under Linux dis­tri­bu­tions such as Debian 10 in a few steps. The entire in­stall­a­tion process of MongoDB under Debian can be performed in the terminal.

What you need to install MongoDB on Debian

You don’t need much to install MongoDB on Debian 10. It will be enough to know the basic Linux-terminal commands. You will also need a current version of Debian. We will show you the in­stall­a­tion using Debian 10. Be sure to choose a 64-bit version of the operating system. Otherwise NoSQL database’s in­stall­a­tion won’t work.

Note

You can install MongoDB not only under Debian, but basically under all Linux dis­tri­bu­tions. However, if you want to install MongoDB on Ubuntu or another dis­tri­bu­tion, the process is slightly different.

Step-by-step in­struc­tions to install MongoDB on Debian

Step 1: Download MongoDB key

First, you need to download the MongoDB GPG open key. You might not have the required package installed on your system yet. As a first step, check if the gnupg program is already running on your system. You can use the following command for this:

dpkg --get-selections gnupg

If gnupg is not yet installed, also get it installed in the terminal by typing the code line below. Otherwise, you can skip this substep.

sudo apt-get install gnupg

You can get the MongoDB public key by entering the following command:

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

Step 2: Create a List-File

In the next step you create a list file suitable for your Debian version. Again, you can simply use the terminal for this:

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

In order for the MongoDB re­pos­it­ory to be added to your system, you will need to upgrade your system af­ter­wards. This process may take a few moments, this is perfectly normal depending on the number of updates required. The following command begins the updates:

sudo apt-get update

Step 3: Install MongoDB packages

This is when MongoDB is installed on Debian. The command you use to install MongoDB differs depending on the version. Normally, it is enough to install the current version of MongoDB:

sudo apt-get install -y mongodb-org
Note

Make sure you install the package called ‘mongodb-org’. This is the official MongoDB package. The Debian in­teg­rated package ‘mongodb’ cannot be installed with the in­struc­tions here.

If you want to install a specific version of MongoDB, the in­stall­a­tion command is different in that you need to detail the specific version number for each package. So, if you want to install MongoDB version 6.0.1 on your system, use the following command:

sudo apt-get install -y mongodb-org=6.0.1 mongodb-org-database=6.0.1 mongodb-org-server=6.0.1 mongodb-mongosh=6.0.1 mongodb-org-mongos=6.0.1 mongodb-org-tools=6.0.1

The in­stall­a­tion process may take a few moments. After it is complete, you’ll have suc­cess­fully installed MongoDB on Debian.

Start MongoDB service

To start MongoDB, you can use the following command:

sudo systemctl start mongod

If the start-up does not work and an error message appears, you may first need to run the following command to reload or restart your system’s con­fig­ur­a­tion files and units:

sudo systemctl daemon-reload
Tip: Managed MongoDB from IONOS

Managed MongoDB from IONOS enables you to con­cen­trate on the es­sen­tials. From in­stall­a­tion to operation and main­ten­ance work, IONOS makes sure you always get the best per­form­ance from your data banks.

Stop or restart MongoDB on Debian

Ter­min­at­ing MongoDB is possible with a simple terminal command:

sudo systemctl stop mongod

The situation is similar with a restart:

sudo systemctl restart mongod

To verify that the command you ran MongoDB in the correct state, you can view the status of MongoDB with the following command:

sudo systemctl status mongod

Add MongoDB Password

The MongoDB default settings do not include password pro­tec­tion. However, you can easily add this if needed with a few lines of code.

Open the con­fig­ur­a­tion file

The first step is to open the MongoDB con­fig­ur­a­tion file:

nano /etc/mongod.conf

Add password pro­tec­tion

You add password pro­tec­tion by adding the following lines of code to the MongoDB con­fig­ur­a­tion file:

security:
    authorization: enabled

Now save the changes and close the file. Re­start­ing MongoDB ensures that the changes are applied and take effect:

systemctl restart mongod

Creat an admin

To create an admin, first start the MongoDB shell:

mongo

Then you can create a new database with the name “admin”:

use admin

You can set the username and your password by creating a new admin user:

db.createUser(
   {
     user: "MyUsername", 
     pwd: "MyPassword", 
     roles: [ { role: "userAdmin", db: "admin" } ]
   }
 )

To exit the MongoDB shell, you can use the quit() command. To connect to the shell again af­ter­wards, you need your username and password and the following command:

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

You use the MongoDB Shell to interact with MongoDB. For example, you can create ad­di­tion­al users or assign different roles. Read our MongoDB tutorial for more tips on using the MongoDB shell.

Uninstall MongoDB on Debian

Of course, you can also uninstall MongoDB again. Un­in­stalling only makes sense if you have decided to use another DBMS after comparing different databases or if you no longer need any databases at all. This is because un­in­stalling MongoDB also removes all the databases you have created so far.

Step 1: Stop MongoDB service

First, stop MongoDB using the following command:

sudo service mongod stop

Step 2: Remove packages

Now remove all the packages you have installed for MongoDB on Debian. You can do that with this command:

sudo apt-get purge mongodb-org*

Step 3: Delete databases and log files

Finally, delete all databases created during your MongoDB usage as well as log files to com­pletely uninstall MongoDB on Debian:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Go to Main Menu