How to change the password for a database in MySQL?

To reset the MySQL database password, you can follow these steps:

  1. Log in to the MySQL database server using an administrator account.
  2. Open the command line window or use the MySQL client tool.
  3. Enter the following command to change the password:
ALTER USER '用户名'@'localhost' IDENTIFIED BY '新密码';

Replace the username with the username of the user whose password needs to be changed, and replace the new password with the new password to be set.

If the user to be modified is a remote access user, you need to change “localhost” to the corresponding remote hostname or IP address.

  1. After executing the command, the MySQL database will change the password to the new one.

Before the MySQL 8.0 version, you could modify passwords using the SET PASSWORD command with the following syntax.

SET PASSWORD FOR '用户名'@'localhost' = PASSWORD('新密码');

However, starting from version 8.0 of MySQL, it is recommended by the official source to use the ALTER USER command to change passwords.

If you forget the password for the admin account, you can also reset the database password by resetting the root account password. For specific instructions, please refer to the official MySQL documentation or relevant tutorials.

bannerAds