skip-grant-tables 加入到mysqld中
mysql -uroot -p
查看root的位置
use mysql;
select user,host from user;
use mysql;
update user set authentication_string='' where user='root';
flush privileges;
alter user 'root'@'localhost' identified by 'newpassword';
flush privileges;
完成后Ctrl+Z退出mysql,使用新密码重新登录即可。
去掉刚才加的东西后
/bin/systemctl restart mysqld.service
! 如果使用Navicat等可视化界面连接数据库时连接不成功的情况,如错误信息:2059。此时需要更改验证方式,使用命令提示符登录数据库,输入: ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘yourpassword’; yourpassword是你想要设置的密码。
https://www.cnblogs.com/mzxiaoze/p/10413399.html