How do you enable authentication and secure a self-managed MongoDB server?
IntermediateHands-onMongoDB
Answer
Short answer
Turn on security.authorization, create an admin user, bind only to private interfaces, enable TLS, use a keyFile or x.509 for replica set member authentication, restrict the port with a firewall, and keep MongoDB patched.
mongod.conf
net:
port: 27017
bindIp: 127.0.0.1,10.0.21.10 # private IPs only
tls:
mode: requireTLS
certificateKeyFile: /etc/mongo/server.pem
CAFile: /etc/mongo/ca.pem
security:
authorization: enabled
keyFile: /etc/mongo/keyfile # replica set members authenticate to each other
replication:
replSetName: rs0
openssl rand -base64 756 > /etc/mongo/keyfile && chmod 400 /etc/mongo/keyfile && chown mongodb: /etc/mongo/keyfile
Checklist
- ✅ Auth on, admin user created, apps use least-privilege users.
- ✅ No public exposure: security group allows 27017 only from app subnets.
- ✅ TLS for clients and members.
- ✅ Audit logs (Enterprise / Percona), backups encrypted.
- ❌ Never
bindIp: 0.0.0.0with authorization disabled — that's how thousands of databases were wiped by ransom bots.
What to say in the interview
"Enable authorization and least-privilege users, bind to private interfaces, require TLS, use a keyFile or x.509 for internal member auth, lock the port to the app tier and keep it patched. An open, unauthenticated MongoDB is one of the most common real-world breaches."