MySQL is a popular relational database management system that can be widely used in various Web applications. When developing and maintaining MySQL databases, it is often necessary to change user passwords to ensure data security. This article will introduce how to change user password in MySQL.
In MySQL, each user has a username and password. The username is used to identify the user, while the password is used to authenticate the user. When creating a user in MySQL, you must provide a password to the database. If you need to change your password, you can use the following steps:
To connect to the MySQL database, you need to use the MySQL client. You can connect to the MySQL server using the following command:
mysql -u root -p
This command will connect to the MySQL server and prompt you for the password of the root user. If everything is fine, you should now be in the MySQL command prompt.
To change the user password, you need to know the user name whose password you want to change. You can list all users in MySQL using the following command:
SELECT User FROM mysql.user;
This command will list the usernames of all MySQL users. Select the user whose password you want to change and note their name for later steps.
Update user password using the following command:
UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='username';
In this command, replace newpassword with the new one you want to assign to the user password and replace username with the name of the user whose password you want to update. The password needs to be enclosed in single quotes.
After changing the password, you need to refresh the MySQL privilege table for the change to take effect:
FLUSH PRIVILEGES;
This command will make MySQL restart Load the privilege table to make the new password effective.
Use the following command to exit the MySQL client:
quit
Example of modifying the MySQL user password
Assumptions You want to change the password of the user named john. Use the following command:
UPDATE mysql.user SET Password=PASSWORD('mynewpassword') WHERE User='john';
This command will assign a new password mynewpassword to the user named john.
Summary
In MySQL, changing a user password is a very basic operation. By following the steps above, you can more easily and securely change your MySQL user password. Also remember to refresh MySQL's privilege table after changing the password to ensure that the new password takes effect immediately.
The above is the detailed content of mysql change user's password. For more information, please follow other related articles on the PHP Chinese website!