How to secure MongoDB with authentication
Updated 7/20/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 connect an app to a remote database
Allow access to a database from another server, securely: bind address, dedicated user and firewall.
Updated 7/20/2026DatabasesHow to migrate a database between servers
Move a MySQL or PostgreSQL database to a new server with minimal downtime, using dump and restore.
Updated 7/20/2026DatabasesHow to automate database backups with cron
Schedule daily MySQL or PostgreSQL backups with cron, with automatic retention, and send them to a Backup Box.
Updated 7/20/2026