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