ALTER USER

This statement changes an existing user inside the TiDB privilege system. In the MySQL privilege system, a user is the combination of a username and the host from which they are connecting from. Thus, it is possible to create a user 'newuser2'@'192.168.1.1' who is only able to connect from the IP address 192.168.1.1. It is also possible to have two users have the same user-portion, and different permissions as they login from different hosts.

Synopsis

AlterUserStmt

ALTER USER - 图1

UserSpecList

ALTER USER - 图2

UserSpec

ALTER USER - 图3

RequireClauseOpt

ALTER USER - 图4

RequireList

ALTER USER - 图5

Username

ALTER USER - 图6

AuthOption

ALTER USER - 图7

PasswordOption

ALTER USER - 图8

LockOption

ALTER USER - 图9

AttributeOption

ALTER USER - 图10

ResourceGroupNameOption

ALTER USER - 图11

RequireClauseOpt

ALTER USER - 图12

RequireListElement

ALTER USER - 图13

  1. AlterUserStmt ::=
  2. 'ALTER' 'USER' IfExists (UserSpecList RequireClauseOpt ConnectionOptions PasswordOption LockOption AttributeOption | 'USER' '(' ')' 'IDENTIFIED' 'BY' AuthString) ResourceGroupNameOption
  3. UserSpecList ::=
  4. UserSpec ( ',' UserSpec )*
  5. UserSpec ::=
  6. Username AuthOption
  7. RequireClauseOpt ::=
  8. ( 'REQUIRE' 'NONE' | 'REQUIRE' 'SSL' | 'REQUIRE' 'X509' | 'REQUIRE' RequireList )?
  9. RequireList ::=
  10. ( "ISSUER" stringLit | "SUBJECT" stringLit | "CIPHER" stringLit | "SAN" stringLit | "TOKEN_ISSUER" stringLit )*
  11. Username ::=
  12. StringName ('@' StringName | singleAtIdentifier)? | 'CURRENT_USER' OptionalBraces
  13. AuthOption ::=
  14. ( 'IDENTIFIED' ( 'BY' ( AuthString | 'PASSWORD' HashString ) | 'WITH' StringName ( 'BY' AuthString | 'AS' HashString )? ) )?
  15. PasswordOption ::= ( 'PASSWORD' 'EXPIRE' ( 'DEFAULT' | 'NEVER' | 'INTERVAL' N 'DAY' )? | 'PASSWORD' 'HISTORY' ( 'DEFAULT' | N ) | 'PASSWORD' 'REUSE' 'INTERVAL' ( 'DEFAULT' | N 'DAY' ) | 'FAILED_LOGIN_ATTEMPTS' N | 'PASSWORD_LOCK_TIME' ( N | 'UNBOUNDED' ) )*
  16. LockOption ::= ( 'ACCOUNT' 'LOCK' | 'ACCOUNT' 'UNLOCK' )?
  17. AttributeOption ::= ( 'COMMENT' CommentString | 'ATTRIBUTE' AttributeString )?
  18. ResourceGroupNameOption::= ( 'RESOURCE' 'GROUP' Identifier)?
  19. RequireClauseOpt ::= ('REQUIRE' ('NONE' | 'SSL' | 'X509' | RequireListElement ('AND'? RequireListElement)*))?
  20. RequireListElement ::= 'ISSUER' Issuer | 'SUBJECT' Subject | 'CIPHER' Cipher | 'SAN' SAN | 'TOKEN_ISSUER' TokenIssuer

Examples

  1. mysql> CREATE USER 'newuser' IDENTIFIED BY 'newuserpassword';
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> SHOW CREATE USER 'newuser';
  4. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | CREATE USER for newuser@% |
  6. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  7. | CREATE USER 'newuser'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*5806E04BBEE79E1899964C6A04D68BCA69B1A879' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK |
  8. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  9. 1 row in set (0.00 sec)

Modify basic user information

Change the password for user newuser:

  1. mysql> ALTER USER 'newuser' IDENTIFIED BY 'newnewpassword';
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> SHOW CREATE USER 'newuser';
  4. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | CREATE USER for newuser@% |
  6. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  7. | CREATE USER 'newuser'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*FB8A1EA1353E8775CA836233E367FBDFCB37BE73' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK |
  8. +----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  9. 1 row in set (0.00 sec)

Lock the user newuser:

  1. ALTER USER 'newuser' ACCOUNT LOCK;
  1. Query OK, 0 rows affected (0.02 sec)

Modify the attributes of newuser:

  1. ALTER USER 'newuser' ATTRIBUTE '{"newAttr": "value", "deprecatedAttr": null}';
  2. SELECT * FROM information_schema.user_attributes;
  1. +-----------+------+--------------------------+
  2. | USER | HOST | ATTRIBUTE |
  3. +-----------+------+--------------------------+
  4. | newuser | % | {"newAttr": "value"} |
  5. +-----------+------+--------------------------+
  6. 1 rows in set (0.00 sec)

Modify the comment of newuser using ALTER USER ... COMMENT:

  1. ALTER USER 'newuser' COMMENT 'Here is the comment';
  2. SELECT * FROM information_schema.user_attributes;
  1. +-----------+------+--------------------------------------------------------+
  2. | USER | HOST | ATTRIBUTE |
  3. +-----------+------+--------------------------------------------------------+
  4. | newuser | % | {"comment": "Here is the comment", "newAttr": "value"} |
  5. +-----------+------+--------------------------------------------------------+
  6. 1 rows in set (0.00 sec)

Remove the comment of newuser using ALTER USER ... ATTRIBUTE:

  1. ALTER USER 'newuser' ATTRIBUTE '{"comment": null}';
  2. SELECT * FROM information_schema.user_attributes;
  1. +-----------+------+---------------------------+
  2. | USER | HOST | ATTRIBUTE |
  3. +-----------+------+---------------------------+
  4. | newuser | % | {"newAttr": "value"} |
  5. +-----------+------+---------------------------+
  6. 1 rows in set (0.00 sec)

Change the automatic password expiration policy for newuser to never expire via ALTER USER ... PASSWORD EXPIRE NEVER:

  1. ALTER USER 'newuser' PASSWORD EXPIRE NEVER;
  1. Query OK, 0 rows affected (0.02 sec)

Modify the password reuse policy for newuser to disallow the reuse of any password used within the last 90 days using ALTER USER ... PASSWORD REUSE INTERVAL ... DAY:

  1. ALTER USER 'newuser' PASSWORD REUSE INTERVAL 90 DAY;
  1. Query OK, 0 rows affected (0.02 sec)

Modify the resource group bound to the user

Use ALTER USER ... RESOURCE GROUP to modify the resource group of the user newuser to rg1.

  1. ALTER USER 'newuser' RESOURCE GROUP rg1;
  1. Query OK, 0 rows affected (0.02 sec)

View the resource group bound to the current user:

  1. SELECT USER, JSON_EXTRACT(User_attributes, "$.resource_group") FROM mysql.user WHERE user = "newuser";
  1. +---------+---------------------------------------------------+
  2. | USER | JSON_EXTRACT(User_attributes, "$.resource_group") |
  3. +---------+---------------------------------------------------+
  4. | newuser | "rg1" |
  5. +---------+---------------------------------------------------+
  6. 1 row in set (0.02 sec)

Unbind the user to a resource group, that is, bind the user to the default resource group.

  1. ALTER USER 'newuser' RESOURCE GROUP `default`;
  2. SELECT USER, JSON_EXTRACT(User_attributes, "$.resource_group") FROM mysql.user WHERE user = "newuser";
  1. +---------+---------------------------------------------------+
  2. | USER | JSON_EXTRACT(User_attributes, "$.resource_group") |
  3. +---------+---------------------------------------------------+
  4. | newuser | "default" |
  5. +---------+---------------------------------------------------+
  6. 1 row in set (0.02 sec)

See also