MySQL Users, Roles and Privileges: GRANT, REVOKE and Least Privilege

MySQL 60 min min read Updated: Nov 27, 2026 Intermediate
MySQL Users, Roles and Privileges: GRANT, REVOKE and Least Privilege
Intermediate Topic 1 of 3

Accounts in MySQL

A MySQL account is user plus host: 'app'@'10.0.0.5' and 'app'@'localhost' are two different accounts. The host part limits where the user may connect from. % means any host (avoid it for powerful accounts).

Principle of Least Privilege

Give every application and person only what they need. A reporting tool needs SELECT, not DROP. If the account leaks or the app has an SQL injection bug, the damage stays small.

A Real Test: Users and Grants

This test ran on a real MySQL 8.0 server as root, then connected as the new user. Passwords here are throw-away test values:

text root CREATE USER 'report_user'@'localhost' IDENTIFIED BY 'S3cret!pass' root GRANT SELECT ON shopdemo.orders TO 'report_user'@'localhost' root SHOW GRANTS FOR 'report_user'@'localhost' -> [['GRANT USAGE ON *.* TO `report_user`@`localhost`'], ['GRANT SELECT ON `shopdemo`.`orders` TO `report_user`@`localhost`']] report SELECT id, total FROM orders -> [[1, 500]] report SELECT * FROM customers -> ERROR 1142 SELECT command denied to user 'report_user'@'localhost' for table 'customers' report UPDATE orders SET total = 0 WHERE id = 1 -> ERROR 1142 UPDATE command denied to user 'report_user'@'localhost' for table 'orders' report DROP TABLE orders -> ERROR 1142 DROP command denied to user 'report_user'@'localhost' for table 'orders' report SELECT CURRENT_USER() -> [['report_user@localhost']]

GRANT USAGE means "can log in, nothing else". The user could read orders but got error 1142 for the other tables and statements.

Privilege Levels

LevelSyntaxExample use
GlobalON *.*Administrators (use sparingly)
DatabaseON shop.*An application's own database
TableON shop.ordersA reporting user
ColumnSELECT (id, name) ON shop.customersHide sensitive columns
RoutineEXECUTE ON PROCEDURE shop.pAllow only stored procedures

Column Privileges

text root GRANT SELECT (id, name) ON shopdemo.customers TO 'report_user'@'localhost' report SELECT id, name FROM customers -> [[1, 'Asha']] report SELECT email FROM customers -> ERROR 1143 SELECT command denied to user 'report_user'@'localhost' for column 'email' in table 'customers'

Common Privileges

PrivilegeAllows
SELECT, INSERT, UPDATE, DELETENormal data access
CREATE, ALTER, DROP, INDEXStructure changes
EXECUTERun stored routines
REFERENCESCreate foreign keys
ALL PRIVILEGESEverything on that level (avoid for apps)
GRANT OPTIONPass your privileges on to others
SUPER / SYSTEM_VARIABLES_ADMINServer-level administration

Roles (MySQL 8.0+)

Instead of repeating the same grants for many users, group them in a role:

text root CREATE ROLE 'reader' root GRANT SELECT ON shopdemo.* TO 'reader' root CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'An0ther!pass' root GRANT 'reader' TO 'app_user'@'localhost' root SET DEFAULT ROLE 'reader' TO 'app_user'@'localhost' root SHOW GRANTS FOR 'app_user'@'localhost' USING 'reader' -> [['GRANT USAGE ON *.* TO `app_user`@`localhost`'], ['GRANT SELECT ON `shopdemo`.* TO `app_user`@`localhost`'], ['GRANT `reader`@`%` TO `app_user`@`localhost`']] app SELECT CURRENT_ROLE() -> [['`reader`@`%`']] app SELECT COUNT(*) FROM customers -> [[1]] app DELETE FROM customers -> ERROR 1142 DELETE command denied to user 'app_user'@'localhost' for table 'customers'

A role must be active for a session to use it. SET DEFAULT ROLE activates it at login; otherwise the user runs SET ROLE 'reader';.

REVOKE

text root REVOKE SELECT ON shopdemo.orders FROM 'report_user'@'localhost' root SHOW GRANTS FOR 'report_user'@'localhost' -> [['GRANT USAGE ON *.* TO `report_user`@`localhost`'], ['GRANT SELECT (`id`, `name`) ON `shopdemo`.`customers` TO `report_user`@`localhost`']]

Removing the table grant left the earlier column grant untouched. Privileges are separate pieces.

Creating Safe Accounts

sql -- application account, can connect only from the app server CREATE USER 'shop_app'@'10.0.0.5' IDENTIFIED BY 'a-long-random-password' PASSWORD EXPIRE INTERVAL 180 DAY FAILED_LOGIN_ATTEMPTS 5 PASSWORD_LOCK_TIME 1; GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop_app'@'10.0.0.5'; -- never do this for an application: -- GRANT ALL PRIVILEGES ON *.* TO 'shop_app'@'%'; ALTER USER 'shop_app'@'10.0.0.5' IDENTIFIED BY 'new-password'; DROP USER 'shop_app'@'10.0.0.5';
Authentication plugin MySQL 8 uses caching_sha2_password by default. Very old client libraries may not support it. The fix is to update the client, not to weaken the account with mysql_native_password.

Seeing Who Can Do What

sql SELECT CURRENT_USER() IS NOT NULL AS logged_in, @@version >= '8.0' AS is_8_or_later;

Result:

logged_inis_8_or_later
11
sql SHOW GRANTS FOR 'shop_app'@'10.0.0.5'; SELECT user, host, account_locked, password_expired FROM mysql.user;

Common Beginner Mistakes

MistakeWhat happensFix
App connects as rootAny bug is a total breachDedicated least-privilege user
'user'@'%' with ALL PRIVILEGESOpen to the worldLimit host and privileges
Password in source codeLeaks through gitEnvironment variables or a secrets manager
Forgetting FLUSH PRIVILEGES after editing mysql.user by handStale privilegesUse GRANT and CREATE USER, which need no flush
Shared accounts for peopleNo accountabilityOne account per person, or roles
Leaving GRANT OPTION onPrivileges spreadOnly for administrators

Practice

  1. Create a read-only user for one database.
  2. Create a role analyst and give it to two users.
  3. Hide the email column using a column grant.
  4. Run SHOW GRANTS for each account and check nothing is too wide.

Conclusion

MySQL accounts are user plus host. Grant at the lowest useful level, use roles for groups, prefer column and table grants for sensitive data, never run applications as root, and verify with SHOW GRANTS.

Quick Summary: CREATE USER, GRANT, REVOKE, roles, least privilege, column privileges.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators