MySQL:通信链路故障错误和telnet localhost 3306:连接关闭

时间:2011-03-31 04:30:00

标签: java mysql jdbc

我尝试使用Java连接MYSQL数据库,其中我的SQL数据库位于localhost上。 我面临一些有关通信链路故障的问题。

public testfile() throws ClassNotFoundException, SQLException{

        Connection connection = null;
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        connection = DriverManager.getConnection("jdbc:mysql://localhost/rts?user=root&password=password");
        System.out.println("Connected");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if(connection != null)
                connection.close();
        }
}

堆栈追踪:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused

STACKTRACE:

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
    at java.net.Socket.connect(Socket.java:546)
    at java.net.Socket.connect(Socket.java:495)
    at java.net.Socket.<init>(Socket.java:392)
    at java.net.Socket.<init>(Socket.java:235)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:222)
    at test.testfile.<init>(testfile.java:18)
    at test.mainfile.main(mainfile.java:9)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:620)
    at java.sql.DriverManager.getConnection(DriverManager.java:222)
    at test.testfile.<init>(testfile.java:18)
    at test.mainfile.main(mainfile.java:9)

Telnet输出:

telnet localhost 3306 
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

包装/软件信息:

  • 适用于Web开发人员的Eclipse Java EE IDE。
  • 版本:Helios Service Release 1。
  • JDK6
  • Connector / J version 5.0.8
  • Ubuntu:Ubuntu 10.04 LTS- Lucid Lynx

我尝试了一些东西,但它不起作用:

  • 我在my.cnf中更改了端口但没有效果,我改为3312 同样的错误重复和telnet显示如下:

    shreya@shreya-laptop:~$ telnet localhost 3306
    Trying 127.0.0.1...
    telnet: Unable to connect to remote host: Connection refused
    shreya@shreya-laptop:~$ telnet localhost 3312
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    Connection closed by foreign host.
    

    nmap输出:

    shreya@shreya-laptop:~$ nmap localhost -p 3306
    
    Starting Nmap 5.00 ( [url]http://nmap.org[/url] ) at 2011-03-31 09:45 IST
    Interesting ports on localhost (127.0.0.1):
    PORT     STATE  SERVICE
    3306/tcp closed mysql
    
    Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds
    shreya@shreya-laptop:~$ nmap localhost -p 3312
    
    Starting Nmap 5.00 ( [url]http://nmap.org[/url] ) at 2011-03-31 09:45 IST
    Interesting ports on localhost (127.0.0.1):
    PORT     STATE SERVICE
    3312/tcp open  unknown
    
    Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds
    
  • 我尝试在hosts.allow中输入条目,但没有效果

  • 我尝试重新安装mysql。通过mysql提示和c程序插入数据可以很好地工作,但不能通过java代码。

  • 我尝试在ip表中添加规则仍然没有帮助

  • 我尝试过不同的url字符串组合。我也尝试过不同版本的eclipse和Connector / J jar文件(最新5.1.15)

3 个答案:

答案 0 :(得分:1)

首先尝试使用MYSQL Command Line Client使用程序中使用的相同凭据连接到MySQL。

答案 1 :(得分:1)

  

java.net.ConnectException:拒绝连接

在给定的主机/端口上没有任何侦听/接受连接的东西。验证以下内容:

  • my.cnf验证端口号是否确实为3306。
  • 验证数据库是否已启动(使用MySQL DB管理工具)。
  • 验证数据库是否接受TCP / IP连接,mysqld不应选择--skip-networking
  • 验证数据库是否没有用完连接,如有必要,重新启动它并相应地修复所有现有Java代码,使其正确关闭finally块中的连接。
  • 验证在哪个阻止给定端口上的连接之间没有防火墙/代理。

答案 2 :(得分:0)

您需要向通过端口3306连接的用户授予权限......这个人有一个很好的解释:link