How can we change MySQL user password by using UPDATE statement?



To change MySQL user password with the help of UPDATE statement, we need to update the β€˜user’ table of the β€˜mysql’ database. Its syntax would be as follows βˆ’

Syntax

USE mysql;
UPDATE user
SET authentication_string = PASSWORD(β€˜new_password’)
WHERE user = user_name AND host = host_name;

The first two statements will be common because to change the password for MySQL user we need to use MySQL database and update the user table.

  • New_password would be new password we want to set for MySQL user
  • User_name is the name of the current user.
  • Host_name is the name of the host of the current user.

Example

Suppose if we want to change the password user@localhost to β€˜tutorials’ then it can be done as follows βˆ’

USE mysql;
UPDATE user
SET authentication_string = PASSWORD('tutorials')
WHERE user = 'user' AND
   host = 'localhost';
FLUSH PRIVILEGES;
Updated on: 2020-06-20T11:43:23+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements