Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

MySQL Users and Privileges

Direct answer

MySQL/MariaDB users are identified as 'user'@'host' and granted privileges at global, database, table or column level.

When to use this

Use it when creating app users or when access is denied.

First 30 seconds

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.

Operational verification

client user + host → account matched → password/plugin → grants

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Key commandSHOW GRANTS FOR 'app'@'10.0.%';
Scope / prerequisiteUse it when creating app users or when access is denied.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

The host part matters — 'app'@'%' and 'app'@'localhost' are different accounts. Grant least privilege to app users; no SUPER/ALL for apps.

Key checks
  • SELECT user,host FROM mysql.user; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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 forRisk
Anonymous users (user = '')Match before named accounts
GRANT ALL ON *.* or SUPER for applicationsFull server control
host = '%' on privileged accountsLogin from anywhere
mysql_native_password pluginDeprecated weaker hashing; caching_sha2_password is the default

MySQL privileges are additive; there is no explicit deny, so remove rights with REVOKE.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
mysql users and privilegesmysql grant privilegesmysql access denied
Confirmation

Confirm action

Continue?