MongoDB is a free and open-source cross-platform document database. The software is characterized as a NoSQL database, a tool for storing JSON, or even a Document Database with optional schemas.
Some features and advantages include:
- Flexible document schemas
- Code-native data access
- Change-friendly design
- Powerful querying and analytics
- Easy horizontal scale-out
In the following tutorial, you will learn how to install MongoDB 5.0 latest Community edition on Debian 11 Bullseye Desktop or Server by importing the community repository and after installing securing the software.
Table of Contents
Install Required Packages
The first step is to install the dependencies needed during the installation, use the following command to install or check that they are present.
sudo apt install dirmngr gnupg apt-transport-https software-properties-common ca-certificates curl -y
Import MongoDB Repository
Before you install the latest Community Release of the MongoDB 5.0 series, you will need to install the required MongoDB GPG key with the following.
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Next, you will add the repository.
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
If you noticed the command had Buster instead of Bullseye, do not panic, the MongoDB Community Edition does not have a separate repository for Debian Bullseye. However, the continued work and development in Buster works and is compatible.
Before installing, it is a good idea to refresh your apt-cache.
sudo apt-get update
Install MongoDB 5.0
Now that you have imported the repository, you can install the MongoDB software. The installation will install the following.
The following packages will be installed as part of the MongoDB-org package.
- mongodb-org-server – The MongoDB server daemon.
- mongodb-org-mongos – The MongoDB ongos daemon.
- monodb-org-shell – The MongoDB shell.
- monodb-org-tools – Contains tools for importing, exporting, and other utilities.
In your terminal, use the following command to begin.
sudo apt install mongodb-org
Example output:
Type Y, then press the ENTER KEY to proceed and complete the installation.
To start and activate MongoDB, use the following command.
sudo systemctl enable mongod --now
Verify the installation by testing the database connection and version check with the following terminal command.
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Example output:
Check MongoDB 5.0 Service Status
Before you continue, it is advised to check the status of MongoDB even though you did a connection test to make sure everything is in working order.
systemctl status mongod
Example:
The following commands will provide systemd operations in managing the MongoDB service.
To start MongoDB:
sudo systemctl start mongod
To stop MongoDB:
sudo systemctl stop mongod
To enable MongoDB on system startup:
sudo systemctl enable mongod
To disable MongoDB on system startup:
sudo systemctl disable mongod
To restart the MongoDB service:
sudo systemctl restart mongod
Configure MongoDB Security
The next in configuring MongoDB is the need to modify the MongoDB configuration file named mongod.conf which is located in the /etc directory. You will uncomment the security section, or else any users on the system will access any database and perform any actions, leaving the databases more exposed to possible attacks.
Open the mongod.conf file using nano editor.
sudo nano /etc/mongod.conf
Next, uncomment the following line and add authorization: enabled as follows.
security:
authorization: enabled
Example:
Once complete, perform a restart of the MongoDB service using the following systemctl command.
sudo systemctl restart mongod
Create Admin User on MongoDB
By default on MongoDB, the admin account is insecure, which is why authorization was enabled. Now, you need to create an account to access the Admin database securely.
mongo
Now inside the MongoDB shell, connect to the admin database.
use admin
Example output:
switched to db admin
Next, type the following to create a new Mongo user account. For the tutorial, a user mongouser will be made.
db.createUser(
{
user: "mongouser",
pwd: "PASSWORDCHANGE",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Remember, change the pwd: “PASSWORDCHANGE”, do not just blindly copy this without editing first, and optionally rename the admin if you wish.
Example output if successful:
Now you can exit MongoDB with the following.
quit()
Next, test MongoDB with the new account you created connection status.
mongo --port 27017 --authenticationDatabase "admin" -u "mongouser" -p
Example output:
The above output states that the user mongouser has connected to the admin database. This is, of course, just a test. You can rename the databases and rename users as you would going forward.
To exit MongoDB with the following.
quit()
How to Update MongoDB
Updates are done with the standard terminal command sudo apt update and upgrade.
sudo apt update && sudo apt upgrade
New versions do come frequently, given you are using the Community Edition. Remember to keep a snapshot of your database if things break, especially in a production environment.
How to Remove (Uninstall) MongoDB
To remove MongoDB, use the following command.
sudo apt remove mongodb-org
This will remove MongoDB from your system. However, many packages will remain that are no longer needed. To remove everything, use the following command instead.
sudo apt autoremove mongodb-org
The next step is if you no longer want to use, for example, MongoDB 5.0 series repository, you can safely remove this using the following command.
sudo rm /etc/apt/sources.list.d/mongodb-org-5.0.list
Now you can install an alternative branch of the MongoDB repository if you wish.
Comments and Conclusion
One main benefit MongoDB has over MySQL is its ability to handle extensive unstructured data, and it is magically faster. The popularity of Mongo is rising and should be looked into for any practical development projects that require databases.