Verified guide
This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.
- OS
- Debian GNU/Linux 12.15 (bookworm) · x86_64
- Installed version
- MongoDB 8.0.34 (mongodb-org 8.0.34), mongosh 2.14.0
- Test server
- Microsoft Azure · 1 vCPU / 2 GB RAM / 30 GB SSD (Standard_F1als_v7, koreacentral, Debian:debian-12:12-gen2)
- Last checked
- 2026-10-26
Add the official MongoDB repository to a Debian 12 server and install MongoDB 8.0, create an admin user and a dedicated user for applications and turn on authentication, and then configure remote access and the firewall (ufw) so that only your PC can connect.
1. Requirements
- One Debian 12 server (x86_64, a CPU that supports AVX)
- SSH access with an account that has
sudoprivileges - The public IP address of the PC you will connect from (e.g.
203.0.113.10) - Docker on the PC you will use to check the connection (not needed if
mongoshis installed on your PC)
Every step in this guide was verified on a server created as a Microsoft Azure virtual machine (1 vCPU · 2 GB memory · Standard_F1als_v7 · Debian 12.15 · Korea Central).
According to the official MongoDB Production Notes, MongoDB 5.0 and later require a CPU that supports AVX instructions on x86_64 servers. The command below displays the number of CPU cores that support AVX. If it is 1 or more, you can install MongoDB. If it is 0, MongoDB cannot run on this server.
grep -c avx /proc/cpuinfo12. Add the official MongoDB repository
Install MongoDB from the official MongoDB repository (repo.mongodb.org). First, install gnupg and curl, which are used to save the repository signing key. gnupg was not installed on the Debian 12 server used to verify this guide.
sudo apt-get update
sudo apt-get install -y gnupg curlDownload and save the repository signing key for MongoDB 8.0. The signing key is used to check that the packages you download were made by MongoDB. If there is no output, it is working.
curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmorRegister the repository address. bookworm is the code name of Debian 12.
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.listdeb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 mainUpdate the package list and check the version that will be installed. If Candidate starts with 8.0., it is correct.
sudo apt-get updateapt-cache policy mongodb-org-server | head -4mongodb-org-server:
Installed: (none)
Candidate: 8.0.34
Version table:3. Install MongoDB 8.0
Installing the mongodb-org package installs the server (mongod), the connection tool (mongosh) and the backup tools (mongodump and others) together.
sudo apt-get install -y mongodb-orgCheck the installed version.
mongod --versiondb version v8.0.34mongosh --version2.14.04. Start the service and check its status
On Debian 12, the service does not start when the installation finishes, and it is not registered to start automatically when the server restarts either (disabled).
sudo systemctl status mongod --no-pager○ mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; preset: enabled)
Active: inactive (dead)
Docs: https://docs.mongodb.org/manualStart the service and register it to start automatically when the server restarts.
sudo systemctl start mongod
sudo systemctl enable mongodCreated symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.If active (running) is displayed, it is working.
sudo systemctl status mongod --no-pager● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; preset: enabled)
Active: active (running) since Mon 2026-10-26 06:59:49 UTC; 3s agoCheck which addresses MongoDB accepts connections on. Only 127.0.0.1:27017 is displayed, so for now you can connect only from the server itself.
sudo ss -tlnp | grep 27017LISTEN 0 4096 127.0.0.1:27017 0.0.0.0:* users:(("mongod",pid=1520,fd=9))5. Check the connection with mongosh
Connect to the server with mongosh and check the response. --eval is an option that runs only the one command written after it and then exits, and --quiet is an option that hides the connection messages. If { ok: 1 } is displayed, it is working.
mongosh --quiet --eval "db.runCommand({ ping: 1 })"{ ok: 1 }6. Create an admin user and turn on authentication
Right after installation, MongoDB lets anyone read and write all data without user authentication. Before you open remote access, create an admin user and turn on authentication. passwordPrompt() is a function that asks for the password instead of having you write it in the command. The password you enter is displayed as *.
mongosh --quiet admin --eval 'db.createUser({ user: "admin", pwd: passwordPrompt(), roles: [ "userAdminAnyDatabase", "readWriteAnyDatabase" ] })'Enter password
**************************{ ok: 1 }userAdminAnyDatabase: Can create users and grant permissions on every database.readWriteAnyDatabase: Can read and write data in every database.- Because this user is created in the
admindatabase, authenticate with--authenticationDatabase adminwhen you connect.
In the configuration file /etc/mongod.conf, replace the commented-out #security: with the authentication setting.
sudo sed -i "s/^#security:/security:\n authorization: enabled/" /etc/mongod.conf
grep -n -A1 "^security:" /etc/mongod.conf28:security:
29- authorization: enabledRestart MongoDB. If active is displayed, it is working.
sudo systemctl restart mongod
sudo systemctl is-active mongodactiveCommands are now refused if you connect without authentication.
mongosh --quiet --eval "show dbs"MongoServerError: Command listDatabases requires authenticationIf you give the user name with -u and add -p without a value, you are asked for the password. If the list of databases is displayed, authentication is working.
mongosh -u admin -p --authenticationDatabase admin --quiet --eval "show dbs"Enter password: **************************
admin 100.00 KiB
config 12.00 KiB
local 40.00 KiB7. Create a dedicated user for applications
It is safer for applications to connect as a dedicated user that can use only one database, instead of the admin user. Create appuser, which can only read and write the appdb database. You enter a password twice. The first is the admin password, and the second is the password for the new appuser.
mongosh -u admin -p --authenticationDatabase admin --quiet appdb --eval 'db.createUser({ user: "appuser", pwd: passwordPrompt(), roles: [ { role: "readWrite", db: "appdb" } ] })'Enter password: **************************
Enter password
********************{ ok: 1 }Connect as appuser, then store and read data. The /appdb at the end of the connection address is the database to connect to. Because appuser was created in this database, authentication also uses this database.
mongosh "mongodb://localhost:27017/appdb" -u appuser -p --quiet --eval 'db.items.insertOne({ name: "hello" })'Enter password: ********************
{
acknowledged: true,
insertedId: ObjectId('6adefa834426ef2e9b7f0a61')
}mongosh "mongodb://localhost:27017/appdb" -u appuser -p --quiet --eval "db.items.find()"Enter password: ********************
[ { _id: ObjectId('6adefa834426ef2e9b7f0a61'), name: 'hello' } ]Writing to a database that is not allowed is refused. If the error below is displayed, the permissions are applied as intended.
mongosh "mongodb://localhost:27017/appdb" -u appuser -p --quiet --eval 'db.getSiblingDB("otherdb").items.insertOne({ name: "hello" })'Enter password: ********************
MongoServerError: not authorized on otherdb to execute command { insert: "items", documents: [ { name: "hello", _id: ObjectId('6adefa87f6f5f274cbc4dff3') } ], ordered: true, lsid: { id: UUID("a486f90d-395d-4742-b9ea-3a932a5ebe5d") }, $db: "otherdb" }8. Configure the firewall (ufw)
No firewall program was installed on the server used to verify this guide. Install ufw.
sudo apt-get install -y ufwBefore you change the MongoDB settings, allow only your PC's IP to reach port 27017 in the firewall. Always allow SSH before you enable the firewall. If you enable the firewall without allowing SSH, your connection to the server is cut off.
sudo ufw allow OpenSSH
sudo ufw allow from 203.0.113.10 to any port 27017 proto tcp
sudo ufw --force enable
sudo ufw statusRules updated
Rules updated (v6)
Rules updated
Firewall is active and enabled on system startup
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
27017/tcp ALLOW 203.0.113.10
OpenSSH (v6) ALLOW Anywhere (v6)If you use your cloud provider's network firewall (such as an Azure network security group), you also need to allow port 27017 for your PC's IP there. This guide was verified with ports 22 and 27017 allowed only for the PC's IP in an Azure network security group.
9. Allow remote connections
The default settings allow only localhost connections. To allow remote connections, change bindIp in /etc/mongod.conf to 0.0.0.0. 0.0.0.0 means that MongoDB accepts connections on all of the server's IPv4 addresses.
sudo sed -i "s/^ bindIp: 127.0.0.1/ bindIp: 0.0.0.0/" /etc/mongod.conf
grep -n -A2 "^net:" /etc/mongod.conf19:net:
20- port: 27017
21- bindIp: 0.0.0.0Restart MongoDB and check that port 27017 is now open on external addresses as well. If you see 0.0.0.0:27017, it is working.
sudo systemctl restart mongodsudo ss -tlnp | grep 27017LISTEN 0 4096 0.0.0.0:27017 0.0.0.0:* users:(("mongod",pid=2524,fd=9))10. Check the connection from your PC
If mongosh isn't installed on your PC, you can connect by running the mongosh included in the official MongoDB image (mongo:8.0) with Docker. Replace 203.0.113.20 with the server's IP. Because the --rm option is added, the container is deleted automatically when the command finishes. -it is an option that attaches a terminal so that you can enter the password.
docker run --rm -it mongo:8.0 mongosh "mongodb://203.0.113.20:27017/appdb" -u appuser -p --quiet --eval "db.items.find()"Enter password: ********************
[ { _id: ObjectId('6adefa834426ef2e9b7f0a61'), name: 'hello' } ]If the data you stored in step 7 is displayed, remote access is working. Also check that commands are refused when you connect without a user.
docker run --rm -it mongo:8.0 mongosh "mongodb://203.0.113.20:27017/appdb" --quiet --eval "db.items.find()"MongoServerError: Command find requires authentication11. Troubleshooting
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
This means that you connected from inside the server, but MongoDB is not running. The service does not start right after installation. Check the status with sudo systemctl status mongod --no-pager and start it with sudo systemctl start mongod.
MongoNetworkError: connect ECONNREFUSED 203.0.113.20:27017
This means that you connected from your PC and the server refused the connection. You get this error if you connect remotely before changing bindIp. Check with grep -n -A2 "^net:" /etc/mongod.conf that it is bindIp: 0.0.0.0, and check that you ran sudo systemctl restart mongod after changing it.
MongoServerSelectionError: Socket 'connect' timed out after 30022ms (connectTimeoutMS: 30000)
This means that there was no response from the server, so the connection attempt was given up after 30 seconds. You get this error when the firewall is blocking port 27017. Check that sudo ufw status has the rule 27017/tcp ALLOW 203.0.113.10 and that the IP matches your PC's current public IP. Also check your cloud provider's network firewall.
MongoServerError: Command listDatabases requires authentication
This means that you ran a command without a user after turning on authentication. Connect with -u admin -p --authenticationDatabase admin (admin) or "mongodb://localhost:27017/appdb" -u appuser -p (dedicated user).
MongoServerError: Authentication failed.
This means that the user name, the password or the authentication database is wrong. admin authenticates against the admin database, and appuser against the appdb database.
MongoServerError: not authorized on otherdb to execute command …
This means that you used a database on which appuser has no permissions. This guide allows only reading and writing (readWrite) on appdb. If you need to use other databases as well, connect as the admin user and add permissions on that database to appuser.
12. Uninstall
First, delete the port 27017 rule you added to the firewall.
sudo ufw delete allow from 203.0.113.10 to any port 27017 proto tcpRule deletedStop MongoDB and delete the packages. Because this also deletes the data stored in /var/lib/mongodb, back up any data you need first.
sudo systemctl stop mongodsudo DEBIAN_FRONTEND=noninteractive apt-get purge -y "mongodb-org*"Only the packages that start with mongodb-org are deleted. mongosh and the backup tools that were installed with them remain.
dpkg -l | grep mongoii mongodb-database-tools 100.19.1 amd64 mongodb-database-tools package provides tools for working with the MongoDB server:
ii mongodb-mongosh 2.14.0 amd64 MongoDB Shell CLI REPL PackageDelete the two remaining packages as well. DEBIAN_FRONTEND=noninteractive is a setting that keeps confirmation prompts from appearing during removal.
sudo DEBIAN_FRONTEND=noninteractive apt-get purge -y mongodb-mongosh mongodb-database-toolsDelete the data and log folders.
sudo rm -rf /var/log/mongodb /var/lib/mongodbAlso delete the repository and signing key you added in step 2, then update the package list.
sudo rm /etc/apt/sources.list.d/mongodb-org-8.0.list /usr/share/keyrings/mongodb-server-8.0.gpg
sudo apt-get updateCheck that no MongoDB packages remain. If 0 is displayed, everything has been deleted.
dpkg -l | grep -c mongo0Was this guide helpful? Let us know if anything is wrong or out of date.
Request a correction