MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.
Previously, we have discussed how to install MongoDB on Ubuntu , Mint , and CentOS.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Software Installation tasks on Debian.
In this context, we shall look into How to install MongoDB on the Debian system.
How to install MongoDB on Debian ?
MongoDB's latest release is not available in the official repositories of Debian. Therefore, we will first need to add the MongoDB repository to our system and then install it.
We are going to install the latest release of MongoDB that is 4.4.
Here are the steps for the installation of MongoDB on the Debian system.
1. Import MongoDB GPG key
Import the MongoDB GPG key to your system’s list of trusted keys.
Here is the command to do so:
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add –
If you receive a "gnupg is not installed" error when adding the GPG key to your system, then install "gnupg" as follows:
$ sudo apt-get install gnupg
After the gnupg is installed, again run the above command to add the GPG key.
2. Add MongoDB Repository
Add MongoDB repository to your system. Here is the command to do so:
$ echo "deb [ arch=amd64,arm64 ]http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
3. Update local package database
After adding the MongoDB repository to your system, update the local package database as follows:
$ sudo apt-get update
4. Install MongoDB
As the MongoDB repository and its GPG key have been added, now you can install MongoDB using the apt-get command as follows:
$ sudo apt-get install mongodb-org
You will be prompted with y/N, hit y. After this, the installation of MongoDB will be started.
5. Start MongoDB
After installation, MongoDB does not start automatically.
Therefore, you will have to manually start its service. Here is the command to start the MongoDB service:
$ sudo systemctl start mongod
After starting the MongoDB service, if you receive the service not found error, then first run the below command and after that try to start the service:
$ sudo systemctl daemon-reload
Now to check if the MongoDB service is started and running without any issues, run the below command:
$ sudo systemctl status mongod
You should see the active (running) status in the output.
If you want MongoDB to start automatically after a system reboot, run the below command:
$ sudo systemctl enable mongod
In some cases, you may need to stop the MongoDB service. Here is the command to do so:
$ sudo systemctl stop mongod
After making any configuration changes, you must restart the MongoDB service. Here is the command to do so:
$ sudo systemctl restart mongod
6. Connecting to MongoDB
Now to test if you can connect to the MongoDB server, run the below command:
This command will display the MongoDB version installed on your system.
How to uninstall MongoDB on Debian ?
There may come a time when you want to remove MongoDB from your system.
To do so, first stop the MongoDB service as follows:
$ sudo systemctl stop mongod
Then uninstall MongoDB as follows:
$ sudo apt-get purge mongodb-org*
You can also remove MongoDB databases and libraries.
Here are the commands to do so:
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb
[Need urgent assistance to install missing Packages on Debian Linux Server? We are available to help you. ]