修改配置文件 /etc/my.cnf
- 在此文件中添加
skip-grant-tables - 重启mysql服务
systemctl restart mysqld - 登录mysql
mysql -u root -p,回车,会出现输入密码提示,直接回车,可以登录成功
把密码改为空
-
切换当前数据库
use mysql; -
通过语句
select user, host from user;查看root用户是否存在当前系统 -
可以看到如下
%表示root用户登录的地址匹配所有IP -
将密码置空
update user set authentication_string='' where user='root';
删除配置文件/etc/my.cnf中的skip-grant-tables,重新登录,修改密码
- 删除配置文件中的
skip-grant-tables,重启mysql服务systemctl restart mysqld - 在控制台
mysql -u root -p,直接两次回车,无密码登录 - 修改密码:
alter user 'root'@'%' identified by 'new passwd'; - 重启mysql服务
systemctl restart mysqld, 使用新密码登录
注意
- mysql5.7.9版本之后废弃了
password字段和password()函数,不能再使用update user set authentication_string=password('123123') where user='root来修改密码 - mysql8.0的加密方式默认是
caching_sha2_password,而不是mysql_native_password