我在 Debian Wheezy ( apt-get install mysql-server mysql-client
)上有一个非常标准的 MySQL 安装,我之前已经成功完成了很多次。
当我尝试通过 连接时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
Run Code Online (Sandbox Code Playgroud)
当我尝试从 Java 应用程序连接时,我得到了类似的错误,尽管我使用的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.
Run Code Online (Sandbox Code Playgroud)
当 MySQL 服务器关闭空闲连接或重新启动时,我通常会收到此异常。但是,这会在应用程序首次尝试连接时在应用程序启动时发生。
有趣的是,这在几个小时前确实有效。不幸的是,我不记得在服务器上做过任何更改。:-(
所以说实话,这篇文章包含两个问题:为什么我不能通过 连接127.0.0.1
?为什么我的应用程序localhost
不能通过 CLI连接,但我可以通过 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"
Run Code Online (Sandbox Code Playgroud)
编辑
我尝试将服务器绑定到0.0.0.0
和::
,但无济于事。
服务器支持 IPv6 并进行了相应配置:
# host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1
Run Code Online (Sandbox Code Playgroud)
当我尝试连接到::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
Run Code Online (Sandbox Code Playgroud)
编辑 2
连接通过telnet
提供的信息不多,但显示连接立即关闭。
# telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
Run Code Online (Sandbox Code Playgroud)
顺便说一句,即使启用了日志记录,MySQL 日志文件也是完全无声的。
归档时间: |
|
查看次数: |
17140 次 |
最近记录: |