在连接服务器后,操作MySQL数据库mysql -u username -p

use mysql;

select host,user from user where user='root';

update user set host='%' where user='root' and host='localhost';

flush privileges;

select host,user from user where user='root';

alter user 'root'@'%' identified with mysql_native_password by '123456';

flush privileges;

注意,这里最后一行是把root密码设置为123456,可以把自定义成自己的密码

如果是遇到root连不上数据库的问题,可能是加密规则变了,试试如下处理,这里修改加密规则,这里密码用的是123456,可以改成自己的密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

FLUSH PRIVILEGES;