2017-01-06 51 views
0

我有一個MySQL數據庫。我的朋友正在嘗試使用我的公共IP從其他計算機訪問它。使用公共IP訪問MySQL數據庫時出錯

這是他用來測試連接方案:

public class Main { 
    public static void main(String args[]) { 

     String url = "jdbc:mysql://IPadresss:3307/javabase"; 
     String username = "bob"; 
     String password = "bob"; 
     Properties properties = new Properties(); 
     properties.setProperty("bob", "bob"); 
     properties.setProperty("bob", "bob"); 
     properties.setProperty("useSSL", "false"); 
     properties.setProperty("autoReconnect", "true"); 

     System.out.println("Connecting database..."); 

     try (Connection connection = DriverManager.getConnection(url, properties)) { 
      System.out.println("Database connected!"); 
     } catch (SQLException e) { 
      throw new IllegalStateException("Cannot connect the database!", e); 
     } 
    } 
} 

這是錯誤我的朋友,誰是客戶,得到:


Connecting database... 
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database! 
at SQLConnector.Main.main(Main.java:23) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up. 
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:423) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 
at com.mysql.jdbc.Util.getInstance(Util.java:408) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) 
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2163) 
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2088) 
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806) 
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 
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:423) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410) 
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328) 
at java.sql.DriverManager.getConnection(DriverManager.java:664) 
at java.sql.DriverManager.getConnection(DriverManager.java:208) 
at SQLConnector.Main.main(Main.java:20) 
Caused by: java.sql.SQLException: Access denied for user ''@'Freinds ip' (using password: NO) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:873) 
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1710) 
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226) 
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253) 
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2104) 
... 13 more 

請幫我看看我的連接到服務器的方法出了什麼問題,我已經遠程授予了我的朋友(用戶「bob」)的權限。

+2

報價如果你把MySQL數據庫在公共知識產權,即使假設沒有人設法找出你的用戶名和密碼,他們可以非常,非常容易DOS攻擊你的SQL數據庫並將其關閉。因此只打開防火牆到有效的客戶端IP。或者使用更安全的方法(我推薦ssh隧道)。人們會在任何港口找到世界無障礙開放式服務。 –

+0

堆棧跟蹤似乎表明問題來自MySQL訪問控制:「訪問拒絕用戶'''Freinds ip'(使用密碼:否)」。最好的解決方案可能是爲你的朋友用密碼創建一個數據庫用戶,並且允許該用戶訪問數據庫,無論是從所有機器還是(更好)特別是從他正在使用的機器。 –

回答

0

所以,你有以下錯誤:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure java.net.ConnectException: Connection refused

這種情況可能是由多種原因,而且我從this answer

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException : Communications link failure, then it means that the DB isn't reachable at all.

This can have one or more of the following causes:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL. [Assuming the one you mentioned is correct]
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. DB server has run out of connections.
  7. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

To solve the one or the other, follow the following advice:

  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnf of MySQL DB.
  4. Start the DB.
  5. Verify if mysqld is started without the --skip-networking option.
  6. Restart the DB and fix your code accordingly that it closes connections in finally.
  7. Disable firewall and/or configure
  8. firewall/proxy to allow/forward the port.
相關問題