Java:无法使用JDBC访问远程MySQL数据库

时间:2014-12-04 15:30:51

标签: java mysql networking jdbc cpanel

我正在尝试使用我的Java代码访问安装在我的Web服务器中的MySQL数据库。我的Java应用程序是一个在我的PC上运行的Swing应用程序。下面是我的Java代码。

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;


public class TestDatabase {

    private Connection con;

    private void connect()
    {
        try
        {
             Class.forName("com.mysql.jdbc.Driver");


             con = DriverManager.getConnection("jdbc:mysql://72.0.0.0:3306/aceko_test","aceko_me","rtf7890");

             System.out.println("Connection Established");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[]args)
    {
        new TestDatabase().connect();
    }

}

我收到以下错误。

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:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    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:347)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at TestDatabase.connect(TestDatabase.java:31)
    at TestDatabase.main(TestDatabase.java:43)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
    ... 16 more

我在我的网络服务器的CPanel中最好地完成了我的知识,以分配远程访问权限。正如您在下图所示,我提供了&#34; Any Access&#34;

enter image description here

如下图所示,我创建了数据库,用户,并将用户分配到数据库。

enter image description here

请注意我在我的代码中提到的IP是假的,仅供SO使用。

1 个答案:

答案 0 :(得分:1)

Google中的搜索我发现this article显示了如何连接DBMS,但是是西班牙语。无论如何,我试图翻译文章:

1.-修改/etc/mysql/my.cnf文件 您必须评论跳过外部锁定绑定地址条目

    [mysqld]
    #
    # * Basic Settings
    #
    user            = mysql
    pid-file        = /var/run/mysqld/mysqld.pid
    socket          = /var/run/mysqld/mysqld.sock
    port            = 3306
    basedir         = /usr
    datadir         = /var/lib/mysql
    tmpdir          = /tmp
    lc-messages-dir = /usr/share/mysql
    #skip-external-locking
    #
    # Instead of skip-networking the default is now to listen only on
    # localhost which is more compatible and is not less secure.
    #bind-address           = 127.0.0.1

2.-使用以下命令为用户授予权限

haciendoti@haciendoti:~$ sudo mysql -u root -ppasswd

mysql > GRANT ALL ON *.* TO haciendoti@'%' IDENTIFIED BY 'passwd';

3.-向防火墙添加例外(我不知道您是否可以在服务器中执行此操作)

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

最好的问候。