MongoDB is an open-source, document-oriented database management system that is widely used for storing and managing data. It is a popular choice for many applications because of its flexibility and scalability. In this article, we will show you how to install MongoDB on Debian 11 (Bullseye), the latest stable release of Debian.
Prerequisites
Before you begin, you need to have a server running Debian 11. You can use a physical server or a virtual private server (VPS) from a cloud provider. You will also need to have a user account with sudo
privileges.
Installing MongoDB on Debian 11
Add the MongoDB package repository to your system by running the following command:
sudo apt-get install gnupg
wget -qO - <https://www.mongodb.org/static/pgp/server-4.4.asc> | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] <https://repo.mongodb.org/apt/debian> buster/mongodb-org/4.4 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Update the package list and install the MongoDB package by running the following commands:
sudo apt-get update
sudo apt-get install -y mongodb-org
After the installation is complete, start the MongoDB service and enable it to start automatically on boot by running the following commands:
sudo systemctl start mongod
sudo systemctl enable mongod
To verify that the MongoDB service is running, run the following command:
sudo systemctl status mongod
You should see output similar to the following:
mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-12-07 19:18:14 UTC; 1min 29s ago
Docs: <https://docs.mongodb.org/manual>
Main PID: 19922 (mongod)
Tasks: 23 (limit: 4665)
CGroup: /system.slice/mongod.service
└─19922 /usr/bin/mongod --config /etc/mongod.conf
Dec 07 19:18:14 ubuntu systemd[1]: Started MongoDB Database Server.
By default, MongoDB listens on the local interface only, which means that you cannot connect to it from a remote host. To allow connections from other hosts, you need to edit the /etc/mongod.conf
file and uncomment the bindIp
line:
sudo nano /etc/mongod.conf
Then, change the line bindIp: 127.0.0.1
to bindIp: 0.0.0.0
. Save the file and exit the editor.
After making the changes, restart the MongoDB service to apply them:
sudo systemctl restart mongod
Connecting to MongoDB
To connect to the MongoDB server, you can use the mongo
shell, which is included in the mongodb-org
package. To start the mongo
shell, run the following command:
mongo
This will connect you to the MongoDB server and open the mongo
shell, where you can run various commands to manage and manipulate the data in your database. For example, you can create a new database by running the following command:
use mydb
This will create a new database named mydb
and switch to it. You can then create a new collection in the database by running the following command:
db.createCollection("users")
This will create a new collection named users
in the mydb
database. You can insert documents into the collection by running the following command:
db.users.insert({ name: "John Doe", age: 30 })
This will insert a new document into the users
collection. You can retrieve the documents from the collection by running the following command:
db.users.find()
This will return all the documents from the users
collection.
Conclusion
In this article, we have shown you how to install MongoDB on Debian 11. We have also demonstrated how to connect to the MongoDB server and perform some basic operations using the mongo
shell. Now that you have MongoDB installed and running on your server, you can start using it for your own applications and projects.