Home>Article>Database> How to change password in MySQL

How to change password in MySQL

小老鼠
小老鼠 Original
2023-09-05 13:49:17 3187browse

Methods for changing passwords in MySQL include using the UPDATE statement to change the password, using the SET PASSWORD statement to change the password, using the mysqladmin command to change the password, using the GRANT statement to create a new user and set a password, etc. : 1. Use the UPDATE statement to modify. If you have sufficient permissions, you can use the UPDATE statement to directly modify the MySQL user's password; 2. Use the SET PASSWORD statement to modify the password, etc.

How to change password in MySQL

In MySQL, there are several ways to change your password, depending on your access rights and MySQL version. The following are several common methods:

1. Use the UPDATE statement to change the password: If you have sufficient permissions, you can use the UPDATE statement to directly change the MySQL user's password. In the MySQL command line or client, execute the following command:

UPDATE mysql.user SET Password = PASSWORD('新密码') WHERE User = '用户名'; FLUSH PRIVILEGES;

Replace 'new password' in the above command with the new password you want to set, and replace 'username' with the user whose password you want to change. name. After executing the UPDATE statement, use the FLUSH PRIVILEGES command to refresh the permissions.

2. Use the SET PASSWORD statement to change the password: In MySQL 5.7.6 and later versions, you can use the SET PASSWORD statement to change the password. In the MySQL command line or client, execute the following command:

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

Replace 'new password' in the above command with the new password you want to set, and replace 'username' with the user whose password you want to change. name.

3. Use the mysqladmin command to change the password: If you have root or a user with sufficient permissions, you can use the mysqladmin command to change the password. Execute the following command in the terminal:

mysqladmin -u 用户名 -p password 新密码

After entering the above command, the system will prompt for a password, enter the password of the current user, and then enter the new password you want to set.

4. Use the GRANT statement to create a new user and set a password: If you do not have sufficient permissions to change the password of an existing user, you can use the GRANT statement to create a new user and set a password. In the MySQL command line or client, execute the following command:

CREATE USER '新用户名'@'localhost' IDENTIFIED BY '新密码';GRANT ALL PRIVILEGES ON *.* TO '新用户名'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;

Replace 'new username' in the above command with the new username you want to create, and replace 'new password' with the new username you want to set. new password. After executing the GRANT statement, use the FLUSH PRIVILEGES command to refresh the permissions.

No matter which method you use to change the password, make sure the new password is a strong password and keep the password properly to ensure the security of the database.

The above is the detailed content of How to change password in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn