⬅ Previous Topic
Restoring Data using mongorestoreNext Topic ⮕
Role-Based Access Control and Authentication⬅ Previous Topic
Restoring Data using mongorestoreNext Topic ⮕
Role-Based Access Control and AuthenticationIn MongoDB, access control is managed through users and roles. When authorization is enabled, users must be created and authenticated before they can perform actions on the database.
Each user is associated with one or more roles, which determine what the user is allowed to do — such as reading from a collection, writing to a database, or administering the server.
By default, MongoDB starts with no authentication, which means anyone can access the database. This might be fine for development, but it’s insecure for production.
Creating users and assigning specific roles helps you:
Before you can use users and roles, make sure MongoDB is running with authorization enabled.
mongod --auth --dbpath /data/db
This starts the MongoDB server with authentication mode on.
If you haven’t created any users yet, you can connect to the database without credentials (just for initial setup).
mongo
use admin
This user will have full privileges over the entire server.
db.createUser({
user: "adminUser",
pwd: "adminPass123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
});
Successfully added user: { "user" : "adminUser", "roles" : [ ... ] }
Explanation: The userAdminAnyDatabase
role allows this user to create and manage other users. readWriteAnyDatabase
gives read and write permissions on all databases.
mongo -u "adminUser" -p "adminPass123" --authenticationDatabase "admin"
Explanation: Now that authentication is enabled, you must use the admin credentials to connect and perform further operations.
Let’s create a user that only has access to a specific database, for example, inventory
.
use inventory
db.createUser({
user: "appUser",
pwd: "appPass456",
roles: [ { role: "readWrite", db: "inventory" } ]
});
Successfully added user: { "user" : "appUser", "roles" : [ { "role" : "readWrite", "db" : "inventory" } ] }
Explanation: This user can only read and write data in the inventory
database. They cannot access other databases or perform administrative tasks.
Q: Can appUser
drop collections in the admin
database?
A: No! Because their role is scoped only to the inventory
database and does not include admin privileges.
To list all users in the current database:
db.getUsers()
[ { "_id" : "inventory.appUser", "user" : "appUser", "db" : "inventory", "roles" : [ { "role" : "readWrite", "db" : "inventory" } ] } ]
If you want to delete a user:
db.dropUser("appUser")
true
MongoDB provides a flexible, role-based access control system. By defining users and roles, you can ensure that only authorized users can perform specific actions on your databases.
This not only protects your data but also allows better organization of user responsibilities across your application.
In the next lesson, we’ll cover MongoDB Security Best Practices to further harden your deployment for production environments.
⬅ Previous Topic
Restoring Data using mongorestoreNext Topic ⮕
Role-Based Access Control and AuthenticationYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.