环境:MySQL 8.0 + Windowsmysql
因为密码错误或者其余缘由致使没法连上MySQL服务,以下图:sql
解决该问题的具体步骤以下:测试
1.关闭MySQL服务加密
以管理员权限运行cmd程序而后输入net stop mysql,或者运行services.msc 而后找到MySQL服务并中止运行;spa
2.跳过权限登陆MySQL服务blog
看了不少人的博客,写的都是找到my.ini文件而后在[mysqld]后添加skip-grant-tables,经测试无效。ip
又查到有的说在cmd中运行mysqld –skip-grant-tables,实测在mysql8.0中已失效,现使用mysqld --console --skip-grant-tables --shared-memory,运行结果以下图:cmd
3.无密登陆博客
再打开一个cmd窗口,输入mysql -u root -p,而后回车,再回车(不输入密码),直接无密登陆,以下图:string
4.修改密码
修改密码所用的主要命令以下所示:
把密码设置为空:UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';
查看USER表:select host,user,plugin,authentication_string from mysql.user;
以caching_sha2_password加密方式设置密码修改密码:ALTER user 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'root';
刷新:flush privileges;
5.从新登陆
把以前打开的cmd窗口关闭,而后以管理员权限运行cmd程序,再执行net start mysql。在MySQL服务开启成功以后,输入mysql -u root -p而后在输入密码,成功登陆MySQL。