1.修改配置文件:進入/etc/mysql/mysql.conf.d/ ,在配置文件mysqld.cnf下添加 skip-grant-tables
2.在終端輸入命令:sudo service mysql restart來重新啟動mysql服務
3.輸入:mysql -u root -p,回車進入mysql(此時不需要密碼即可直接進入)
4.輸入:use mysql;
5.輸入:update user set authentication_string='' where user='root';,將authentication_string 置空;

6.輸入:alter user 'root'@'localhost' identified by 'newpassword';,newpassword為要修改的新密碼;
此時提示錯誤:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

輸入:FLUSH PRIVILEGES;
再重新alter user 'root'@'localhost' identified by 'newpassword';
此時錯誤提示為:ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
,
輸入select user,plugin from mysql.user;來查看plugin

這里輸入update user set plugin='mysql_native_password' where user='root';將plugin改為以前版本的密碼認證方式

然后輸入:FLUSH PRIVILEGES;
再重試alter user 'root'@'localhost' identified by 'newpassword';即可修改密碼
7.修改完成后輸入FLUSH PRIVILEGES;
8. 將第一步的skip-grant-tables注釋掉;
9.輸入service mysql restart重啟mysql
