Authentication and Access Control
How to secure a MongoDB instance in production environments.
This guide describes the essential steps to secure a MongoDB instance in production environments. Security should be a priority when deploying MongoDB to avoid unauthorized access, data leaks, or data loss.
Enable Authentication
Enable internal authentication to ensure only registered users can access the database.
Steps:
1
3
Using authentication
After enabling authentication (security.authorization: enabled), attempting to connect using the ms alias without proper credentials will fail:
mongodb@vm00:/home/mongodb/ [mdb01] ms
Current Mongosh Log ID: 68628382b3d1df2e6369e327
Connecting to: mongodb://127.0.0.1:27017/?tls=true&tlsCertificateKeyFile=%2Fu01%2Fapp%2Fmongodb%2Fadmin%2Fmdb01%2Fsecret%2Fmongo-x509%2Fclients%2FclientUser.pem&tlsCAFile=%2Fu01%2Fapp%2Fmongodb%2Fadmin%2Fmdb01%2Fsecret%2Fmongo-x509%2Fca%2Fca.pem&directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.6
Using MongoDB: 8.0.16
Using Mongosh: 2.5.6
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
test> use admin
switched to db admin
admin> show collections
MongoServerError[Unauthorized]: Command listCollections requires authenticationInstead, use the msp alias, which reads credentials from the previously created credential file:
DMK will then use the granted user credentials automatically.
Last updated