Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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

3 Answers 3

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

share|improve this answer

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

share|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 127.0.0.1. If so, you can either remove the ::1 mapping or reconfigure MySQL to listen on the IPv6 ::1 address as well as 127.0.0.1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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