1. End the currently running mysql process.
service mysql stop
2. Run in mysql safe mode and skip permission verification.
/www/server/mysql/bin/mysqld_safe --skip-grant-tables &
3. Reopen a terminal window and log in to mysql as root.
Enter: mysql -u root -p command and press Enter. When you need to enter a password, just press the enter key to log in to the database without a password.
4. Change password
mysql> update user set password=password('password') where user='root';
If the following error occurs:
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
The reason is that there is no password field in the mysql database. The password field can be changed to authentication_string.
update user set authentication_string=PASSWORD('password') where Use
r='root';
5. Reload the system permission table
mysql> flush privileges;
6. Close the safe mode operation (close the terminal window just running in safe mode)
service mysql stop
7. Start the database
service mysql start