Bird
Raised Fist0
MySQLquery~10 mins

Role-based access in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Role-based access
Create Roles
↓
Assign Privileges to Roles
↓
Create Users
↓
Assign Roles to Users
↓
User Accesses Database
↓
Check User Roles
↓
Grant or Deny Access Based on Role
This flow shows how roles are created, given privileges, assigned to users, and then used to control access.
Execution Sample
MySQL
CREATE ROLE reader;
GRANT SELECT ON employees TO reader;
CREATE USER 'alice'@'localhost' IDENTIFIED BY 'pass';
GRANT 'reader' TO 'alice'@'localhost';
SET DEFAULT ROLE reader TO 'alice'@'localhost';
SELECT * FROM employees;
This code creates a role with select permission, creates a user, assigns the role, and then the user queries the table.
Execution Table
StepActionEvaluationResult
1CREATE ROLE reader;Role 'reader' does not existRole 'reader' created
2GRANT SELECT ON employees TO reader;Role 'reader' existsSELECT privilege granted on employees to reader
3CREATE USER 'alice'@'localhost' IDENTIFIED BY 'pass';User 'alice'@'localhost' does not existUser 'alice'@'localhost' created
4GRANT reader TO 'alice'@'localhost';Role 'reader' and user existRole 'reader' granted to user 'alice'@'localhost'
5SET DEFAULT ROLE reader TO 'alice'@'localhost';User and role existDefault role for 'alice' set to 'reader'
6SELECT * FROM employees; -- executed by aliceUser 'alice' has role 'reader' with SELECT privilegeQuery succeeds, returns employee rows
7SELECT * FROM employees; -- executed by user without roleUser lacks SELECT privilegeQuery denied
💡 Execution stops after query success or denial based on role privileges.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
Roles{}{reader}{reader}{reader}{reader}{reader}{reader}
Privileges{}{}{reader: SELECT on employees}{reader: SELECT on employees}{reader: SELECT on employees}{reader: SELECT on employees}{reader: SELECT on employees}
Users{}{}{}{'alice'@'localhost': {}}{'alice'@'localhost': {reader}}{'alice'@'localhost': {reader}}{'alice'@'localhost': {reader}}
Default Role{}{}{}{}{}{'alice'@'localhost': reader}{'alice'@'localhost': reader}
Key Moments - 3 Insights
Why can't a user access a table even if the table exists?
Because the user does not have a role with the required privileges. See execution_table step 7 where the query is denied due to missing SELECT privilege.
What happens if a role is created but no privileges are granted to it?
The role exists but grants no access. Users with that role cannot perform any actions. See variable_tracker Privileges after Step 1 and 2 for difference.
Why do we set a default role for a user?
The default role determines which privileges apply automatically when the user connects. Without it, the user might have no active privileges. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the user 'alice' created?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Action' column for user creation commands.
According to variable_tracker, what is the default role of 'alice' after Step 5?
Areader
BNo default role
Cadmin
Dguest
💡 Hint
Look at the 'Default Role' row after Step 5.
In execution_table, what happens at Step 7 when a user without the role tries to SELECT?
AQuery succeeds
BQuery denied
CRole is automatically assigned
DUser is created
💡 Hint
See the 'Result' column for Step 7.
Concept Snapshot
Role-based access in MySQL:
- CREATE ROLE to define roles
- GRANT privileges to roles
- CREATE USER to add users
- GRANT role TO user to assign roles
- SET DEFAULT ROLE to activate roles on login
- User actions checked against active roles' privileges
Full Transcript
Role-based access control in MySQL involves creating roles, assigning privileges to those roles, creating users, and then granting roles to users. When a user connects, their default role determines what actions they can perform. For example, creating a role 'reader' with SELECT privilege on a table, then creating a user 'alice' and granting her the 'reader' role, allows 'alice' to query the table. If a user lacks the role or privileges, their queries are denied. This method simplifies managing permissions by grouping privileges into roles and assigning roles to users.

Practice

(1/5)
1. What is the main purpose of role-based access control (RBAC) in MySQL?
easy
A. To store user passwords securely
B. To group permissions into roles and assign them to users
C. To optimize query performance
D. To backup the database automatically

Solution

  1. Step 1: Understand role-based access control concept

    RBAC groups permissions into roles instead of assigning them individually to users.
  2. Step 2: Identify the main purpose in MySQL context

    Assigning roles to users simplifies permission management and improves security.
  3. Final Answer:

    To group permissions into roles and assign them to users -> Option B
  4. Quick Check:

    RBAC = Group permissions + assign roles [OK]
Hint: RBAC groups permissions, then assigns roles to users [OK]
Common Mistakes:
  • Confusing RBAC with password storage
  • Thinking RBAC improves query speed
  • Assuming RBAC is for backups
2. Which of the following is the correct syntax to create a role named manager in MySQL?
easy
A. CREATE ROLE manager;
B. MAKE ROLE manager;
C. ADD ROLE manager;
D. NEW ROLE manager;

Solution

  1. Step 1: Recall MySQL syntax for creating roles

    The correct command to create a role is CREATE ROLE role_name;.
  2. Step 2: Match syntax with options

    Only CREATE ROLE manager; uses the correct CREATE ROLE syntax.
  3. Final Answer:

    CREATE ROLE manager; -> Option A
  4. Quick Check:

    CREATE ROLE = create new role [OK]
Hint: Use CREATE ROLE to make a new role in MySQL [OK]
Common Mistakes:
  • Using MAKE ROLE instead of CREATE ROLE
  • Using ADD ROLE which is invalid
  • Using NEW ROLE which is not a MySQL command
3. Given the following commands:
CREATE ROLE analyst;
GRANT SELECT ON sales TO analyst;
GRANT analyst TO alice;

What permission does user alice have on the sales table?
medium
A. SELECT permission on sales
B. INSERT permission on sales
C. No permissions on sales
D. ALL permissions on sales

Solution

  1. Step 1: Analyze role creation and permission grant

    The role analyst is created and given SELECT permission on the sales table.
  2. Step 2: Check user role assignment

    User alice is granted the analyst role, so she inherits its permissions.
  3. Final Answer:

    SELECT permission on sales -> Option A
  4. Quick Check:

    User with role gets role's permissions [OK]
Hint: User inherits permissions from assigned roles [OK]
Common Mistakes:
  • Assuming user gets INSERT without grant
  • Thinking user has no permissions without direct grant
  • Assuming ALL permissions without explicit grant
4. Consider this MySQL command:
GRANT SELECT ON employees TO 'john';

Why might this command fail when using role-based access?
medium
A. Because 'john' is not a valid username format
B. Because SELECT is not a valid permission
C. Because GRANT cannot be used on tables
D. Because permissions should be granted to roles, not directly to users

Solution

  1. Step 1: Understand role-based access best practice

    In RBAC, permissions are granted to roles, then roles are assigned to users.
  2. Step 2: Identify why direct grant to user may fail

    Granting permissions directly to users bypasses roles and may be restricted or discouraged.
  3. Final Answer:

    Because permissions should be granted to roles, not directly to users -> Option D
  4. Quick Check:

    Grant permissions to roles, assign roles to users [OK]
Hint: Grant permissions to roles, not users directly [OK]
Common Mistakes:
  • Thinking SELECT is invalid permission
  • Assuming username format is wrong
  • Believing GRANT can't be used on tables
5. You want to create a role developer with INSERT and UPDATE permissions on the projects table, then assign it to user emma. Which sequence of commands correctly achieves this?
hard
A. CREATE ROLE developer; GRANT SELECT ON projects TO developer; GRANT developer TO emma;
B. GRANT INSERT, UPDATE ON projects TO emma; CREATE ROLE developer; GRANT developer TO emma;
C. CREATE ROLE developer; GRANT INSERT, UPDATE ON projects TO developer; GRANT developer TO emma;
D. CREATE USER developer; GRANT INSERT, UPDATE ON projects TO developer; GRANT developer TO emma;

Solution

  1. Step 1: Create the role named developer

    Use CREATE ROLE developer; to define the role.
  2. Step 2: Grant INSERT and UPDATE permissions on projects to the role

    Use GRANT INSERT, UPDATE ON projects TO developer; to assign permissions.
  3. Step 3: Assign the role to user emma

    Use GRANT developer TO emma; to give emma the role's permissions.
  4. Final Answer:

    CREATE ROLE developer; GRANT INSERT, UPDATE ON projects TO developer; GRANT developer TO emma; -> Option C
  5. Quick Check:

    Create role, grant permissions, assign role [OK]
Hint: Create role, grant permissions, then assign role to user [OK]
Common Mistakes:
  • Granting permissions directly to user instead of role
  • Using CREATE USER instead of CREATE ROLE
  • Granting wrong permissions like SELECT instead of INSERT, UPDATE
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard