In this tutorial, we will show you how to install MongoDB on Debian 11. For those of you who didn’t know, MongoDB is a NoSQL database that is simple, object-oriented, scalable, and dynamic database. It is also called a NoSQL database because it does not rely on a traditional table-based relational database structure. It stores data in JSON format instead of the table style method. It can be integrated easily with various programming languages.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you through the step-by-step installation of the MongoDB NoSQL database on a Debian 11 (Bullseye).
Prerequisites
- A server running one of the following operating systems: Debian 10 or Debian 11.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install MongoDB on Debian 11 Bullseye
Step 1. Before running the tutorial below, it’s important to make sure your system is up to date by running the following apt
commands in the terminal:
sudo apt update sudo apt install curl apt-transport-https software-properties-common gnupg2
Now, install the necessary packages, including gnupg2 and wget, which will be used during the MongoDB installation process:
sudo apt install gnupg2 wget
Step 2. Installing MongoDB.
By default, MongoDB is not available on Debian 11 base repository, Now add the MongoDB repository to your Debian 11 system:
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main" | tee /etc/apt/sources.list.d/mongodb-org.list
Next, add the GPG key with the following command:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
After that, refresh APT and install MongoDB using the following command below:
sudo apt update sudo apt install mongodb-org
To check the version of MongoDB which is installed:
mongod --version
Let’s enable and also start the service of the Database server so that we don’t need to run it again and again with system boot:
sudo systemctl start mongod sudo systemctl enable mongod
You can check the status of the MongoDB service by running:
sudo systemctl status mongod
If the service is running, you should see an output similar to:
mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2023-05-26 10:00:00 UTC; 1min 30s ago Docs: https://docs.mongodb.org/manual Main PID: 12345 (mongod) CGroup: /system.slice/mongod.service └─12345 /usr/bin/mongod --config /etc/mongod.conf
To secure the MongoDB, launch the MongoDB:
mongo
Once you are connected, create a database named admin with the following command:
use admin
Then, create an admin user and set a password:
> db.createUser( { user: "ngadimin", pwd: "your-strong-passwd", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
To enable the security of MongoDB, open the configuration file of MongoDB:
nano /etc/mongod.conf
Add the following lines:
security: authorization: enabled
Save and close a file, then restart the MongoDB service to apply the changes:
sudo systemctl restart mongod
Verify MongoDB connection by running the following command to connect the MongoDB shell using the username and password:
mongo -u madmin -p
Congratulations! You have successfully installed MongoDB. Thanks for using this tutorial for installing the latest version of MongoDB 5 on the Debian system. For additional help or useful information, we recommend you check the official MongoDB website.