How to change password in MySQL

小老鼠
Release: 2023-09-05 13:49:17
Original
3171 people have browsed it

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;
Copy after login

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('新密码');
Copy after login

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 新密码
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!