- 检查mysql服务是否启动,如果启动,关闭mysql服务
- 修改mysql的配置文件my.cnf
my.cnf配置文件的位置,一般在/etc/my.cnf,有些版本在/etc/mysql/my.cnf
在配置文件中,增加2行代码:
[mysqld]
skip-grant-tables
作用是登录mysql的时候跳过密码验证
2. 然后启动mysql服务
3. 连接mysql这个数据库
4. 查看用户表
命令:select user,plugin from user;
5. 修改root密码格式
update user set plugin='mysql_native_password' where user='root'; # 修改其密码格式
select user,plugin from user; # 查询其用户
6. 刷新权限
(可以理解为高并发,可能还没处理完数据让他缓存,就执行下一步操作可能会出错)
命令:flush privileges;
7. 增加root密码
alter user 'root'@'localhost' identified by '密码';
8. 再次刷新权限
命令:flush privileges;
9. 退出登录
命令:exit
10. 将my.cnf中输入的那俩行删去 / 注释掉
11. 重启mysql服务,就可以使用刚刚设置的密码登录了。