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#
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_nameArguments#
| Name | Meaning | Default |
|---|---|---|
host | %, a name, an address, or a pattern. localhost matches loopback. | % |
plugin | mysql_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_type | SELECT, 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 level | db.* may be a pattern with %. | none |
Remarks#
- Login. A client logs in as the stored account with the most specific host that matches (
%,localhostfor loopback, patterns, exact address). - Which privilege runs what.
CREATE USERis needed forCREATE USER,ALTER USER(of another account),DROP USER,RENAME USER, andSET PASSWORD FORanother account;RELOADforFLUSH;SUPERforSET GLOBAL. Data statements are checked from their parsed form: the privilege, database, table and columns each needs (SELECTalso for the columns ofWHERE,ORDER BYandUPDATEexpressions;REPLACEneedsDELETEtoo;TRUNCATEneedsDROP;ALTER TABLE ... RENAMEneedsDROPon the old andCREATE/INSERTon 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 (
GRANTto 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
GRANTandREVOKEact on the next statement of open connections. SHOW GRANTSuses MySQL's canonical order and theALL PRIVILEGEStext.SHOW DATABASES,SHOW TABLES,information_schemaandDESCRIBEshow only what the account may see.ALTER USER ... IDENTIFIED BYchanges the password hashes; an account may change its own.- Privileges
EXECUTE,CREATE ROUTINE,ALTER ROUTINE,TRIGGER,CREATE VIEW(as distinct fromCREATE),SHOW VIEW,EVENTandLOCK TABLEScan 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_HOURand the others). OnlyACCOUNT UNLOCKandREQUIRE NONEare accepted, because they change nothing. - Privileges on routines (
GRANT ... ON PROCEDURE|FUNCTION ...). SHOW CREATE USERandSHOW PRIVILEGESwork (the hash of acaching_sha2_passwordaccount is Raft DB's, not the server's salted one).
Examples#
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.