1. First edit the /etc/my.cnf file
vim /etc/my.cnf
Add a line to the file to configure skip-grant-tables
For example, add it to this location
# read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock skip-grant-tables
Then save and exit
2. Restart the mysql service
service mysqld restart
In this way, you can skip the verification and directly enter mysql
3. Enter mysql
mysql
4. Execute sql statements in sequence
>use mysql; change database >UPDATE user SET PASSORD =password('The new password you want to set') WHERE USER='root'; reset password >flush privileges; Refresh the MySQL system privilege related tables to prevent denial of access after changes; or restart the MySQL server >quit;
Note:
If you reset the password and report an error (ERROR 1054 (42S22): Unknown column 'PASSORD' in 'field list'
Use
>update mysql.user set authentication_string=password('The new password you want to set ') where user='root' ;
5. Edit the /etc/my.cnf file again
vim /etc/my.cnf
Remove the skip-grant-tables configuration, save and exit
6. Restart the mysql service
service mysqld restart
At this point, you can log in with the new password!
mysql -uroot -p
This article comes from the PHP Chinese website, Linux system tutorial column, please continue to pay attention to this column for more related tutorials !
The above is the detailed content of what to do if the MySQL password is forgotten in Linux. For more information, please pay attention to other related articles on 1024programmer.com!