How to secure MongoDB with authentication
Updated 11/29/2026
A MongoDB install without authentication is an easy target, especially if it is exposed to the internet. Enabling authentication takes a few minutes.
1. Create an admin user
mongosh
use admin
db.createUser({
user: "admin",
pwd: "strong_password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})
2. Enable authentication
In /etc/mongod.conf, add:
security:
authorization: enabled
systemctl restart mongod
3. Connect authenticated
mongosh -u admin -p --authenticationDatabase admin
4. Important rules
- Keep MongoDB bound to
127.0.0.1(inbindIp) unless you need remote access. - Create dedicated per-application users with limited roles (only
readWriteon the needed database). - Never expose port 27017 directly to the internet without a firewall and authentication.
Put this guide into practice on your own VPS
EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.
See pricingRelated tutorials
Databases
How to install and secure PostgreSQL on Ubuntu
Install the most advanced open-source database and configure it safely: password, scram-sha-256 authentication and controlled access.
Updated 11/29/2026DatabasesHow to install and secure MySQL on Ubuntu
Install MySQL or MariaDB on your VPS and secure it properly: root password, removing test accounts and controlled access.
Updated 11/29/2026DatabasesHow to create a MySQL user and grant privileges
Create dedicated MySQL users per application and grant exactly the privileges needed, step by step.
Updated 11/29/2026