简体   繁体   English

无法使用jdbc远程连接到OpenShift MySQL

[英]Cannot connect to OpenShift mysql remotely with jdbc

I am trying to connect to a remote database on openshift using jdbc. 我正在尝试使用jdbc连接到openshift上的远程数据库。 Here is my code below - 这是我的以下代码-

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class Conn {
    public static String url        = "jdbc:mysql://127.9.77.2:3306/mysql";
    public static String user   = "adminSKr7Tbz";
    public static String password = "h1xha****";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // TODO Auto-generated method stub
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url,user,password);
        System.out.println("Connecting to database...");
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from country LIMIT 10;");
        while (rs.next()) {
            HashMap map =  new HashMap();
            String name = rs.getString("Name");
            Integer col       = Integer.parseInt(rs.getString("Area"));
            map.put("name",name);
            map.put("value",col);
            System.out.println(map);

        }
    }
}

I get the following error - 我收到以下错误-

Exception in thread "main" java.sql.SQLException: Access denied for user 'adminSKr7Tbz'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3808)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1256)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at visualize.Conn.main(Conn.java:18)

Can anyone please help me fix this. 谁能帮我解决这个问题。 I am giving all credentials correctly , even then it says Access Denied. 我正确地提供了所有凭据,即使它说“访问被拒绝”。

It is possible that MySQL is only setup to accept connections on local host. MySQL可能仅设置为接受本地主机上的连接。 Can you update the grant statement to use 'user@%' 您可以更新授权声明以使用“ user @%”

String USERNAME = System.getEnv("OPENSHIFT_MYSQL_DB_USERNAME");
String PASSWORD = System.getEnv("OPENSHIFT_MYSQL_DB_PASSWORD");
String DB_NAME = System.getEnv("OPENSHIFT_APP_NAME");
String FORNAME_URL = "com.mysql.jdbc.Driver";
String URL = "jdbc:"+System.getEnv("OPENSHIFT_MYSQL_DB_URL")+ DB_NAME;
Connection m_connection = DriverManager.getConnection(URL , USERNAME , PASSWORD);

Use it like this.Openshift will not relay on static ip so they will not accept your connection via that ip or the localhost. 这样使用它.Openshift将不会在静态IP上进行中继,因此它们将不会通过该IP或本地主机接受您的连接。 Also if you are trying it on your local server rather that deploying it on openshift you have to use ssh tokens for the connection and i think it's not allowed. 另外,如果您在本地服务器上尝试使用它,而不是在openshift上部署它,则必须使用ssh令牌进行连接,我认为这是不允许的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM