SQL reference (MySQL and MariaDB dialects)

User and privilege statements

CREATE USER, ALTER USER, DROP USER, RENAME USER, SET PASSWORD, GRANT, REVOKE and SHOW GRANTS, with the privilege checks and the refused forms.

The front-ends have MySQL's account model (D54). The users written in the front-end's configuration file are bootstrap administrators: they have every privilege and cannot be changed by SQL. Every other account is one document in the hidden Raft DB database __rdb_accounts, keyed by user and host. It holds the two double hashes (SHA1(SHA1(pw)), SHA256(SHA256(pw))) and the grants, never the password.

Syntax#

SQL
CREATE USER [IF NOT EXISTS] account [auth] [account_option] [, account [auth] [account_option]] ...
ALTER USER [IF EXISTS] account [auth] [account_option] [, ...] ...
DROP USER [IF EXISTS] account [, account] ...
RENAME USER account TO account [, account TO account] ...
SET PASSWORD [FOR account] = { 'plain' | PASSWORD('plain') }

GRANT priv_type [(col_name, ...)] [, priv_type [(col_name, ...)]] ...
    ON priv_level TO account [auth] [, account [auth]] ... [WITH GRANT OPTION]
REVOKE priv_type [(col_name, ...)] [, ...] ON priv_level FROM account [, account] ...
REVOKE ALL [PRIVILEGES], GRANT OPTION FROM account [, account] ...
SHOW GRANTS [FOR account]

account:
    'user'@'host' | user@host | user | CURRENT_USER[()] | USER()

auth:
    IDENTIFIED [WITH plugin] BY 'password'
  | IDENTIFIED [WITH plugin] AS 'hash'
  | IDENTIFIED BY PASSWORD 'hash'
  | IDENTIFIED WITH plugin

account_option:
    ACCOUNT UNLOCK | REQUIRE NONE

priv_level:
    * | *.* | db_name.* | [TABLE] [db_name.]tbl_name

Arguments#

NameMeaningDefault
host%, a name, an address, or a pattern. localhost matches loopback.%
pluginmysql_native_password or caching_sha2_password. Any other is error 1524.caching_sha2_password
AS 'hash'The hash as mysql.user shows it (*... for the native plugin). A client offering caching_sha2_password for such an account is switched to the native plugin. A bad hash is error 1827.none
priv_typeSELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, plus ALL [PRIVILEGES], USAGE, GRANT OPTION. An unknown name is a syntax error.none
(col_name, ...)Column privileges, for SELECT, INSERT, UPDATE, REFERENCES.whole object
database leveldb.* may be a pattern with %.none

Remarks#

  • Login. A client logs in as the stored account with the most specific host that matches (%, localhost for loopback, patterns, exact address).
  • Which privilege runs what. CREATE USER is needed for CREATE USER, ALTER USER (of another account), DROP USER, RENAME USER, and SET PASSWORD FOR another account; RELOAD for FLUSH; SUPER for SET GLOBAL. Data statements are checked from their parsed form: the privilege, database, table and columns each needs (SELECT also for the columns of WHERE, ORDER BY and UPDATE expressions; REPLACE needs DELETE too; TRUNCATE needs DROP; ALTER TABLE ... RENAME needs DROP on the old and CREATE/INSERT on the new name).
  • Errors (MySQL's): 1044 database denied, 1142 table, 1143 column, 1227 global privilege, 1141, 1147, 1133, 1396 (with its reason: account exists / does not exist), 1410 (GRANT to an account that does not exist; create it first), 1221, 1144, 1827, 1045 (with 'user'@'localhost' for loopback), 1524.
  • A change bumps a counter every session compares, so GRANT and REVOKE act on the next statement of open connections.
  • SHOW GRANTS uses MySQL's canonical order and the ALL PRIVILEGES text. SHOW DATABASES, SHOW TABLES, information_schema and DESCRIBE show only what the account may see.
  • ALTER USER ... IDENTIFIED BY changes the password hashes; an account may change its own.
  • Privileges EXECUTE, CREATE ROUTINE, ALTER ROUTINE, TRIGGER, CREATE VIEW (as distinct from CREATE), SHOW VIEW, EVENT and LOCK TABLES can be granted and shown but are not enforced for those statements in this version.
  • The accounts are the front-end's own: they do not log in to Raft DB's native protocol.

Not supported yet#

Refused with error 1235:

  • Roles: CREATE ROLE, DROP ROLE, GRANT role TO user, SET ROLE (syntax error), proxy grants.
  • IDENTIFIED BY RANDOM PASSWORD.
  • Account options REQUIRE SSL|X509|CIPHER|ISSUER|SUBJECT, PASSWORD EXPIRE, PASSWORD HISTORY, PASSWORD REUSE INTERVAL, PASSWORD REQUIRE CURRENT, FAILED_LOGIN_ATTEMPTS, PASSWORD_LOCK_TIME, ACCOUNT LOCK, COMMENT, ATTRIBUTE, resource limits (WITH MAX_QUERIES_PER_HOUR and the others). Only ACCOUNT UNLOCK and REQUIRE NONE are accepted, because they change nothing.
  • Privileges on routines (GRANT ... ON PROCEDURE|FUNCTION ...).
  • SHOW CREATE USER and SHOW PRIVILEGES work (the hash of a caching_sha2_password account is Raft DB's, not the server's salted one).

Examples#

SQL
CREATE USER IF NOT EXISTS 'app'@'%' IDENTIFIED BY 'secret';
CREATE USER 'legacy'@'localhost' IDENTIFIED WITH mysql_native_password AS '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19';
GRANT SELECT, INSERT, UPDATE ON shop.* TO 'app'@'%';
GRANT SELECT (id, name) ON shop.cust TO 'report'@'%';
SHOW GRANTS FOR 'app'@'%';
REVOKE INSERT ON shop.* FROM 'app'@'%';
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'app'@'%';
ALTER USER 'app'@'%' IDENTIFIED BY 'new secret';
RENAME USER 'app'@'%' TO 'web'@'%';
DROP USER IF EXISTS 'web'@'%';

These statements are checked against MySQL and MariaDB.

See also#