Roles & Privileges — Complete Guide
Roles & Privileges — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 83 of 100
Roles & Privileges
Basics ✓ → Advanced
Advanced · 2 — Production · ~10 min · MySQL — Security & Cloud MySQL
What is this?
Roles (MySQL 8) bundle privileges. GRANT role TO user; activate with SET ROLE. Easier onboarding: new dev gets dataflow_developer role not 20 GRANT lines.
Why should you care?
Contractor leaves — revoke one role instead of hunting individual grants across schemas.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
CREATE ROLE IF NOT EXISTS 'dataflow_developer';
GRANT SELECT, INSERT, UPDATE, DELETE ON DataFlow.* TO 'dataflow_developer';
CREATE USER IF NOT EXISTS 'dev_priya'@'%' IDENTIFIED BY 'tempPass!';
GRANT 'dataflow_developer' TO 'dev_priya'@'%';
SET DEFAULT ROLE 'dataflow_developer' TO 'dev_priya'@'%';
What happened?
- Role holds privileges on DataFlow schema.
- User dev_priya inherits via role.
- DEFAULT ROLE applies on login without SET ROLE each session.
Practice next
- CREATE ROLE and GRANT privileges.
- CREATE USER and GRANT role TO user.
- Login as dev_priya; run SELECT on orders.
- CREATE ROLE dataflow_readonly with SELECT only; compare.
- SHOW GRANTS FOR dev_priya showing role grants.
Remember
Roles group privileges for job functions. DEFAULT ROLE simplifies login. REVOKE role TO removes access batch.
DataFlow team access
Intern gets dataflow_readonly role; senior gets dataflow_developer.
Outcome: Access reviews map to roles not individuals.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!