GRANT Privileges
In this page:
Introduction to GRANT
A freshly created user starts with zero permissions by default — GRANT is the statement that assigns specific rights, like SELECT or INSERT, on particular databases or tables to that account.
Example: Introduction to GRANT
GRANT SELECT, INSERT ON mydb.* TO 'app_user'@'localhost';
Granting All Privileges
Granting ALL PRIVILEGES hands a user complete administrative control over a database, including the ability to pass their own privileges on to other accounts — a powerful option that should be reserved for trusted admins only.
Example: Granting All Privileges
GRANT ALL PRIVILEGES ON mydb.* TO 'admin_user'@'localhost';
Granting Column Level Privileges
Privileges can be scoped down to individual columns rather than an entire table, which is the right tool when a user needs to query most of a table but should never see sensitive fields like salary or phone number.
Example: Granting Column Level Privileges
GRANT SELECT (id, name) ON mydb.employees TO 'app_user'@'localhost';
Checking Existing User Privileges
SHOW GRANTS lists every privilege currently assigned to a given account, making it easy to audit exactly what access a user has without digging through every GRANT statement that was ever run.
Example: Checking Existing User Privileges
SHOW GRANTS FOR 'app_user'@'localhost';
Applying Granted Privileges Instantly
MySQL sometimes caches privilege information in memory, so after making permission changes it's good practice to reload the grant tables to guarantee the new permissions take effect immediately rather than on next login.
Example: Applying Granted Privileges Instantly
GRANT SELECT ON mydb.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: