Home  >  Article  >  Database  >  How to set a password for mysql root

How to set a password for mysql root

PHPz
PHPzOriginal
2023-04-20 10:06:482751browse

MySQL is currently the most popular relational database management system, and its flexibility and powerful functions have been widely recognized. However, many MySQL users neglect to set the root user's password for convenience, which also brings security risks. So, how to set a password for MySQL root?

  1. Log in to the MySQL command line

Before setting the root password, you need to log in to the MySQL command line. You can log in through the following command:

mysql -u root

If the MySQL service is password protected, you also need to enter the password to log in. Otherwise, just press Enter in the pop-up prompt box.

  1. Modify the root password

Enter the following command in the command line to modify the root password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';

where'new_password' Replace with the new password you want to set. If your MySQL version is lower than 5.7.6, you can use the following command:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

At this point, the root user password has been successfully modified. However, in a production environment, new passwords may need to be encrypted for maximum security.

  1. Encrypt root password

In order to better protect the security of the MySQL database, it is often necessary to encrypt the password. MySQL has a built-in MD5 function, which can convert a string into an MD5 encrypted form. In the MySQL command line, you can use the following statement to encrypt the root password with MD5:

mysql> UPDATE user SET password=PASSWORD(MD5('new_password')) WHERE user='root';

Among them, 'new_password' is replaced with the new password you want to set. At this point, the root user's password has been successfully encrypted. Even if a hacker breaks into your server, they cannot easily obtain your password information.

  1. Refresh permissions

Please note that after changing the password, you need to refresh the permissions to take effect. In the MySQL command line, enter the following command to refresh permissions:

mysql> FLUSH PRIVILEGES;
  1. Disconnect

Finally, to be on the safe side, we need to disconnect, which can be done via The following command is implemented:

mysql> exit;
  1. Summary

Through the above steps, we successfully set the password of the MySQL root user and encrypted it, so as to better Protect MySQL data security. In practical applications, we should frequently update the root password and choose a stronger password to enhance security.

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