DevOps
Creating MongoDB Users: A Quick Setup Guide π
Learn how to create and configure MongoDB admin users in 5 minutes. Includes security tips, role explanations, and troubleshooting steps for database administration.

Hey there, tech enthusiast!
Today, we'll learn how to create a new user in MongoDB with admin privileges. This is a crucial step for database security, so let's make it happen!
β±οΈ Estimated time: 5 minutes
Before We Start π
Make sure:
- MongoDB is installed and running
- You have access to the terminal
- MongoDB service is active
Pro tip: Always use strong passwords in production environments!
Let's Create a MongoDB User π€
- First, connect to MongoDB shell:
mongosh
π‘
Note: You're good to go if you see a connection message!
- Switch to admin database:
use admin
π‘
A quick tip: The 'admin' database stores user information by default.
- Create your new user π
db.createUser({
user: "root",
pwd: "password",
roles: [ "userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase" ]
})
Let's break down those roles π
The roles we're assigning here are pretty powerful:
- userAdminAnyDatabase: Can manage users for any database
- dbAdminAnyDatabase: Can perform admin tasks on all databases
- readWriteAnyDatabase: Can read and write to any database
β οΈ Security Note:
Remember to:
- Change "password" to a strong password
- Store credentials securely
- Never share your admin passwords
- Exit the MongoDB shell:
quit
Verification Step β
You can test your new user by logging in with these credentials.
Common Issues & Solutions π§
- If you get an "Authentication failed" error:
- Double-check your password
- Ensure you're using the correct database
- If you can't create a user:
- Verify you're in the admin database
- Check if you have sufficient privileges
Additional Tips π‘
- Keep your passwords complex and secure
- Document your user credentials safely
- Regular audit of user access is recommended
- Consider using environment variables for passwords in production
β±οΈ That's it! In just about 5 minutes, you've successfully created a MongoDB admin user.
Need to add more users or modify roles? Drop a comment below, and I'll help you out! Happy database managing! π
Pro tip: Save these commands somewhere safe - you might need them again for future database setups!
Written by Shashikant Dwivedi
Engineer, occasional writer, full-time noticer. Based in Prayagraj, India. New essays land roughly twice a month.
Keep reading
Adjacent essays.
The newsletter
New articles in your inbox.
Occasional articles on engineering, tooling, and software development practices. No marketing, no fluff β just the article, when it's ready.
Unsubscribe with one click. Your email never leaves the list.


