5

我正在运行 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
4

4 回答 4

2

Netstat 显示 MySQL 确实在侦听所有本地地址的端口 3306。由于 telnet 无法连接到 127.0.0.1,因此有一个防火墙会阻止您 - 尝试运行sudo ufw default allow以有效禁用它,或者sudo ufw allow 3306查看它是否进行了任何更改。

于 2010-06-18T17:17:28.230 回答
1

您的 /etc/hosts 文件是什么样的?您是否有类似以下的条目?

127.0.0.1   localhost

您可以尝试远程登录吗:

telnet 127.0.0.1 3306 or telnet 127.0.0.1:3306

或 JDBC 连接到:

jdbc:mysql://127.0.0.1:3306
于 2010-06-20T04:51:00.907 回答
0

连接字符串应该是

jdbc:mysql://localhost:3306

代替

jdbc:mysql:///localhost:3306

同样在 unix/linux 上,默认情况下客户端/服务器通信是通过 unix 套接字文件而不是通过 tcp/ip 完成的。因此,您无法远程登录到本地计算机上的 3306 端口。

于 2010-05-30T13:57:39.327 回答
0

您是否总是对 getConnection 方法使用相同的签名,或者这是您在它停止工作之前更改的内容之一?(字符串中的 '///' 表明它是最近添加的。)

如果这是新代码,您可以尝试以下连接字符串:

String dbUrl = "jdbc:mysql://localhost:3306/[db]?user=[user]&password=[password]"; 连接连接 = DriverManager.getConnection(dbUrl);

这是 mysql.com 文档页面上的示例格式:

http://dev.mysql.com/doc/refman/5.0/es/connector-j-usagenotes-basic.html

于 2010-06-18T17:08:51.090 回答