Ho un'installazione MySQL piuttosto standard su Debian Wheezy ( apt-get install mysql-server mysql-client) che ho fatto con successo molte volte prima.
Quando provo a connettermi tramite localhost, tutto funziona. Ma la connessione tramite 127.0.0.1genera un messaggio di errore:
$ 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
Quando provo a connettermi da un'applicazione Java ottengo errori simili, anche se sto usando localhostcome nome host:
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.
Di solito ricevo questa eccezione quando il server MySQL ha chiuso una connessione inattiva o è stato riavviato. Tuttavia, ciò ora accade all'avvio dell'applicazione quando l'applicazione tenta di connettersi per la prima volta.
Abbastanza divertente questo ha funzionato solo poche ore prima. Purtroppo non ricordo di aver cambiato nulla sul server. :-(
Per essere onesti, questo post contiene due domande: perché non riesco a collegarmi tramite 127.0.0.1? E perché le mie applicazioni non possono connettersi tramite localhostanche se posso tramite la 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"
MODIFICARE
Ho provato a legare il server a , 0.0.0.0e ::senza risultato.
Il server supporta IPv6 ed è configurato di conseguenza:
# host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1
Lo stesso problema descritto sopra si verifica quando provo a connettermi ::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
MODIFICA 2
La connessione tramite telnetnon fornisce molte informazioni ma mostra che la connessione viene chiusa immediatamente.
# telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
A proposito, i file di log di MySQL sono totalmente silenziosi anche con la registrazione abilitata.
localhostnel socket ma utilizza la rete. Come ci si aspetterebbe naturalmente ... Ma la domanda originale rimane.