MongoDB is a popular open-source NoSQL document database designed for applications that require flexible data models, scalability, and high-performance data storage. Unlike traditional relational databases, MongoDB stores data in BSON documents, allowing developers to work with flexible JSON-like structures.
In this tutorial, we will install MongoDB 9.0 Community Edition on Debian 13 (Trixie) using the official MongoDB APT repository. We will also configure the MongoDB service, verify the installation, enable authentication, and discuss basic security recommendations for production environments.
1. Prerequisites
Before installing MongoDB, make sure your server meets the following requirements:
- Debian 13 (Trixie).
- 64-bit x86_64 architecture.
- A user account with sudo privileges.
- Internet connectivity to access MongoDB repositories.
MongoDB 9.0 Community Edition officially supports Debian 13 on x86_64 architecture.
Check your Debian version:
cat /etc/os-release
Check your server architecture:
dpkg --print-architectureCode language: PHP (php)
Expected architecture:
amd64
Note: MongoDB 9.0 is the appropriate version for Debian 13. MongoDB 8.0 Community Edition officially supports Debian 12, not Debian 13.
2. Add the Official MongoDB Repository
Debian’s default repositories may contain MongoDB-related packages, but for production environments, I recommend using the official MongoDB repository maintained by MongoDB Inc.
First, update the package index and install the required dependencies:
sudo apt update
sudo apt install -y curl gnupg
Import the MongoDB GPG Key
Download and import the official MongoDB 9.0 signing key:
curl -fsSL https://pgp.mongodb.com/server-9.asc | \
sudo gpg --dearmor \
-o /usr/share/keyrings/mongodb-server-9.gpgCode language: JavaScript (javascript)
This key allows APT to verify that packages downloaded from the MongoDB repository are properly signed.
Add the Debian 13 Repository
Create the repository configuration:
echo "deb [ arch=amd64 signed-by=/usr/share/keyrings/mongodb-server-9.gpg ] https://repo.mongodb.org/apt/debian trixie/mongodb-org/9.0 main" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-9.0.listCode language: PHP (php)
Update the package index:
sudo apt update
At this point, your Debian server should be able to retrieve MongoDB packages from the official repository.
3. Install MongoDB 9.0
Install MongoDB Community Edition using APT:
sudo apt install -y mongodb-org
The mongodb-org metapackage installs the MongoDB server and its associated components, including MongoDB Shell and database tools.
The main packages include:
mongodb-org-server: MongoDB database server.mongodb-org-mongos: MongoDB query router.mongodb-org-tools: Database administration tools.mongodb-mongosh: MongoDB interactive shell.
Using the official APT repository also makes future patch updates easier to manage.
4. Start and Enable MongoDB
After installation, reload the systemd configuration and start the MongoDB service:
sudo systemctl daemon-reload
sudo systemctl enable --now mongod
Check the MongoDB service status:
sudo systemctl status mongod
You should see:
Active: active (running)Code language: HTTP (http)
The enable option ensures that MongoDB starts automatically whenever the server boots.
5. Verify MongoDB Installation
Check the installed MongoDB server version:
mongod --version
Check the MongoDB Shell version:
mongosh --version
Connect to the local MongoDB instance:
mongosh
Execute a ping command to verify that MongoDB is responding:
db.runCommand({ ping: 1 })Code language: CSS (css)
Expected result:
{ ok: 1 }Code language: CSS (css)
If the command returns ok: 1, MongoDB is running and accepting local connections.
Exit the MongoDB shell:
exitCode language: PHP (php)
6. Enable MongoDB Authentication
For production environments, enabling authentication is strongly recommended.
MongoDB does not enable access control by default. Without authentication, clients that can reach the database may be able to access or modify its data.
First, connect to MongoDB locally:
mongosh
Switch to the administrative database:
use adminCode language: PHP (php)
Create an administrative user:
db.createUser({
user: "mongoAdmin",
pwd: passwordPrompt(),
roles: [
{ role: "root", db: "admin" }
]
})Code language: CSS (css)
MongoDB will prompt you to enter the password without displaying it.
Next, exit the MongoDB shell:
exitCode language: PHP (php)
Edit the MongoDB configuration:
sudo nano /etc/mongod.conf
Find the security section or add the following configuration at the top level:
security:
authorization: enabled
Save the configuration and restart MongoDB:
sudo systemctl restart mongod
Test authentication:
mongosh -u mongoAdmin -p --authenticationDatabase admin
Enter the administrative password when prompted.
You should now be authenticated and able to execute administrative commands.
7. MongoDB Default Configuration
MongoDB uses /etc/mongod.conf as its primary configuration file.
Here are some important default configuration locations and settings:
| Configuration | Default value |
|---|---|
| Service name | mongod |
| Default port | 27017 |
| Bind IP | 127.0.0.1 |
| Configuration file | /etc/mongod.conf |
| Data directory | /var/lib/mongodb |
| Log directory | /var/log/mongodb |
| Process user | mongodb |
You can inspect the listening port with:
sudo ss -lntp | grep 27017
By default, MongoDB listens only on localhost (127.0.0.1). This is a good security starting point because external clients cannot connect directly to the database.
8. Configure Remote Access (Optional)
If your application runs on a different server, you may need to allow remote MongoDB connections.
Edit the configuration:
sudo nano /etc/mongod.conf
Find the net section:
net:
port: 27017
bindIp: 127.0.0.1Code language: CSS (css)
For example, if your MongoDB server has a private IP address of 10.10.10.20, configure:
net:
port: 27017
bindIp: 127.0.0.1,10.10.10.20Code language: CSS (css)
Restart MongoDB:
sudo systemctl restart mongod
Verify the listening interfaces:
sudo ss -lntp | grep 27017
Important security considerations:
- Enable MongoDB authentication before allowing remote access.
- Never expose MongoDB port 27017 directly to the public internet.
- Restrict connections using your firewall.
- Allow only trusted application servers and private network addresses.
- Consider using TLS for connections across untrusted networks.
9. Useful MongoDB Systemd Commands
Here are some useful commands for managing MongoDB on Debian 13.
Check service status:
sudo systemctl status mongod
Start MongoDB:
sudo systemctl start mongod
Stop MongoDB:
sudo systemctl stop mongod
Restart MongoDB:
sudo systemctl restart mongod
Enable automatic startup:
sudo systemctl enable mongod
Disable automatic startup:
sudo systemctl disable mongod
View MongoDB logs:
sudo journalctl -u mongod -f
Alternatively, inspect the MongoDB log file:
sudo tail -f /var/log/mongodb/mongod.logCode language: JavaScript (javascript)
10. Keep MongoDB Updated
Because MongoDB was installed using its official APT repository, you can use Debian’s standard package management workflow to receive MongoDB updates.
Check available package updates:
sudo apt update
apt list --upgradable | grep mongodbCode language: PHP (php)
Upgrade MongoDB packages:
sudo apt upgrade
For production environments, I recommend testing MongoDB upgrades in a staging environment before applying them to production servers.
You should also maintain regular database backups and verify that your backup restoration process works.
Conclusion
Installing MongoDB 9.0 on Debian 13 is straightforward when using the official MongoDB APT repository.
Using the official repository provides a convenient way to install MongoDB, manage its systemd service, and receive future package updates through APT.
For production deployments, enabling authentication, restricting network access, implementing regular backups, and monitoring database performance are essential steps.
With MongoDB successfully installed, Debian 13 is ready to host applications that require a flexible NoSQL database.