Step 1: First log in to MySQL
Enter the following command in the terminal to enter the MySQL console:
sudo mysql
If you enter this You will be prompted for a password after the command. Please enter the password of your root user.
Step 2: Change the password
After we enter the MySQL console, we can start to change the password of the root user. Follow the command below to set the password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
'new_password' is the new password you want to set.
Step 3: Refresh permissions
After running the above command, you need to refresh the permissions so that MySQL can use the new password to authenticate the root account. Enter the following command in the MySQL console:
FLUSH PRIVILEGES;
Step 4: Exit MySQL
After completing the password change, you can exit MySQL through the following command:
exit
Now you have successfully changed the password of the root account.
How to set a secure MySQL root password
For a network-based service, password security is very important. If a hacker cracks the root account, they can easily access the database, which will have serious consequences for the user and the company. Therefore, when setting the MySQL root password, you must abide by the following rules:
The password must be no less than 10 characters long
The password consists of letters , numbers and symbols
Avoid using easy-to-guess passwords, such as birthday, 123456, etc.
Avoid using public information, such as company name or Your name
Change your password regularly
If you are not sure how to set a strong password, you can use some password generator tools, such as 1Password or LastPass.
The above is the detailed content of How to set password for root in mysql. For more information, please follow other related articles on the PHP Chinese website!