12

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.

1
  • 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. Jul 22, 2014 at 7:26

5 Answers 5

1

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

1

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 127.0.0.1 -> localhost and, since all my GRANTs are for user@localhost, the user isn't allowed to connect from host 127.0.0.1.

Two solutions exist for this particular problem:

  1. Disable skip-name-resolve
  2. Expand your GRANTs to include 127.0.0.1 as well as localhost
0

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.

2
  • 1
    The answer you posted answers a question that is exactly the opposite to mine. And I do have both networking and file sockets active. Jul 22, 2014 at 8:03
  • Yes you are right. Sorry misread your question.
    – Nebu
    Jul 22, 2014 at 9:06
0

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

1
  • 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. Jul 22, 2014 at 8:10
0

it worked for me in linux. edit the file - /etc/mysql/mysql.conf.d/mysqld.cnf make it like this -> bind-address = 0.0.0.0

Refer - https://phoenixnap.com/kb/mysql-remote-connection

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .