连接到远程MySQL数据库时通信链路故障

时间:2014-06-20 19:24:40

标签: java mysql database jdbc

我正在尝试使Java程序与使用A Small Orange托管的MySQL服务器进行通信。他们为我提供的IP和端口是129.121.106.234:3306。用户已获得数据库的完全权限,在我的CPanel中,我允许所有远程主机通过添加访问主机“%”连接到MySQL数据库。

我正在使用Eclipse,我已经下载并将JDBC驱动程序添加到构建路径中。

尽我所知,我正在尝试使用的用户拥有对数据库的完全权限,并且MySQL服务器对来自任何IP的任何请求都是开放的。我也尝试过我的管理员MySQL凭据,同样的错误,所以我对我的代码问题很有信心。但不是很确定,因为我自己并没有托管MySQL服务器。

我的代码:

package com.package.IPBounce;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    /*
     * Isaac the Support Ninja says: The MySQL port is 3306. The IP you use
     * is 129.121.106.234.
     */
    URL whatismyip;
    String IP;
    try {
        whatismyip = new URL("http://checkip.amazonaws.com");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                whatismyip.openStream()));
        IP = in.readLine();
        System.out.println(IP);

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

                    //this is just testing code until I get the connection working
        String url = "jdbc:mysql://129.121.106.234:3306/db_name";
        String user = "user_name";
        String password = "small_password";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection(url, user, password);//error is on this line
        st = con.createStatement();
        rs = st.executeQuery("SELECT VERSION()");

        if (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

错误文字:

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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1137)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2504)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2541)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2323)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.package.IPBounce.Main.main(Main.java:39)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
... 15 more

编辑:我找到了解决方案。我的托管服务阻止了防火墙上的MySQL请求。清除了它,现在它运作得非常好。

2 个答案:

答案 0 :(得分:0)

我认为唯一可能的问题是“newInstance()”调用。尝试删除它,看看是否有效。

来自:

Class.forName("com.mysql.jdbc.Driver").newInstance();

致:

Class.forName("com.mysql.jdbc.Driver");

答案 1 :(得分:-1)

这个问题已在这里得到解答:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

请尝试接受的答案中的步骤,看看您的问题是否已解决。

根据我的经验,当我的MySql配置文件仅允许localhost访问时,我遇到了此问题。将localhost替换为ip地址。