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 gives an error message:

$ mysql -h localhost -P 3306 -u xxx -p
-- works

$ mysql -h -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 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 =

# grep socket /etc/mysql/my.cnf
socket = /var/run/mysqld/mysqld.sock

# ping localhost
PING localhost ( 56(84) bytes of data.
64 bytes from localhost ( icmp_req=1 ttl=64 time=0.022 ms

# grep localhost /etc/hosts 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*               LISTEN

tomcat # grep mysql conf/server.xml


I tried binding the server to and ::, to no avail.

The server supports IPv6 and is configured accordingly:

# host localhost
localhost has address
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


Connecting via telnet gives not a lot of information but shows that the connection is closed immediately.

# telnet localhost 3306
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 reason why I get the errors from my Java application is that the JDBC driver does not resolve localhost to the socket but uses networking. As one would naturally expect... But the original question remains. – Philipp Jardas Jul 22 '14 at 7:26

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...

|improve this answer|||||

This question is very similar to MySQL cannot connect via "localhost", only . As stated here you probably configured MySQL to only listen to the network socket and not to the file system socket.

|improve this answer|||||
  • The answer you posted answers a question that is exactly the opposite to mine. And I do have both networking and file sockets active. – Philipp Jardas Jul 22 '14 at 8:03
  • Yes you are right. Sorry misread your question. – Nebu Jul 22 '14 at 9:06

You may have IPv6 enabled, its very possible localhost resolves to the ipv6 localhost, that is not defined in your mysql config.

You can check this by seeing if 'host localhost' at the command line returns ::1 as well as If so, you can either remove the ::1 mapping or reconfigure MySQL to listen on the IPv6 ::1 address as well as

|improve this answer|||||
  • Indeed localhost resolves to both and ::1. I tried binding MySQL to ::1, no change. I even tried binding MySQL to and ::, no difference. – Philipp Jardas Jul 22 '14 at 8:10

I recently broke a working installation where most clients are Java-based. The CLI tools would work, but the Java clients all stopped dead in their tracks. In my case, the culprit was a new setting that I had enabled "to improve performance":

skip-name-resolve       = on

When you do this, MySQL lo longer uses rDNS to resolve -> localhost and, since all my GRANTs are for user@localhost, the user isn't allowed to connect from host

Two solutions exist for this particular problem:

  1. Disable skip-name-resolve
  2. Expand your GRANTs to include as well as localhost
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.