Mysql

無法在本地通過 TCP 連接到 MySQL - 連接超時 - Ubuntu 9.04

  • May 31, 2010

我正在執行 Ubuntu,並最終嘗試使用 JDBC 將 Tomcat 連接到我的 MySQL 數據庫。

它以前可以工作,但在重新啟動後,實例現在無法連接。

  • Tomcat 6 和 MySQL 5.0.75 都在同一台機器上
  • 連接字元串:jdbc:mysql:///localhost:3306
  • mysql我可以使用以下命令在命令行上連接到 MySQL
  • my.cnf 文件非常標準(可根據要求提供)綁定地址:127.0.0.1
  • 儘管 netstat 說 MySQL 正在偵聽,但我無法 Telnet 到 MySQL 埠
  • 我有一個 IpTables 規則來轉發 80 -> 8080 並且我知道沒有防火牆。

我對此很陌生,我不確定還有什麼要測試的。我不知道我是否應該在 etc/interfaces 中尋找,以及我是否做了要尋找的東西。這很奇怪,因為它曾經可以工作,但重新啟動後它就關閉了,所以我必須改變一些東西…… :)。

我意識到超時表明伺服器沒有響應,我認為這是因為請求實際上沒有通過。我通過 apt-get 和 Tomcat 手動安裝了 MySQL。

MySqld 程序

root@88:/var/log/mysql#  ps -ef | grep mysqld
root     21753     1  0 May27 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql    21792 21753  0 May27 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
root     21793 21753  0 May27 ?        00:00:00 logger -p daemon.err -t mysqld_safe -i -t mysqld
root     21888 13676  0 11:23 pts/1    00:00:00 grep mysqld

網路統計

root@88:/var/log/mysql# netstat -lnp | grep mysql
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      21792/mysqld
unix  2      [ ACC ]     STREAM     LISTENING     1926205077 21792/mysqld        /var/run/mysqld/mysqld.sock

玩具連接類

root@88:~# cat TestConnect/TestConnection.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TestConnection {

 public static void main(String args[]) throws Exception {
   Connection con = null;

   try {
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     System.out.println("Got driver");
     con = DriverManager.getConnection(
               "jdbc:mysql:///localhost:3306",
               "uname", "pass");
     System.out.println("Got connection");

     if(!con.isClosed())
       System.out.println("Successfully connected to " +
         "MySQL server using TCP/IP...");

   } finally {
       if(con != null)
         con.close();
   }
 }
}

玩具連接類輸出

注意:這與我從 Tomcat 得到的錯誤相同。

root@88:~/TestConnect# java -cp mysql-connector-java-5.1.12-bin.jar:. TestConnection
Got driver
               Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 1 milliseconds ago. The driver has not received any packets from the server.
       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
       at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
       at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)            
       at TestConnection.main(TestConnection.java:14)
Caused by: 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.
       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
       at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
       at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
       at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
       at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
       ... 12 more
Caused by: java.net.ConnectException: Connection timed out
       at java.net.PlainSocketImpl.socketConnect(Native Method)
       ... 13 more

遠端登錄輸出

root@88:~/TestConnect# telnet localhost 3306
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection timed out

iptables

注意:我確實為 NAT 設置了規則,但我已將其刪除,問題仍然存在。

root@88:~# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

root@88:~# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

用於設置 iptables 的命令

iptables -t nat -I PREROUTING --src 0/0 --dst 88.198.31.14 -p tcp --dport 80 -j REDIRECT --to-ports 8080

我已經刪除了這個 NAT 設置的規則,所以除非這個命令有副作用,它不相關。

更新 - 我無法 ping 本地主機

ipa

root@88:~/TestConnect# ip a
1: lo: <LOOPBACK> mtu 16436 qdisc noop state DOWN
   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
   link/void
   inet 127.0.0.1/32 scope host venet0
   inet 88.198.31.14/32 scope global venet0:0

智慧財產權

root@88:~/TestConnect# ip r
   192.0.2.1 dev venet0  scope link
   default via 192.0.2.1 dev venet0

ip規則

root@88:~/TestConnect# ip rule
0:      from all lookup local
32766:  from all lookup main
32767:  from all lookup default

ping -c 1 本地主機

root@88:~/TestConnect# ping -c 1 localhost
PING localhost.localdomain (127.0.0.1) 56(84) bytes of data.

--- localhost.localdomain ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms

貓 /etc/hosts

root@88:~/TestConnect# cat /etc/hosts
127.0.0.1 localhost.localdomain localhost
# Auto-generated hostname. Please do not remove this comment.
88.198.31.14 88.198.31.14  88 88

如果配置

root@88:/var/log/mysql# ifconfig
venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
         inet addr:127.0.0.1  P-t-P:127.0.0.1  Bcast:0.0.0.0  Mask:255.255.255.255
         UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1
         RX packets:144432 errors:0 dropped:0 overruns:0 frame:0
         TX packets:153825 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:37896766 (37.8 MB)  TX bytes:28722595 (28.7 MB)

venet0:0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
         inet addr:88.198.31.14  P-t-P:88.198.31.14  Bcast:0.0.0.0  Mask:255.255.255.255
         UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1

感謝 pQd,我認為我們已經縮小了錯誤範圍,我需要了解這一切的含義以及為什麼 localhost 無法 ping。

你絕對確定沒有阻止 tcp/3306 流量的 iptables 規則嗎?試試這個作為臨時解決方法:

iptables -I INPUT -p tcp --dport 3306 -i lo -j ACCEPT
iptables -A OUTPUT -p tcp --sport 3306 -o lo -j ACCEPT

或者您的 NAT 規則可能比您預期的要多一點?

好的-您的環回不僅已關閉,而且沒有綁定 127.0.0.1 。添加到 /etc/network/interfaces:

auto lo
iface lo inet loopback

和..你到底為什麼把它綁定到vnet0?從那裡刪除它。

快速的一次性 cmd 行修復:

ip a d 127.0.0.1/32 dev venet0
ip a a 127.0.0.1/32 dev lo
ip link set dev lo up

引用自:https://serverfault.com/questions/146668