MongoDB
Users & Roles Commands
MongoDB provides flexible role-based access control. Learn to create users, assign built-in roles (read, readWrite, dbAdmin), create custom roles, and enable authentication.
16 commands
Pro Tips
Create admin users in the 'admin' database. Other users can be created in their target database.
Use built-in roles (read, readWrite, dbAdmin, userAdmin) before creating custom roles.
MongoDB 6.0+ uses SCRAM-SHA-256 by default. Ensure clients support it.
Common Mistakes
Create an admin user BEFORE enabling --auth, or you'll be locked out.
The localhost exception only works once. After creating the first user, all connections require auth.