2014-12-04 105 views
1

我想用我的Java代碼訪問安裝在我的Web服務器中的MySQL數據庫。我的Java應用程序是一個Swing應用程序,運行在我的電腦中。以下是我的Java代碼。Java:無法使用JDBC訪問遠程MySQL數據庫

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 

我已經在我的web服務器的CPanel中完成了我的最佳知識來分配遠程訪問。正如你可以看到下面的圖片,我所提供的「任何訪問」

enter image description here

,你可以在下面的圖片中看到的,我已經建立了數據庫,用戶和分配用戶數據庫。

enter image description here

請注意,我在我的代碼中提到的IP是假的,只爲使用的SO。

+0

對不起,但java代碼作爲桌面應用程序運行? – Jessai 2014-12-04 15:41:12

+0

@Jessai:是的,這是一個swing應用程序。 – 2014-12-04 15:41:54

+0

我記得有一次我試圖在PHP中做同樣的事情。您無法連接您的代碼的原因是您需要安全和靜態的IP。隨意糾正如果我錯了:D – Jessai 2014-12-04 15:45:58

回答

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:授予權限的用戶使用以下命令

[email protected]:~$ sudo mysql -u root -ppasswd 

mysql > GRANT ALL ON *.* TO [email protected]'%' IDENTIFIED BY 'passwd'; 

3 .-添加例外防火牆(我不知道,如果你可以在你的服務器做到這一點)

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

此致敬禮。

+1

''%''將允許來自任何IP地址的連接,並且端口'3306'是默認端口。這一組合是要求殭屍等人嘗試暴力強制。我建議或者更改爲非標準端口(不是簡單的證明,但會過去大多數愚蠢的機器人)和/或限制用戶帳戶只有它將連接的IP地址(即您的辦公室的公共靜態IP地址) 。 – SnakeDoc 2014-12-04 16:17:42