自己在遇到了这个问题后在查看了许多文章后找到了如下解决方案
1.进入mysql
1)通过管理员身份进入、
输入以下命令进入
sudo mysql -u root -p
2)通过配置文件 /etc/mysql/debian.cnf中的信息
将mysql -u root -p命令中的“root”即用户改为配置文件中的user后字段,之后将password字段输入进密码进入mysql
2.查找user表
在mysql中输入如下命令显示表内容
select user, authentication_string,plugin from mysql.user
与许多大佬教程里的plugin的mysql_native_password不同,mysql8.0使用的是新的密码规则
3.修改root密码
原本是输入以下命令即可,但由于mysql8.0在user表加了字段authentication_string,所以其修改方式会有些不同
MySQL 5.7
update mysql.user set authentication_string=PASSWORD('123456'), plugin='mysql_native_password' where user='root';
mysql8.0
1)置空字段
update mysql.user set authentication_string='', plugin='mysql_native_password' where user='root';
其中密码规则我仍使用了原来的mysql_native_password,各位也可以自行设置。
2)修改字段
ALTER user 'root'@'localhost' IDENTIFIED BY 'root'; root为要修改的密码
3)刷新
flush privileges;
成功: