Skip to main content

VillageSQL is a drop-in replacement for MySQL with extensions.

All examples in this guide work on VillageSQL. Install Now →
MySQL’s access control model separates authentication (who you are) from authorization (what you can do). You create a user account, then grant privileges to it separately.

Creating Users

The @'localhost' part is the host restriction — this account can only connect from the local machine. Common host patterns: 'alice'@'localhost' and 'alice'@'%' are two distinct accounts, even though the username is the same.

Granting Privileges

The privilege object uses database.table notation:
  • *.* — all databases, all tables (global)
  • myapp.* — all tables in the myapp database
  • myapp.orders — only the orders table

Privilege Levels

Common privileges:

Viewing Grants

Revoking Privileges

Revoking a privilege that was never granted returns ERROR 1141: There is no such grant defined for user. The user retains any privileges granted at other levels — revoking SELECT ON myapp.* does not remove SELECT ON myapp.orders if that was granted separately.

Changing Passwords

Force a password change on next login:

Roles (MySQL 8.0+)

Roles are named collections of privileges. Instead of granting individual privileges to each user, grant a role.
Users must activate a role to use its privileges (or set it as a default):

Dropping Users

Dropping a user automatically revokes all their privileges and removes them from all roles. If the user is currently connected, the session continues until it ends.

Least-Privilege Best Practices

Give each account only the privileges it needs:
  • Application read replicas: SELECT only
  • Application write account: SELECT, INSERT, UPDATE, DELETE
  • Application schema account (migrations): add CREATE, ALTER, DROP, INDEX
  • DBA account: ALL PRIVILEGES on *.*
  • Never grant SUPER or ALL to application accounts
Restrict hosts as tightly as possible. An application on app.example.com should use 'appuser'@'app.example.com', not 'appuser'@'%'.

Frequently Asked Questions

Does MySQL flush grants automatically?

All changes via CREATE USER, GRANT, REVOKE, and ALTER USER take effect immediately — no FLUSH PRIVILEGES needed. FLUSH PRIVILEGES is only necessary if you modify the grant tables directly with INSERT/UPDATE (which you shouldn’t do).

What’s the difference between localhost and 127.0.0.1?

localhost in MySQL refers to the Unix socket connection (local file socket), not the network loopback. 127.0.0.1 is the TCP loopback. If your client connects via TCP even on the same machine, use 127.0.0.1 as the host restriction.

Troubleshooting

See also