如何允许从外部访问phpMyAdmin的数据库?

时间:2017-01-25 19:33:55

标签: java mysql phpmyadmin

我允许在第一台PC上访问phpMyAdmin。我可以通过在本地局域网中的另一台PC上的浏览器http://192.168.100.10:8080/phpmyadmin打开来打开phpMyAdmin。而且我也可以通过浏览器DB在另一台PC上的phpMyAdmin中打开

但我无法通过java代码创建与DB的连接:

Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
Properties info = new Properties();
info.put("characterEncoding", "UTF-8");
info.put("user", "root");
info.put("password", "");
info.put("autoReconnect", "true");
info.put("useSSL", "false");
info.put("failOverReadOnly", "false");
info.put("maxReconnects", "10");
info.put("connectTimeout", "2000");

DriverManager.setLoginTimeout(10);
mConnection = DriverManager.getConnection(
    "jdbc:mysql://192.168.100.10:8080/flats_flx", info
);//here it's stuck 

mStatement = mConnection.createStatement();

这里https://stackoverflow.com/a/27700425/2425851我发现我需要“允许网络访问MySQL” 如何在phpMyAdmin中执行此操作?

2 个答案:

答案 0 :(得分:2)

尝试使用这种方式检查您的端口号我认为MySql是3306

public class CreateConnection {
    String driver = "com.mysql.jdbc.Driver";
    String DB_username = "username";
    String DB_password = "password";
    String DB_URL = "jdbc:mysql://192.168.100.10:3306/flats_flx";

    public Connection getConnection() {
        try {
            Class.forName(driver);
            java.sql.Connection con = DriverManager.getConnection(DB_URL, DB_username, DB_password);
            System.out.println(con);
            return con;

        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Exception " + e);
            return null;
        }
    }
}

答案 1 :(得分:2)

Class.forName(driver);
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://ip_of_host_server:3306/Database_name", DB_username, DB_password);