MySQL ALTER USER Statement

You can modify an existing account in MySQL using the ALTER USER Statement. Using this statement, you can change the authentication, role, SSL/TLS, resource-limit, and password-management properties.
To execute this statement, the current account needs to have CREATE USER or, ALTER USER privilege.
Following is the syntax of the MySQL CREATE USER Statement −
ALTER USER [IF EXISTS]
   user [auth_option] [, user [auth_option]] ...
   [REQUIRE {NONE | tls_option [[AND] tls_option] ...}]
   [WITH resource_option [resource_option] ...]
   [password_option | lock_option] ...
   [COMMENT 'comment_string' | ATTRIBUTE 'json_object']

Assume we have created a user using the CREATE USER statement.
mysql> CREATE USER 'sample'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.12 sec)
Following query changes the password of the above created user −
mysql> ALTER USER 'sample'@'localhost' IDENTIFIED BY 'testpassword';
Query OK, 0 rows affected (0.17 sec)
You can modify the password of the current user using the following query −
mysql> ALTER USER USER() IDENTIFIED BY 'MyPassword';
Query OK, 0 rows affected (0.27 sec)
Assume we have created a new user by adding a comment as −
mysql> drop user sample@localhost; 
mysql> CREATE USER 'sample'@'localhost' COMMENT 'Sample information';
Query OK, 0 rows affected (0.10 sec)
You can verify he attributes and comments info as −
mysql> SELECT * FROM INFORMATION_SCHEMA.USER_ATTRIBUTES WHERE USER='sample' AND HOST='localhost';
+--------+-----------+-----------------------------------+
| USER   | HOST      | ATTRIBUTE                         |
+--------+-----------+-----------------------------------+
| sample | localhost | {"comment": "Sample information"} |
+--------+-----------+-----------------------------------+
1 row in set (0.00 sec)
Following query changes the comment created above −
mysql> ALTER USER 'sample'@'localhost' COMMENT 'new information';
Query OK, 0 rows affected (0.15 sec)
You can verify the information about attributes and comments as follows −
mysql> SELECT * FROM INFORMATION_SCHEMA.USER_ATTRIBUTES WHERE USER='sample' AND HOST='localhost';
+--------+-----------+--------------------------------+
| USER   | HOST      | ATTRIBUTE                      |
+--------+-----------+--------------------------------+
| sample | localhost | {"comment": "new information"} |
+--------+-----------+--------------------------------+
1 row in set (0.07 sec)
Assume we have created a new user along with attributes as −
mysql> drop user sample@localhost; 
mysql> CREATE USER 'sample'@'localhost' ATTRIBUTE '{"attr1": "val1", "attr2": "val2"}'; 
Query OK, 0 rows affected (0.24 sec)
You can verify he attributes and comments info as −
mysql> SELECT * FROM INFORMATION_SCHEMA.USER_ATTRIBUTES WHERE USER='sample' AND HOST='localhost';
+--------+-----------+------------------------------------+
| USER   | HOST      | ATTRIBUTE                          |
+--------+-----------+------------------------------------+
| sample | localhost | {"attr1": "val1", "attr2": "val2"} |
+--------+-----------+------------------------------------+
1 row in set (0.00 sec)
Following query alters the attributes created above −
ALTER USER 'sample'@'localhost' ATTRIBUTE '{"attr1": "newval1", "attr2": "newval2"}';
You can verify the information about attributes and comments as follows −
mysql> SELECT * FROM INFORMATION_SCHEMA.USER_ATTRIBUTES WHERE USER='sample' AND HOST='localhost'; 
+--------+-----------+------------------------------------------+ 
| USER   | HOST      | ATTRIBUTE                                | 
+--------+-----------+------------------------------------------+ 
| sample | localhost | {"attr1": "newval1", "attr2": "newval2"} | 
+--------+-----------+------------------------------------------+ 
1 row in set (0.06 sec)
We can create a role using the CREATE ROLE statement as −
mysql> CREATE ROLE 'TestAdmin', 'TestDeveloper';
Query OK, 0 rows affected (0.30 sec)
Grant privileges to above created roles −
mysql> GRANT ALL PRIVILEGES ON * . * TO TestAdmin, TestDeveloper;
Query OK, 0 rows affected (0.83 sec)
Assume we have created a user with default role as shown below −
mysql> CREATE USER IF NOT EXISTS testuser@localhost DEFAULT ROLE 'TestAdmin';
Query OK, 0 rows affected (0.15 sec)
Grant privileges to the created user −
mysql> GRANT ALL PRIVILEGES ON * . * TO 'testuser'@'localhost';
Query OK, 0 rows affected (0.56 sec)
Following query alters the role of the above created user −
mysql> ALTER USER testuser@localhost DEFAULT ROLE 'TestAdmin';
Query OK, 0 rows affected (0.03 sec)
If you try to alter a user that doesn’t exist error will be generated as shown below −
mysql> ALTER USER demo@localhost;
ERROR 1396 (HY000): Operation ALTER USER failed for 'demo'@'localhost'
If you use the IF EXISTS clause along with the ALTER FUNCTION statement as shown below, the specified user will be altered and if a function with the given name, doesn’t exist the query will be ignored.
mysql> ALTER USER IF EXISTS sample;
Query OK, 0 rows affected, 1 warning (0.18 sec)
If you use the expire clause the old password it will expire immediately and the user need to choose new password at first connection.
mysql> drop user sample@localhost;
mysql> CREATE USER 'sample'@'localhost' IDENTIFIED BY '123456';
Following query alters the above created password.
mysql> ALTER USER 'sample'@'localhost' IDENTIFIED BY 'MyPassword' PASSWORD EXPIRE;
Query OK, 0 rows affected (0.25 sec)
You can also set interval to the expire clause as shown below −
mysql> drop user sample@localhost;
mysql> CREATE USER 'sample'@'localhost'
   IDENTIFIED BY 'MyPassword'
   PASSWORD EXPIRE INTERVAL 25 DAY
   FAILED_LOGIN_ATTEMPTS 5 PASSWORD_LOCK_TIME 1;
Query OK, 0 rows affected (0.20 sec)
Following query alters the above created password and its interval.
mysql> ALTER USER 'sample'@'localhost'
   IDENTIFIED BY 'testpassword'
   PASSWORD EXPIRE INTERVAL 225 DAY
   FAILED_LOGIN_ATTEMPTS 2 PASSWORD_LOCK_TIME 1;
Query OK, 0 rows affected (0.13 sec)
You can lock and unlock an existing user using the alter statement −
mysql> ALTER USER 'sample'@'localhost' ACCOUNT LOCK;
Query OK, 0 rows affected (0.20 sec)

mysql> ALTER USER 'sample'@'localhost' ACCOUNT UNLOCK;
Query OK, 0 rows affected (0.14 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page