三、用户存在但客户端主机无权连接
[root@localhost ~]# mysql -u nonexistant -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user ‘nonexistant‘@‘localhost‘ (using password: YES)
修复:您可以通过以下查询检查 MySQL 允许连接的主机用户/主机:
mysql> SELECT Host, User FROM mysql.user WHERE User=‘nonexistant‘;
+-------------+-------------+
| Host | User |
+-------------+-------------+
| 192.168.0.1 | nonexistant |
+-------------+-------------+
1 row in set (0.00 sec)
如果需要检查客户端连接的 IP,可以使用以下 Linux 命令来获取服务器 IP:
[root@localhost ~]# ip address | grep inet | grep -v inet6
inet 127.0.0.1/8 scope host lo
inet 192.168.0.20/24 brd 192.168.0.255 scope global dynamic wlp58s0
或公共IP:
[root@localhost ~]# dig +short myip.opendns.com @resolver1.opendns.com
177.128.214.181
然后,您可以创建具有正确主机(客户端 IP)的用户,或使用’%’(通配符)来匹配任何可能的 IP:
mysql> CREATE USER ‘nonexistant‘@‘%‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected (0.00 sec)