Home >Database >Mysql Tutorial >How to change user password in MySQL? (code example)
In MySQL, you can use 3 different statements to change the user account password:
UPDATE statement SET PASSWORD statement ALTER USER statement.
But before changing the account password, you should remember Live two very important things:
- The user account details for which the password is to be changed.
- The user whose password is to be changed is using the application because the application will not be able to connect to the database server if the password is changed without changing the application's connection string.
Now let us learn how to change user password in SQL using the three SQL statements mentioned above:
1. Change MySQL user password using SET PASSWORD statement
To use the SET PASSWORD statement to change a user's password, the first requirement is that the account needs to have at least UPDATE permissions.
The user account should be in the format "user@host" and you want to update its password.
You want to use the SET PASSWORD statement to change the password of user account "gfguser1" to "newpass", you should execute the following code statement:
Syntax:
2. Use the ALTER USER statement to change the MySQL user password
The second method of changing the user account password is to use the ALTER USER statement.
ALTER USER statement is used with the "IDENTIFIED BY" clause.
To use the Alter User statement to change the password of user account "gfguser1" to "newpass", the syntax is as follows:
Syntax:
3. Use the UPDATE statement to change the MySQL user password
The third way to change the user account password is to use the UPDATE statement.
Update statement updates the user table of the mysql database.
The FLUSH PRIVILEGES statement needs to be executed after the UPDATE statement is executed.
FLUSH PRIVILEGES statement is used to reload permissions from the grant table in the mysql database.
To change the password of user account "gfguser1" to "newpass" connecting from the localhost server, the syntax is as follows:
Syntax:
This article is about the method of changing user password in MySQL. Recommended related mysql video tutorials: "mysql tutorial"
The above is the detailed content of How to change user password in MySQL? (code example). For more information, please follow other related articles on the PHP Chinese website!