In MySQL, privileges control what actions a user can perform, while roles are collections of privileges that can be assigned to users. Roles simplify permission management and improve database security.
MySQL supports different privilege levels:
*.*)db_name.*)db_name.table)
-- Create a new role for read-only access
CREATE ROLE 'reporting_role';
-- Grant SELECT privilege on a database
GRANT SELECT ON company_db.* TO 'reporting_role';
-- Assign role to a user
GRANT 'reporting_role' TO 'rahul'@'localhost';
-- Set role as default for the user
SET DEFAULT ROLE 'reporting_role' TO 'rahul'@'localhost';
-- Grant multiple privileges directly to a user
GRANT INSERT, UPDATE ON company_db.employees TO 'admin1'@'localhost';
After executing these commands:
reporting_role can only read datarahul inherits all permissions of the roleSHOW GRANTS