阿里云(CentOS)中MySQL8忘记密码的解决方法

阿里云(CentOS)中MySQL8忘记密码的解决方法

方法

skip-grant-tables 模式下启动 MySQL,该模式下启动 MySQL 时不启动授权表功能,可以直接免密码登录

实现

  1. 编辑 /etc/my.cnf 文件

    vim /etc/my.cnf
    
    阿里云(CentOS)中MySQL8忘记密码的解决方法_第1张图片
  2. [mysqld] 区域末尾添加配置,设置免密登录

    skip-grant-tables
    
  3. 重启 MySQL 服务

    systemctl restart mysqld
    
  4. 登录 MySQL

    mysql
    
  5. 修改 root 密码为空

    update mysql.user set authentication_string=''  where User='root';
    
  6. 刷新权限,退出

    flush privileges;
    quit;
    
    阿里云(CentOS)中MySQL8忘记密码的解决方法_第2张图片
  7. /etc/my.cnf 中的 skip-grant-tables 去掉

  8. 再次重启 MySQL 服务

    mysql
    
  9. 再次登录 MySQL,密码为空,修改 root 密码为新密码

    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xxxxxx';
    
  10. 再次刷新权限,就可以进行访问了

    flush privileges;
    

你可能感兴趣的:(阿里云,centos,mysql)