
MongoDB is a document-based database management system that uses JSON (JavaScript Object Notation) to store its data. Its primary function is to provide flexible, fast, and scalable data storage. This system is often used for applications that require high performance and good scalability. MongoDB is open source software, meaning anyone can download, use, and modify it. This open source status makes it a favorable choice due to the community’s support and continuous updates to the system. In this article, we will show you how to install MongoDB on Debian 13.
Conventions
# – given commands should be executed with root privileges either directly as a root user or by use of sudo command
$ – given commands should be executed as a regular user
Table of Contents
Step 1. Install Dependencies
Debian 13 was released not long ago, but Debian 13 (Trixie) is still not listed on the MongoDB repository. Due to this, we can not install MongoDB using their official repository; instead, we can install MongoDB using Docker. We need to install some dependencies to proceed with installing MongoDB in our Debian 13 system. Let’s install them by running the command below.
# apt install ca-certificates curl gnupg -y
Step 2. Install Docker
In this step, we will install Docker from their repository. Before installing it, we need to add and configure the repository first to help us install MongoDB later.
# install -m 0755 -d /etc/apt/keyrings
# curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
# chmod a+r /etc/apt/keyrings/docker.asc
It’s time to add the repository to apt sources:
#echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
# apt update
We should be able to install Docker packages now. We will install the latest version.
# apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
After the packages are installed, we can check the version of Docker installed with the following command:
# docker --version
You will get the following output:
Docker version 28.3.3, build 980b856
Step 3. Add a System User
In this article, we will use a specific system user to run Docker. Let’s create a new user called ‘master’
# useradd -G docker,sudo -s /bin/bash -m -d /opt/master master
Now, let’s give the user a password
# passwd master
This user will be used to execute the commands in the next steps.
Step 4. Install MongoDB
Setting up MongoDB for a project can be a breeze with Docker Compose. This guide will walk you through the process, from creating a docker-compose.yml file and running MongoDB in a container. Let’s switch to the new system user shell now.
# su - master
Creating a MongoDB container with Docker Compose is simple with a docker-compose.yml file. Below is a basic example that lets you control the MongoDB version, set up persistent data storage, and add custom environment variables. Let’s create the docker-compose.yaml file for Debian 13 installation.
$ nano docker-compose.yaml
Paste the following into the file:
services:
mongo:
image: mongo:8.0
container_name: mongodb
restart: always
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: m0d1fyth15
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
driver: local
Make sure to replace ‘m0d1fyth15’ with a stronger password. Save the file, then exit from the editor.
Let’s break down the key parts of the Docker Compose configuration:
Service and Image
image: mongo:8.0: This line tells Docker to use the official MongoDB image, version 8.0, from Docker Hub.
container_name: mongodb: We're giving the container a specific, easy-to-remember name: mongodb.
Restart Policy
restart: always: This is a great feature. It ensures that if the MongoDB container ever stops for any reason, Docker will automatically restart it.
Ports
ports: "27017:27017": This maps port 27017 on your computer to port 27017 inside the container. It's how your applications connect to the database.
Environment Variables
environment: This section is used to set up key configuration details.
MONGO_INITDB_ROOT_USERNAME: Sets the username for the database administrator.
MONGO_INITDB_ROOT_PASSWORD: Sets the password for the database administrator.
Volumes
volumes: This is crucial for persistent data storage.
mongo-data: This volume saves the actual database data. If you delete and recreate the container, your data will still be safe.
Now, let’s execute the command below to complete the installation.
$ docker compose up -d
The command above will print an output like this on your screen:
master@debian13:/opt/master/mongodb$ docker compose up -d
[+] Running 9/9
✔ mongo Pulled 53.4s
✔ b71466b94f26 Pull complete 7.4s
✔ e701e7c9324a Pull complete 7.4s
✔ f1102db351d8 Pull complete 7.7s
✔ d713d16a8043 Pull complete 7.8s
✔ cb52df844465 Pull complete 7.9s
✔ 578438e6cda5 Pull complete 7.9s
✔ 0a07b2b41243 Pull complete 48.2s
✔ 91469349d2c2 Pull complete 48.2s
[+] Running 4/4
✔ Network mongodb_default Created 0.1s
✔ Volume "mongodb_mongo-data" Created 0.0s
✔ Volume "mongodb_mongo-config" Created 0.0s
✔ Container mongodb Started
That’s it! MongoDB has been running on port 27017.
Step 5. Access Mongodb
To manage the databases and everything else in MongoDB, we can run the command below to log in to the MongoDB shell. Make sure to replace m0d1fyth15 with your own password that matches the one you created in docker-compose.yaml file.
$ docker exec -it mongodb mongosh "mongodb://root:m0d1fyth15@localhost:27017"
Once logged in, we can create a new database.
use rosehostingdb;
The command above will create a new database called ‘rosehostingdb’. Now, we can create a collection and continue setting up MongoDB on Debian 13.
db.createCollection("blogposts")
Next, let’s create a user called ‘master’ with read and write roles on the rosehostingdb database.
db.createUser(
{
user: "master",
pwd: "m0d1fyth15",
roles: [ { role: "readWrite", db: "rosehostingdb" } ]
}
)
You can list the users created using the db.getUsers() method.
db.getUsers();
You will see an output similar to this:
rosehostingdb> db.getUsers();
{
users: [
{
_id: 'rosehostingdb.master',
userId: UUID('31df4200-ab5c-48b9-9f37-ead9fc0db806'),
user: 'master',
db: 'rosehostingdb',
roles: [ { role: 'readWrite', db: 'rosehostingdb' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
],
ok: 1
}
Alternatively, you can also run the command below to show the users connected to the database:
show users
Type ‘exit’ to exit from the MongoDB shell.
Congratulations
That’s it! You have successfully installed MongoDB on Debian 13.
If you are one of our web hosting customers and use our managed Debian Hosting, you don’t have to follow this tutorial and install MongoDB on Debian 13 yourself; our Linux admins will set up and configure a MongoDB VPS for you. They are available 24×7 and will take care of your request immediately, and all you need to do is to submit a ticket. Installing MongoDB is not just about the installation; we can help you optimize and harden your MongoDB installation if you have an active service with us.
PS. If you liked this post, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.