MySQL - SET PASSWORD Statement

You can password to an existing user using the MySQL SET PASSWORD Statement. Using this you can
Set a specific password to a user account.
Set a random password.
Replace the current password with a new one.
Retain the current password as its secondary password.
Following is the syntax of the MySQL SET PASSWORD Statement −
SET PASSWORD [FOR user_name] new_password
   [REPLACE 'current_password_string']
   [RETAIN CURRENT PASSWORD]

Where, user_name is the name of the user for which you need to change the password.
Assume we have created a user named sample (without any password) using the CREATE USER Statement as shown below −
mysql> CREATE USER 'tp'@'localhost';
Query OK, 0 rows affected (0.34 sec)
To login as the above created user. Open command prompt, browse through the bin folder of MySQL installation folder and execute the command mysql -u user_name -p as −
MySQL_Installation_Directorybin>mysql -u tp
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL SET PASSWORD Statement

mysql>
Now you can assign a password of the current user account as shown below −
mysql> SET PASSWORD ='mypassword123';
Query OK, 0 rows affected (0.59 sec)
You set/assign password to a specific user using the FOR clause in the SET PASSWORD Statement −
SET PASSWORD FOR user_name password_string
Assume we have created a user named test using the CREATE USER statement.
mysql> CREATE USER 'test'@'localhost';
Query OK, 0 rows affected (0.27 sec)
Following query assigns password for the above created user −
mysql> SET PASSWORD FOR 'test'@'localhost' = 'mypassword';
Query OK, 0 rows affected (0.21 sec)
You can replace the existing password using the REPLACE clause along with the SET PASSWORD statement.
SET PASSWORD = new_password REPLACE old_password;
Assume we have created a user named test along with a password, using the CREATE USER statement.
mysql> CREATE USER test@localhost IDENTIFIED BY 'test12345';
Query OK, 0 rows affected (0.18 sec)
Login as the above created user. (Open command prompt, browse through the bin folder of MySQL installation folder and execute the following query) −
MySQL_Installation_Directorybin>mysql -u tp -p
*********
Following query replaces the password of the above created user −
mysql> SET PASSWORD = 'newpassword' REPLACE 'test12345';
Query OK, 0 rows affected (0.12 sec)
You can use REPLACE only for the current user
You can assign random password using the RANDOM clause along with the SET PASSWORD Statement.
SET PASSWORD TO RANDOM;
Assume we have created a user named sample (without any password) using the CREATE USER Statement as shown below −
mysql> CREATE USER 'tp'@'localhost';
Query OK, 0 rows affected (0.17 sec)
Following query sets a random password of the above created user −
mysql> SET PASSWORD FOR 'tp'@'localhost' TO RANDOM;
+------+-----------+----------------------+
| user | host      | generated password   |
+------+-----------+----------------------+
| tp   | localhost | hTWnSP:h0eAe(ywVJArm |
+------+-----------+----------------------+
1 row in set (0.17 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