Choose a different version or distribution
Introduction
Before we begin talking about how to install and use MongoDB on Debian 11, let's briefly understand – What is MongoDB?
A popular NoSQL category and non-relational database type is MongoDB. The method used by SQL is not used by MongoDB to store and retrieve data. In contrast to conventional database management systems, MongoDB saves data in JSON documents. Unstructured data is handled by MongoDB, which also offers a structured method of managing the data.
A free and open-source database management system is MongoDB. It is thus appropriate for Linux-based operating systems. Any distribution that is based on the Linux kernel can be used to access the Linux systems. One of the most popular Linux distributions is Debian, from which dozens of other distributions have been derived.
In this tutorial, you will install and use MongoDB on Debian 11. We will also address a few FAQs on how to install and use MongoDB on Debian 11.
Advantages of MongoDB
- Scalability: MongoDB effortlessly scales horizontally, handling large amounts of data and high traffic loads effectively.
- Flexibility: Its flexible document-based structure allows easy schema changes, accommodating evolving needs without downtime.
- High Performance: MongoDB's optimized data storage and indexing enable fast query execution and real-time data processing.
- Replication & Failover: Automatic replication ensures data redundancy and high availability, preventing data loss and enhancing fault tolerance.
- Easy Integration: MongoDB's comprehensive toolset and robust APIs simplify integration with popular programming languages and frameworks.
Steps to install MongoDB on Debian 11 (Bullseye)
You can find the instructions needed to install MongoDB on your Debian 11 system here:
Step 1 : First, use the following command to import MongoDB's GPG key. APT needs the GPG key to verify the authenticity of Debian (or any other program) being installed. To achieve this, you can practice the command listed below to obtain the GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation
fix it by using the command: sudo apt install gnupg
Note : The most recent version of MongoDB is 5.0 at the time of installation. Any version's GPG key is available on the project website.
Step 2 : The Debian repository must be added to your machine.
For that, add the URL to the MongoDB repository to the source.list file of MongoDB in /etc/apt/sources.list.d/. The command listed below helped us obtain the file and add a link to MongoDB's repository:
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-5.0.list
Note : Access the most recent repository by going to repo.mongodb.org.
Step 3 : After completing the previous two steps, use the following command to update the Debian systems repository.
sudo apt update
And then run the following command to install MongoDB.
sudo apt install mongodb-org
Use the following command to check the installation after the package has been installed.
mongod --version
Step 4 : The mongodb service should be enabled, as it helps to make the service ready after each reboot. You can achieve this by running the next command.
sudo systemctl enable --now mongod
Use the command listed below to check the service's status.
sudo systemctl status mongod
How to use MongoDB on Debian 11
Some fundamental database operations are described in this section. With the aid of the following command, you may access the mongo shell and begin using MongoDB on Debian 11.
mongo
We are now on our way to perform the insert/retrieve/delete/update operations after opening the mongo console.
- Create a Database and Collection
To begin using MongoDB, you need a database and its collection: The first thing we did was create the linuxfoss database.
> use linuxfoss
Note : For instance, if the database already exists with the same name. The aforementioned command will then switch you to that database rather than making a duplicate of the current database.
In our linuxfoss database, the command supplied generates a collection called staff.
> db.createCollection("staff")
- Insert documents
To begin executing operations on the data in MongoDB, you must first have some data there. The following command will add two documents to the staff collection of the linuxfoss database because MongoDB stores data as documents.
> db.staff.insert([{_id:1, name:"John"}, {_id:2, name:"Alen"}])
- Retrieve documents
The MongoDB find() method is used to retrieve documents. We've had success using the following command to retrieve data from staff collection:
> db.staff.find()
- Update documents
Another fundamental action carried out on databases is update. The following query will update the name of the document whose id field value is equal to "1" because we are using staff (as a collection name) and linuxfoss (as a database name).
> db.staff.update({_id:1}, {$set :{name:"Sam"}})
Obtain the staff collection's content to see the difference for yourself.
> db.staff.find()
- Remove documents
MongoDB supports the remove() function for removal. The mongo command provided in the following line will remove the document of id=2:
> db.staff.remove({_id: 2})
MongoDB has a lot of functionality for handling documents. However, any database management system is built on the principles presented here.
How to Uninstall MongoDB from Debian 11
Although compared to conventional database management systems, MongoDB has special features. However, you can use the following command to uninstall MongoDB from your Debian system.
sudo apt remove mongodb-org
FAQs to Install and Use MongoDB on Debian 11
How do I start and stop the MongoDB service?
Start the service using sudo systemctl start mongod
and stop it with sudo systemctl stop mongod
. Use sudo systemctl enable mongod
to enable automatic startup on system boot.
How can I verify if MongoDB is running successfully?
Run sudo systemctl status mongod
to check the service status. If it's running, you'll see a confirmation message indicating that the service is active.
How can I access the MongoDB shell?
Open the terminal, then type mongo
to launch the MongoDB shell. Ensure that the MongoDB service is running before accessing the shell.
Where are the MongoDB configuration files located in Debian 11?
The main configuration file is typically located at /etc/mongod.conf
. Additional configuration files might be located in the /etc/mongod.conf.d
directory.
How do I create a new MongoDB database and user?
After accessing the MongoDB shell, use the use <database>
command to create a new database and db.createUser()
to create a new user with appropriate privileges.
How can I secure my MongoDB installation on Debian 11?
You can secure MongoDB by setting up authentication, enabling SSL/TLS encryption, restricting network access with firewall rules, and regularly applying security updates.
How can I configure MongoDB to run as a replica set on Debian 11?
Configure MongoDB to run as a replica set by modifying the /etc/mongod.conf
file, specifying the replSetName
option and restarting the MongoDB service.
Conclusion
Since it falls within the NoSQL database category, MongoDB has become a popular DBMS all around the world. They became popular because of the way MongoDB handles unstructured data and has the ability to store data and run a number of operations on it. In this tutorial, you learned how to set up and use MongoDB on Debian.
You can access the newest version of MongoDB on Debian by following the tutorial. Using the mongo shell, we have also carried out a number of fundamental database operations. It is advised that you explore MongoDB if you plan to select a database management system.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them.