已解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_数据库


文章目录

  • 报错问题
  • 解决方法
  • 声明


报错问题

粉丝群里面的一个小伙伴敲代码时发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错信息如下:

启动数据库报错:
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)

密码错误

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_解决方法_02

解决方法

解决方法如下

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_数据库_03


修改密码

第一步:关闭Mysql服务

首先先停止mysql服务。可通过net stop mysql或者任务管理器中关闭。

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_sql_04


以管理员权限操作:(这是第一个窗口)

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_mysql_05


第二步:跳过Mysql密码验证

进入命令提示符(管理员登陆)操作,进入mysql目录中bin文件夹下,mysql8.0与其他版本不同的地方在于无法直接使用mysqld --skip-grant-tables来跳过密码登录。在这我们使用mysqld -console --skip-grant-tables --shared-memory来跳过权限验证。

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_数据库_06


输入执行后没有反馈,新开一个管理员窗口重新执行。(这是第二个窗口)

进入目录后,确保自己已经关闭了Mysql的服务:net stop mysql

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_解决方法_07


关闭Mysql服务之后,继续在D:\mysql-8.0.19-winx64\bin目录下进行操作:

输入

mysqld --console --skip-grant-tables --shared-memory

在输入这行代码之后,如下显示,我们就已经成功跳过Mysql的密码登录了:

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_mysql_08

第三步:无密码方式进入Mysql
在上述步骤之后,再打开一个管理员模式运行的cmd.exe (这是第三个窗口)

进入mysql下的bin目录后,直接登录mysql

不需要通过net start mysql打开mysql服务

在命令行中输入以下代码

d:
cd D:\mysql-8.0.19-winx64\bin(此处输入自己电脑上的安装目录)
mysql -u root -p

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_mysql_09


此时会显示让你输入密码,直接回车,就可以成功连接Mysql。

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_sql_10


第四步:将登陆密码设置为空

输入代码,将密码设置为空(此时还不能直接修改密码,必须先设置为空,否则会报错)

输入:

use mysql; (使用mysql数据表)
update user set authentication_string='' where user='root';(将密码置为空)
quit; (然后退出Mysql)

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_sql_11


第五步:更改自己的登陆密码

这里分为两个部分

1.关闭前两个cmd窗口(一定要关闭!);
2.在第三个窗口中输入代码;

net stop mysql(关闭mysql服务,虽然会显示没有开启服务,但是以防万一)
net start mysql(再打开mysql服务)

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_mysql_12

cd D:\mysql-8.0.19-winx64\bin  (此处输入自己电脑上的安装目录)
mysql -u root -p
(此处会显示输入密码,直接回车就好了,第四步我们已经将他置为空了)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';(更改密码)

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_解决方法_13


最后一步:验证密码是否修改成功

输入:

quit(退出mysql)
mysql -u root -p 
(输入新密码,再次登录)

完美解决ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)_java_14


成功

声明

解决方法参考网络,如有侵权联系我删除