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:
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
| Level | Syntax | Example use |
|---|---|---|
| Global | ON *.* | Administrators (use sparingly) |
| Database | ON shop.* | An application's own database |
| Table | ON shop.orders | A reporting user |
| Column | SELECT (id, name) ON shop.customers | Hide sensitive columns |
| Routine | EXECUTE ON PROCEDURE shop.p | Allow only stored procedures |
Column Privileges
Common Privileges
| Privilege | Allows |
|---|---|
SELECT, INSERT, UPDATE, DELETE | Normal data access |
CREATE, ALTER, DROP, INDEX | Structure changes |
EXECUTE | Run stored routines |
REFERENCES | Create foreign keys |
ALL PRIVILEGES | Everything on that level (avoid for apps) |
GRANT OPTION | Pass your privileges on to others |
SUPER / SYSTEM_VARIABLES_ADMIN | Server-level administration |
Roles (MySQL 8.0+)
Instead of repeating the same grants for many users, group them in a role:
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
Removing the table grant left the earlier column grant untouched. Privileges are separate pieces.
Creating Safe Accounts
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
Result:
| logged_in | is_8_or_later |
|---|---|
| 1 | 1 |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
App connects as root | Any bug is a total breach | Dedicated least-privilege user |
'user'@'%' with ALL PRIVILEGES | Open to the world | Limit host and privileges |
| Password in source code | Leaks through git | Environment variables or a secrets manager |
Forgetting FLUSH PRIVILEGES after editing mysql.user by hand | Stale privileges | Use GRANT and CREATE USER, which need no flush |
| Shared accounts for people | No accountability | One account per person, or roles |
Leaving GRANT OPTION on | Privileges spread | Only for administrators |
Practice
- Create a read-only user for one database.
- Create a role
analystand give it to two users. - Hide the
emailcolumn using a column grant. - Run
SHOW GRANTSfor 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.

