title: 解决MySQL、MariaDB新建用户并授权后无法登录问题 #标题tags: #标签
date: 2021-10-30
categories: mysql # 分类

通过 grant all on *.* to ljz@'%' identified by '123.com';创建用户并刷新权限flush privileges;后登录。

后发现不管在本地还是远程都登录不上。报错信息如下:

  1. ERROR 1045 (28000): Access denied for user 'ljz'@'192.168.20.88' (using password: YES)

在排除密码错误的可能性之后,于是一番百度之后,找到了问题所在:MySQL中默认存在一个用户名为空的账户,只要在本地,可以不用输入账号密码即可登录到MySQL中。而因为这个账户的存在,导致了使用密码登录无法正确登录。

  1. $ mysql # 在mysql本机直接执行mysql命令就可以登录到数据库中
  2. $ use mysql
  3. $ select host,user,password from user; # 查询所有账号,发现有两个空的用户名账号
  4. +-----------+------+-------------------------------------------+
  5. | host | user | password |
  6. +-----------+------+-------------------------------------------+
  7. | localhost | root | |
  8. | node02 | root | |
  9. | 127.0.0.1 | root | |
  10. | ::1 | root | |
  11. | localhost | | |
  12. | node02 | | |
  13. | % | ljz | *AC241830FFDDC8943AB31CBD47D758E79F7953EA |
  14. +-----------+------+-------------------------------------------+

问题就出在上面那两个空用户名的账号,删掉就可以正常登录了:

  1. $ delete from user where user='';
  2. $ flush privileges;

OK,问题解决。。。