Skip to content

Creating MySQL users and granting privileges correctly

A reference for CREATE USER and GRANT in MySQL 8 — host masks, granular privileges, the principle of least privilege, and the audit query every operator should know.

Most MySQL outages caused by privilege errors trace to two mistakes — granting ALL PRIVILEGES ON *.* to an application user, or creating an account with an overly permissive host mask. This reference covers MySQL 8 user creation, the privilege granularity that matters, the host-mask rules that catch operators new to MySQL, and the audit query that surfaces drift.

How to verify

To see every user, the host pattern they connect from, and what they are allowed to do:

-- Every account in the system, with auth plugin
SELECT user, host, plugin FROM mysql.user ORDER BY user;

-- Privileges for a specific user across schemas
SHOW GRANTS FOR 'app'@'10.0.0.%';

-- Audit: every account with broad privileges
SELECT GRANTEE, PRIVILEGE_TYPE FROM information_schema.USER_PRIVILEGES
WHERE PRIVILEGE_TYPE IN ('ALL PRIVILEGES','SUPER','FILE','PROCESS','RELOAD','SHUTDOWN','CREATE USER','GRANT OPTION');

What to look for: any application user with ALL PRIVILEGES, any account holding GRANT OPTION outside the DBA cohort, and any host = '%' outside of explicitly scoped read-replicas.

What’s happening

MySQL identifies a user by the pair ('username', 'host'), not by username alone. app@10.0.0.5 and app@10.0.0.6 are two different accounts. The host part accepts wildcards (% matches anything, _ matches one character), hostnames, IP literals, and CIDR-ish patterns (192.168.1.%). When a client connects, MySQL matches the most specific host pattern first — app@10.0.0.5 wins over app@10.0.0.%, which wins over app@%.

Privileges live at four levels: global (ON *.*), database (ON db.*), table (ON db.table), and column (ON db.table (col1, col2)). The grant tables (mysql.user, mysql.db, mysql.tables_priv, mysql.columns_priv) hold them in that order; SHOW GRANTS consolidates the view. MySQL 8 also introduced roles — named bundles of privileges that you grant to users and toggle with SET ROLE.

The default authentication plugin since MySQL 8 is caching_sha2_password. Older clients (5.7 and below libraries) may not speak it; for those, use mysql_native_password per account. Do not change the server default — change the account.

The procedure

  1. Create the user with a specific host pattern. The pattern should match exactly the network where the app lives. If the app is on 10.0.0.0/24, use 10.0.0.%. If it is a single host, use the IP literal.

    CREATE USER 'orderapp'@'10.0.0.%' IDENTIFIED WITH caching_sha2_password BY 'strong-password';
  2. Grant only what the app needs, scoped to its schema. Read/write CRUD on its own schema, no DDL, no GRANT OPTION.

    GRANT SELECT, INSERT, UPDATE, DELETE ON orders.* TO 'orderapp'@'10.0.0.%';

    If the app runs migrations at boot, give it DDL on its own schema only — never globally:

    GRANT CREATE, ALTER, DROP, INDEX, REFERENCES ON orders.* TO 'orderapp'@'10.0.0.%';
  3. Use roles when several apps share a privilege pattern.

    CREATE ROLE 'app_readwrite';
    GRANT SELECT, INSERT, UPDATE, DELETE ON orders.* TO 'app_readwrite';
    GRANT 'app_readwrite' TO 'orderapp'@'10.0.0.%', 'reportapp'@'10.0.1.%';
    
    -- The user must activate the role each session, or set it as default
    ALTER USER 'orderapp'@'10.0.0.%' DEFAULT ROLE 'app_readwrite';
  4. Create a read-only user for replicas, dashboards, and ad-hoc analysts.

    CREATE USER 'analyst'@'10.0.2.%' IDENTIFIED BY 'strong-password';
    GRANT SELECT ON orders.* TO 'analyst'@'10.0.2.%';
    GRANT SELECT ON customers.* TO 'analyst'@'10.0.2.%';
  5. Require TLS for any user that connects from off-host.

    ALTER USER 'orderapp'@'10.0.0.%' REQUIRE SSL;

    For stricter posture, require a client certificate signed by a specific issuer:

    ALTER USER 'orderapp'@'10.0.0.%' REQUIRE X509;
  6. Set resource limits to prevent runaway workloads.

    ALTER USER 'orderapp'@'10.0.0.%'
      WITH MAX_QUERIES_PER_HOUR 100000
           MAX_CONNECTIONS_PER_HOUR 10000
           MAX_USER_CONNECTIONS 50;
  7. Flush privileges only when you edit grant tables directly. Modern CREATE USER, GRANT, REVOKE, ALTER USER apply immediately — no FLUSH PRIVILEGES needed. The only case where you need it is UPDATE mysql.user SET ..., which you should not be doing.

  8. Rotate. Change application passwords at least quarterly. Use the dual-password feature so the app can fail over without downtime:

    -- Set a new primary password; the old one stays valid as a secondary
    ALTER USER 'orderapp'@'10.0.0.%' IDENTIFIED BY 'new-password' RETAIN CURRENT PASSWORD;
    -- After app rollout, drop the secondary
    ALTER USER 'orderapp'@'10.0.0.%' DISCARD OLD PASSWORD;

Common pitfalls

  • A user with host = '%' and a host = 'localhost' row for the same name — the localhost row wins for unix-socket connections and you get baffling “access denied” mismatches. Pick one, drop the other.
  • Granting ALL PRIVILEGES ON *.* to anything other than a DBA account hands SUPER, FILE (read any file the daemon can), and PROCESS (see every query) — far more than any app needs.
  • Without WITH GRANT OPTION a user cannot create other users; that is the desired default for app accounts. Give it only to DBA accounts.
  • Hostname-style patterns ('%.example.com') require skip-name-resolve = 0 and a reliable reverse DNS — most production MySQL hosts run with skip-name-resolve = 1 and IP-only matching, which means hostname grants silently fail to match.
  • The mysql.session and mysql.sys accounts are system-reserved. Never modify or drop them or replication and the sys schema break.

In the engagements we run, MySQL user management is driven by an Ansible role with grants kept in YAML — every privilege change goes through pull request, every account has an owner, and quarterly the role re-applies its inventory so drift is caught and corrected. We carry this under Managed Operations so clients have an audit trail, not a folklore of who created app2@% in 2022.