In the previous article, we have finished the installation of MySQL, the next part of this article will show how to reset the password.
After the installation is complete, MySQL will automatically generate a random password for us
The view command is as follows:
grep 'temporary password'/var/log/mysqld.log
Because the automatically generated password cannot be used directly and is not easy for us to remember, we need to change the password.
Edit MySQL configuration file
vim /etc/my.cnf
Add the following sentence in the following line at the beginning of pid
skip-grant-tables
Save and exit.
service mysqld restart
mysql -u root -p
When prompted to enter the password, hit enter directly.
use mysql;
Because a user table of MySQL users is stored in the mysql database
select host, user, authentication_string, plugin from user;
After executing the above command, a table will be displayed
The following information is in the form:
host: the ip'location' that allows the user to log in% means that it can be remote;
user: the user name of the current database;
authentication_string: user password (password field and password() function are discarded after mysql 5.7.9);
plugin: Password encryption method;
use mysql;
update user set authentication_string='' where user='root';
quit
vim /etc/my.cnf
Delete skip-grant-tables and save and exit.
service mysqld restart
mysql -u root -p
When prompted to enter the password, hit enter, because we have set the password to be blank just now.
ALTER user 'root'@'localhost' IDENTIFIED BY 'Xpf123@';
Xpf123@ is the new password you set. Note that if this password is set relatively simple, such as 123456, etc., it will be unsuccessful. It will prompt you that the password is too simple. It is best to set it to uppercase letters, numbers, and symbols. combination.
After the execution, you will be prompted with OK, it means that the modification is successful. So far, resetting the password is complete. You can use the newly set password to log in and try.
Many students may have encountered many problems in changing the password. For example, if you installed MySQL 8.0 and then reset the password, or used the old command to change the password, an error was reported because the user table was discarded after MySQL 5.7.6 Password field and password() method, so using the old method to reset the password will not work for mysql 8.0!
Okay, that's all for today's explanation.
If you have any questions, please leave a message below!
For more technical articles, please pay attention to my official account:
Recommended Posts