2010-05-30 153 views
5

我正在运行Ubuntu,并最终尝试使用JDBC将Tomcat连接到我的MySQL数据库。无法使用JDBC连接到MySQL - 连接超时 - Ubuntu 9.04

它以前的工作但重启后实例现在无法连接。

  • 两者的Tomcat 6和MySQL 5.0.75是在同一台机器
  • 连接字符串上:JDBC:MySQL的:///本地主机:3306
  • 我可以连接到MySQL使用mysql在命令行上命令
  • my.cnf文件是非常标准的(应要求提供)已绑定地址:127.0.0.1
  • 我不能telnet到MySQL端口的netstat虽然说MySQL是听
  • 我有一个iptables规则转发80 - > 8080,没有我知道的防火墙。

我对此很新,我不确定还有什么要测试。我不知道我是否应该查看etc/interfaces,以及如果我做了什么寻找。这很奇怪,因为它曾经工作,但在重新启动后,它已经关闭了,所以我必须改变一些东西.... :)。

我意识到超时表示服务器没有响应,我认为这是因为请求实际上没有通过。我通过手动安装了apt-get和Tomcat来安装MySQL。

MYSQLD处理

[email protected]:/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

[email protected]:/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 

玩具连接类

[email protected]:~# 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获得的同样的错误。

[email protected]:~/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 

的Telnet输出

[email protected]:~/TestConnect# telnet localhost 3306 
Trying 127.0.0.1... 
telnet: Unable to connect to remote host: Connection timed out 

回答

2

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

+0

这导致我的答案是lo接口被关闭。 *耻辱*。快速的'ifconfig lo up'对它进行排序。 – gav 2010-07-04 19:06:16

0

连接字符串应该是

jdbc:mysql://localhost:3306 

代替

jdbc:mysql:///localhost:3306 

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

+0

我ammended连接字符串和仍然没有运气。是否有可能通过使用Tomcat/Java的套接字进行连接?也许我从来没有能够通过这个端口连接和tomcat用于使用套接字? – gav 2010-05-30 16:01:17

+0

似乎MySQL Connector/J不支持通过unix套接字连接 - 它只支持通过tcp/ip或命名管道的连接。一个简单的选择是将你的MySQL服务器配置为通过tcp/ip接受连接。 – 2010-05-31 03:02:03

0

您是否总是对getConnection方法使用相同的签名,或者是您在停止工作之前更改的某个内容?(中字符串的建议“///”这是最近才加入。)

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

字符串dbUrl =“的jdbc:mysql的:// localhost:3306/[db]?user = [user] & password = [password]“; Connection conn = DriverManager.getConnection(dbUrl);

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

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

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