我在Debian Wheezy( apt-get install mysql-server mysql-client )上安装了一个非常标准的MySQL安装apt-get install mysql-server mysql-client ,我已经成功完成了很多次。
当我尝试通过localhost连接,一切正常。 但是通过127.0.0.1连接给出了一个错误信息:
$ mysql -h localhost -P 3306 -u xxx -p -- works $ mysql -h 127.0.0.1 -P 3306 -u xxx -p ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
当我尝试从Java应用程序进行连接时,出现类似的错误,尽pipe我使用localhost作为主机名:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
当MySQL服务器closures空闲连接或重新启动时,我通常会得到这个exception。 但是,这现在发生在应用程序启动时,应用程序尝试第一次连接。
有趣的是,这只是几个小时前才工作的。 不幸的是,我不记得在服务器上改变了任何东西。 🙁
所以说实话,这个post里有两个问题:为什么我不能通过127.0.0.1连接? 为什么我的应用程序不能通过localhost连接,虽然我可以通过CLI?
# mysqld -V mysqld Ver 5.5.37-0+wheezy1-log for debian-linux-gnu on x86_64 ((Debian)) # mysql -V mysql Ver 14.14 Distrib 5.5.37, for debian-linux-gnu (x86_64) using readline 6.2 # grep bind /etc/mysql/my.cnf bind-address = 127.0.0.1 # grep socket /etc/mysql/my.cnf socket = /var/run/mysqld/mysqld.sock # ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.022 ms # grep localhost /etc/hosts 127.0.0.1 localhost ::1 ip6-localhost ip6-loopback # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination # netstat -ln | grep 3306 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN tomcat # grep mysql conf/server.xml driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/dbname"
编辑
我尝试绑定服务器到0.0.0.0和:: ,无济于事。
服务器支持IPv6并进行相应的configuration:
# host localhost localhost has address 127.0.0.1 localhost has IPv6 address ::1
当我尝试连接到::1时,发生上述相同的问题。
# ping6 ::1 64 bytes from ::1: icmp_seq=1 ttl=64 time=0.020 ms # ping6 localhost 64 bytes from ip6-localhost: icmp_seq=1 ttl=64 time=0.018 ms
编辑2
通过telnet连接提供的信息不是很多,但显示连接立即closures。
# telnet localhost 3306 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Connection closed by foreign host.
顺便说一下,即使启用了日志logging,MySQL日志文件也是完全无声的。
罪魁祸首似乎是hosts.deny和hosts.allow默认情况下有0x600的文件模式。 MySQL无法读取它们来确定是否允许连接。 我将文件模式更改为0x644 ,现在一切正常运行。 我仍然想知道为什么MySQL没有logging任何错误…
这个问题与MySQL非常类似, 无法通过“localhost”连接,只有127.0.0.1 。 如上所述,您可能将MySQLconfiguration为仅侦听networking套接字,而不是侦听文件系统套接字。
你可能已经启用了IPv6,它的非常可能的本地主机parsing到ipv6本地主机,这是没有定义在你的MySQLconfiguration。
您可以通过查看命令行中的“host localhost”是否返回:: 1以及127.0.0.1来检查。 如果是这样,您可以删除:: 1映射或重新configurationMySQL来侦听IPv6 :: 1地址以及127.0.0.1
我最近打破了大多数客户端都是基于Java的工作安装。 CLI工具可以工作,但是Java客户端都停下脚步。 就我而言,这个罪魁祸首是我为改善性能所做的一个新设置:
skip-name-resolve = on
当你这样做的时候,MySQL lo会使用rDNS来parsing127.0.0.1 – > localhost ,因为我所有的授权都是为了user@localhost ,所以用户不允许从主机127.0.0.1连接。
存在两个解决这个特定问题的scheme:
skip-name-resolve GRANT以包含127.0.0.1以及localhost