2

我试图从我的工作场所连接到大学服务器并收到响应:

Connection Failed! Check output console 

我的代码:

public JDBCBasicExample() {
        // TODO Auto-generated constructor stub
    }
    public static void main(String[] argv) {

        System.out.println("-------- MySQL JDBC Connection Testing ------------");

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;
        }

        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;

        try {
            connection = DriverManager
            .getConnection("jdbc:mysql://collegeservername:portnumber/databasenameinserver",
              "username","password");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
      }

我收到以下错误:

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:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:675)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1086)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2486)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    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:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at testingdatabase.JDBCBasicExample.main(JDBCBasicExample.java:29)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly l`enter code here`ost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:599)
    ... 16 more

有人可以帮我解决这个问题吗?我对此并不陌生,并尝试了许多其他方法,例如用 IP 地址替换学院服务器名称:端口号,尽管它不起作用。我也尝试过使用其他远程地址和端口名称,但没有成功。请给我你的建议。

4

1 回答 1

0

连接失败简单检查表:

  1. 您的网络工作正常。
  2. 您的大学服务器中是否有任何防火墙阻止或允许来自一组 IP 地址的连接?
  3. 数据库服务器的地址和端口是否正确??检查以确保外部 IP 在内部实际可用。
  4. mysql服务是否启动?(查看系统服务列表和tcp监听列表,mysql是否监听连接??)
  5. 你可以从你的工作区通过 mysql shell 登录你的数据库吗?(打开你的终端,输入mysql -h your_database_host -u your_username -p登录)
  6. 数据库是否允许远程访问?(检查数据库服务器中的 my.cnf 文件并删除该行bind-address=127.0.0.1
于 2013-11-19T01:38:42.767 回答