How to install MongoDB on Debian 10

The popular document-based database management system MongoDB can be installed under Linux distributions such as Debian 10 in a few steps. The entire installation 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 installation using Debian 10. Be sure to choose a 64-bit version of the operating system. Otherwise NoSQL database’s installation won’t work.

Note

You can install MongoDB not only under Debian, but basically under all Linux distributions. However, if you want to install MongoDB on Ubuntu or another distribution, the process is slightly different.

Step-by-step instructions 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 repository to be added to your system, you will need to upgrade your system afterwards. 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 integrated package ‘mongodb’ cannot be installed with the instructions here.

If you want to install a specific version of MongoDB, the installation 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 installation process may take a few moments. After it is complete, you’ll have successfully 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 configuration files and units:

sudo systemctl daemon-reload
Tip: Managed MongoDB from IONOS

Managed MongoDB from IONOS enables you to concentrate on the essentials. From installation to operation and maintenance work, IONOS makes sure you always get the best performance from your data banks.

Stop or restart MongoDB on Debian

Terminating 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 protection. However, you can easily add this if needed with a few lines of code.

Open the configuration file

The first step is to open the MongoDB configuration file:

nano /etc/mongod.conf

Add password protection

You add password protection by adding the following lines of code to the MongoDB configuration file:

security:
    authorization: enabled

Now save the changes and close the file. Restarting 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 afterwards, 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 additional 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. Uninstalling 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 uninstalling 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 completely uninstall MongoDB on Debian:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Page top