0
0
MySQLquery~10 mins

Granting privileges in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Granting privileges
Start
Identify User
Choose Privileges
Specify Database/Table
Execute GRANT Command
Privileges Updated
User Can Perform Actions
End
The flow shows how to grant privileges to a user step-by-step: identify the user, select privileges, specify scope, execute the command, and update access.
Execution Sample
MySQL
GRANT SELECT, INSERT ON mydb.* TO 'alice'@'localhost';
This command grants SELECT and INSERT privileges on all tables in 'mydb' to user 'alice' connecting from localhost.
Execution Table
StepActionCommand PartEffectResult
1Identify User'alice'@'localhost'Target user for privilegesUser identified
2Choose PrivilegesSELECT, INSERTPrivileges to grantPrivileges selected
3Specify ScopeON mydb.*Apply privileges to all tables in 'mydb'Scope set
4Execute GRANTGRANT SELECT, INSERT ON mydb.* TO 'alice'@'localhost';Privileges assigned in MySQLPrivileges granted
5VerifySHOW GRANTS FOR 'alice'@'localhost';Check assigned privilegesPrivileges confirmed
6EndProcess completeUser can now perform SELECT and INSERT on mydb tables
💡 All steps completed, privileges successfully granted to user 'alice'@'localhost'
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
UserNone'alice'@'localhost''alice'@'localhost''alice'@'localhost''alice'@'localhost''alice'@'localhost'
PrivilegesNoneNoneSELECT, INSERTSELECT, INSERTSELECT, INSERTSELECT, INSERT
ScopeNoneNoneNonemydb.*mydb.*mydb.*
Privileges GrantedNoNoNoNoYesYes
Key Moments - 3 Insights
Why do we specify the user as 'alice'@'localhost' instead of just 'alice'?
MySQL identifies users by both username and host. Specifying 'alice'@'localhost' means privileges apply only when 'alice' connects from the local machine. This is shown in execution_table step 1.
What happens if we omit the database or table after ON?
If you omit the database or table, MySQL will not know where to apply privileges. You must specify a database and optionally tables, like 'mydb.*' for all tables in 'mydb' (execution_table step 3).
Does the GRANT command immediately allow the user to perform actions?
Yes, once the GRANT command executes successfully (step 4), the privileges are active and the user can perform the allowed actions (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are the privileges actually assigned in MySQL?
AStep 4
BStep 5
CStep 2
DStep 3
💡 Hint
Check the 'Effect' column for when privileges are assigned.
According to variable_tracker, what is the value of 'Scope' after step 3?
Amydb.*
B'alice'@'localhost'
CNone
DSELECT, INSERT
💡 Hint
Look at the 'Scope' row under 'After Step 3' column.
If we change the user to 'bob'@'%', which step in execution_table changes?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
User identification is handled in step 1.
Concept Snapshot
GRANT privileges ON database.table TO 'user'@'host';
- Specify user with username and host.
- List privileges like SELECT, INSERT.
- Define scope: database and tables.
- Execute to assign privileges immediately.
- Verify with SHOW GRANTS FOR 'user'@'host';
Full Transcript
Granting privileges in MySQL involves specifying the user with username and host, choosing which privileges to grant, defining the scope of those privileges on databases or tables, and executing the GRANT command. The process starts by identifying the user, selecting privileges, setting the scope, and then running the command to update the user's permissions. After granting, the user can perform the allowed actions immediately. Verification can be done using SHOW GRANTS. This step-by-step flow ensures precise control over database access.