RBAC in MySQL groups privileges into roles and assigns those roles to users, simplifying and securing permission management.
Role-Based Access Control (RBAC) lets you bundle privileges into named roles, then grant those roles to users instead of granting every privilege individually. This keeps permission management clean and auditable.
Roles decouple privilege definition from user accounts. You update a role once and every user holding that role inherits the change, reducing human error and admin time.
CREATE ROLE IF NOT EXISTS order_reporter;
The command registers the role but grants no privileges yet.
GRANT SELECT ON ecommerce.Orders TO order_reporter;
GRANT SELECT ON ecommerce.OrderItems TO order_reporter;
Only data-reader privileges are given, keeping the role read-only.
GRANT order_reporter TO 'analyst'@'%';
SET DEFAULT ROLE order_reporter TO 'analyst'@'%';
GRANT attaches the role; SET DEFAULT ROLE makes it auto-active at login.
SET ROLE order_reporter; -- enable a specific role
SET ROLE DEFAULT; -- enable all default roles
SET ROLE NONE; -- disable all roles
Session-level control lets power users toggle privileges as needed.
SHOW GRANTS FOR order_reporter;
The output lists every privilege associated with the role, aiding audits.
REVOKE SELECT ON ecommerce.Orders FROM order_reporter;
DROP ROLE order_reporter;
Always revoke privileges first if you want a clean drop of the role.
Create small, focused roles (e.g., read, write, admin). Use naming conventions, keep a changelog, and review role assignments quarterly.
Avoid granting privileges directly to users; always use roles. Remember to set default roles, or users will log in with no active privileges.
Yes. Grant several roles to the same account and optionally set multiple default roles. The union of privileges becomes available when the roles are active.
Absolutely. Grant EXECUTE on procedures and SELECT on views to roles exactly as you would with tables, then assign the roles to users.
Roles were introduced in MySQL 8.0 and are available in Community and Enterprise editions. Earlier versions require manual privilege grouping.