Home >Database >Mysql Tutorial >How to change mysql password

How to change mysql password

PHPz
PHPzOriginal
2023-04-17 16:44:591672browse

MySQL is a commonly used relational database management system. When we start using MySQL, the first thing we need to do is set a password. However, sometimes we forget our password or need to change it. In this article, I will introduce to you how to change the MySQL password.

Step 1: Shut down MySQL

First you need to shut down MySQL, because it is unsafe to change the password without shutting down MySQL. The command to close MySQL is as follows:

sudo /etc/init.d/mysql stop

Step 2: Enter MySQL

Next to enter MySQL, you need to log in with the root account. Enter the following command in the bash shell:

sudo mysql -u root

After entering the command, the following prompt will appear, asking for a password:

Enter password:

If your password is correct, you can see the following output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.28-0ubuntu0.18.04.4 (Ubuntu)
...
mysql>

Step 3: Change the password

Then you can change the password. After entering MySQL, enter the following SQL command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your-new-password';

where your-new-password is the new password you want to set. After executing the above command, if no error is reported, it means that the password has been changed successfully.

Step 4: Start MySQL

After completing the password change, you need to restart MySQL. You can enter the following command:

sudo /etc/init.d/mysql start

At this point, the entire process of changing the MySQL password is completed.

Summary

MySQL is a very excellent database system, which is widely used in many websites and business systems. When we use MySQL, in order to ensure the security of the system, we need to set a strong enough password. If the password is leaked or forgotten, you can use the above method to successfully change the MySQL password.

The above is the detailed content of How to change mysql password. 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