Use it when creating app users or when access is denied.
MySQL Users and Privileges
MySQL/MariaDB users are identified as 'user'@'host' and granted privileges at global, database, table or column level.
1. SELECT user,host FROM mysql.user;.
2. SHOW GRANTS FOR 'app'@'host';.
3. Access denied → which user@host matched? check from the client host.
4. Grant minimal privileges.
5. Remove anonymous users.
client user + host → account matched → password/plugin → grants
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
SHOW GRANTS FOR 'app'@'10.0.%';Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Connect from the real application host and run SELECT CURRENT_USER(); to see which account matched. Confirm the account cannot access other databases.
Step 2 Common failures Narrow the fault
An anonymous account (''@localhost) can match before your user. Old clients may not support caching_sha2_password. GRANT ALL ON *.* gives application accounts administrative power.
The host part matters — 'app'@'%' and 'app'@'localhost' are different accounts. Grant least privilege to app users; no SUPER/ALL for apps.
SELECT user,host FROM mysql.user;— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
At login the server matches the client user and host against accounts, picking the most specific host match. Privileges are checked at each statement. Authentication plugins (for example caching_sha2_password) decide how passwords are verified.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
An account is user + host
'app'@'10.0.%' and 'app'@'localhost' are two different accounts with separate passwords and privileges. When a client connects, the server sorts accounts by how specific the host is (literal host before wildcard) and uses the first match — so an anonymous ''@'localhost' account can win over 'app'@'%' for local connections.
SELECT USER(), CURRENT_USER();
-- USER(): what the client sent ('app@10.0.4.17')
-- CURRENT_USER(): the account that matched ('app@10.0.%')
Least privilege with roles (MySQL 8+)
CREATE ROLE app_read, app_write;
GRANT SELECT ON shop.* TO app_read;
GRANT INSERT, UPDATE, DELETE ON shop.* TO app_write;
CREATE USER 'shop_app'@'10.0.%' IDENTIFIED BY '<secret>' REQUIRE SSL;
GRANT app_read, app_write TO 'shop_app'@'10.0.%';
SET DEFAULT ROLE ALL TO 'shop_app'@'10.0.%';
Roles are not active unless set as default or activated with SET ROLE — a common reason for "permission denied" right after granting a role.
Things to audit
SELECT user, host, plugin, password_expired, account_locked FROM mysql.user;
SELECT user, host FROM mysql.user WHERE user = '' OR host = '%';
SHOW GRANTS FOR 'shop_app'@'10.0.%';
| Look for | Risk |
|---|---|
Anonymous users (user = '') | Match before named accounts |
GRANT ALL ON *.* or SUPER for applications | Full server control |
host = '%' on privileged accounts | Login from anywhere |
mysql_native_password plugin | Deprecated weaker hashing; caching_sha2_password is the default |
MySQL privileges are additive; there is no explicit deny, so remove rights with REVOKE.
Common mistakes
GRANT ALL ON *.*for applications.
ReferenceConfiguration & state1 section · depth on demand
Commands
CREATE USER 'app'@'10.0.%' IDENTIFIED BY '<password>';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'10.0.%';
SHOW GRANTS FOR 'app'@'10.0.%';
SELECT user, host, plugin FROM mysql.user;
REVOKE ALL PRIVILEGES ON *.* FROM 'olduser'@'%'; DROP USER 'olduser'@'%';
Documented example, not captured output. 'app'@'%' and 'app'@'localhost' are different accounts. Avoid GRANT ALL ON *.* for applications; use roles (MySQL 8, MariaDB 10.0.5+) for groups of privileges.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.