The User password can be changed using the Update, Set password, and Alter user statements. We will look at each of these statements in turn.
We can reset a user’s password using the Update statements.
#Example of a user with a specific hostname
UPDATE user
SET authentication_string = 'dolphin'
WHERE user = 'dbadmin';
#Example of a user without a specific hostname.
Update user
set authentication_string = 'newPassword'
where user = 'dev_user_1';
FLUSH PRIVILEGES
after each update, this statement reloads all the privileges in the Grants table for the user.
FLUSH PRIVILEGES;
We can use the SET PASSWORD
statement. The syntax for the construct is as follows:
SET PASSWORD FOR <username>@<hostname> = ‘<new Password>’
%
hostname we can skip the @hostname part.We can also use the Alter User statement along with the IDENTIFIED BY
Clause to change the password. The Basic Syntax of the statement is as follows:
ALTER USER <username>@<hostname> IDENTIFIED BY '<newpassword>';
For a User with no hostname specified,
ALTER USER dev_user_4 IDENTIFIED BY 'pass1';
For a user with a specific hostname,
ALTER USER dev_user_3@localhost IDENTIFIED BY 'pass100';
The Alter User statement will fail if the hostname is not specified.