# How to install MongoDB on Debian 10

The popular document-based database management system [MongoDB](https://www.ionos.ca/digitalguide/websites/web-development/mongodb-an-introduction-and-comparison-to-mysql/ "MongoDB: an introduction and comparison to MySQL") can be installed under Linux distributions such as [Debian](https://www.ionos.ca/digitalguide/server/know-how/debian-the-universal-system-software/ "Debian – the universal system software") 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](https://www.ionos.ca/digitalguide/server/configuration/linux-commands-an-overview-of-terminal-commands/ "Linux commands: An overview of 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](https://www.ionos.ca/digitalguide/hosting/technical-matters/nosql/ "NoSQL") database’s installation won’t work.

---

### Note

You can install MongoDB not only under Debian, but basically under all **[Linux distributions](https://www.ionos.ca/digitalguide/server/configuration/linux-distributions/ "Linux distributions")**. However, if you want to [install MongoDB on Ubuntu](https://www.ionos.ca/digitalguide/websites/web-development/install-mongodb-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:

```none
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.

```none
sudo apt-get install gnupg
```

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

```none
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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
sudo systemctl start mongod
```

If the startup 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**:

```none
sudo systemctl daemon-reload
```

## Stop or restart MongoDB on Debian

Terminating MongoDB is possible with a simple terminal command:

```none
sudo systemctl stop mongod
```

The situation is similar with **a restart**:

```none
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:

```none
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:

```none
nano /etc/mongod.conf
```

### Add password protection

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

```none
security:
    authorization: enabled
```

Now save the changes and close the file. **Restarting MongoDB** ensures that the changes are applied and take effect:

```none
systemctl restart mongod
```

### Creat an admin

To create an admin, first start the MongoDB shell:

```none
mongo
```

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

```none
use admin
```

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

```none
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:

```none
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](https://www.ionos.ca/digitalguide/websites/web-development/mongodb-tutorial-first-steps/ "MongoDB tutorial: first steps") 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](https://www.ionos.ca/digitalguide/hosting/technical-matters/database-management-systems-dbms/ "Database Management Systems (DBMS)") after [comparing different databases](https://www.ionos.ca/digitalguide/hosting/technical-matters/open-source-databases-comparison/ "Open source databases comparison") 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:

```none
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:

```none
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:

```none
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
```


This is a markdown version of: [https://www.ionos.ca/digitalguide/websites/web-development/install-mongodb-debian/](https://www.ionos.ca/digitalguide/websites/web-development/install-mongodb-debian/) for AI/LLM consumption.