로컬에서 TCP를 통해 MySQL에 연결할 수 없음 - 연결 시간 초과 - Ubuntu 9.04

로컬에서 TCP를 통해 MySQL에 연결할 수 없음 - 연결 시간 초과 - Ubuntu 9.04

저는 Ubuntu를 실행 중이고 궁극적으로 JDBC를 사용하여 Tomcat을 MySQL 데이터베이스에 연결하려고 합니다.

이전에는 작동했습니다.하지만 재부팅 후 인스턴스가 연결되지 않습니다.

  • Tomcat 6과 MySQL 5.0.75가 모두 동일한 시스템에 있습니다.
  • 연결 문자열: jdbc:mysql:///localhost:3306
  • mysql다음 명령 을 사용하여 명령줄에서 MySQL에 연결할 수 있습니다.
  • my.cnf 파일은 매우 표준적입니다(요청 시 사용 가능). 바인드 주소는 127.0.0.1입니다.
  • netstat에서 MySQL이 수신 중이라는 메시지가 표시되었음에도 불구하고 MySQL 포트에 텔넷을 연결할 수 없습니다.
  • 80 -> 8080을 전달하는 하나의 IpTables 규칙이 있고 내가 알고 있는 방화벽은 없습니다.

나는 이것에 대해 아주 새로운 것이고 무엇을 테스트해야할지 잘 모르겠습니다. 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

Netstat

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

IP테이블

참고: 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에 대한 이 설정 규칙을 제거했으므로 이 명령에 부작용이 없는 한 관련이 없습니다.

업데이트 - localhost를 ping할 수 없습니다.

IP

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

IP R

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

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

ifconfig

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을 수행할 수 없는 이유에 대해 읽어봐야 합니다.

답변1

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

관련 정보