MySQL connection works with localhost but not with 127.0.0.1
I have a pretty standard MySQL installation on Debian Wheezy (apt-get install mysql-server mysql-client
) which I have done successfully lots of times before.
When I try to connect via localhost
, everything works. But connecting via 127.0.0.1
gives an error message:
$ 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
When I try to connect from a Java application I get similar errors, although I am using localhost
as the hostname:
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.
I usually get this exception when the MySQL server has closed an idle connection or has been restarted. However, this now happens at application startup when the application tries to connect for the first time.
Funny enough this did work just a few hours before. Unfortunately I can't recall to have changed anything on the server. :-(
So to be honest, this post kind of contains two questions: Why can't I connect via 127.0.0.1
? And why can't my applications connect via localhost
although I can via 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"
EDIT
I tried binding the server to 0.0.0.0
and ::
, to no avail.
The server supports IPv6 and is configured accordingly:
# host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1
The same problem as described above happens when I try to connect to ::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
EDIT 2
Connecting via telnet
gives not a lot of information but shows that the connection is closed immediately.
# telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
By the way, the MySQL logfiles are totally silent even with logging enabled.
The culprit seemed to be hosts.deny
and hosts.allow
which by default have a file mode of 0x600
. MySQL couldn't read them to determine whether to allow connections. I changed the file modes to 0x644
, and now everything is running smoothly. I'm still wondering why MySQL didn't log any errors...