微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL:能够连接到localhost但不能连接到127.0.0.1

我猜我忘记了一些明显的东西,但我似乎无法使用MySQL连接到127.0.0.1.通过localhost连接工作.

MysqL / Linux版本:服务器版本:10.0.31-MariaDB-0ubuntu0.16.04.2 Ubuntu 16.04

这有效:

$MysqL -h localhost -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 71982
Server version: 10.0.31-MariaDB-0ubuntu0.16.04.2 Ubuntu 16.04

copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

这不起作用:

$MysqL -h 127.0.0.1 -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

在我看来,MysqL正在收听127.0.0.1:3306:

$netstat -plnt
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1841/MysqLd 

我甚至可以使用Telnet连接到端口3306:

$telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
j
5.5.5-10.0.31-MariaDB-0ubuntu0.16.04.2TGYk-=,d5-??wz}'Hr*s+u24MysqL_native_password

为了以防万一,我还为主机127.0.0.1创建了一个具有grant选项的用户

MysqL > CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '<redacted>';
MysqL > GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;

以下是有关用户的一些信息(@ brian-ecker建议的查询):

MariaDB [(none)]> SELECT User, Host, plugin FROM MysqL.user;
+------------------+-----------+-------------+
| User             | Host      | plugin      |
+------------------+-----------+-------------+
| root             | localhost | unix_socket |
| root             | 127.0.0.1 |             |
+------------------+-----------+-------------+

我还尝试创建一个由unix_socket标识的root用户,但没有它帮助:

MariaDB [(none)]> CREATE USER 'root'@'127.0.0.1' IDENTIFIED VIA unix_socket;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;
MariaDB [(none)]> SELECT User, Host, plugin FROM MysqL.user;
+------------------+-----------+-------------+
| User             | Host      | plugin      |
+------------------+-----------+-------------+
| root             | localhost | unix_socket |
| root             | 127.0.0.1 | unix_socket |
+------------------+-----------+-------------+

您对我为什么可以通过“localhost”而不是“127.0.0.1”进行连接有什么建议吗?

解决方法:

通过将其添加MysqL配置解决了它:

skip-name-resolve       = 1

MySQL documentation开始:

If it is OFF, MysqLd resolves host names when checking client connections. If it is ON, MysqLd uses only IP numbers; in this case, all Host column values in the grant tables must be IP addresses or localhost.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐