JDBC连接:访问被拒绝

时间:2013-12-17 04:14:11

标签: mysql ssl jdbc

我正在尝试通过SSL获取与mysql的JDBC连接。 我已经将有效证书导入cacertsSSL.jks文件。 我仍然无法连接。

没有扩展或更改驱动程序有没有出路?

public static void getMysqlConnection() {
    Connection connection = null;
    try {

      Class.forName("com.mysql.jdbc.Driver");
      Driver driver = DriverManager.getDriver(URL);
      DriverPropertyInfo[] info = driver.getPropertyInfo(URL, null);

      String URL = "jdbc:mysql://myHost:3307/myDB?verifyServerCertificate=false&requireSSL=true&useSSL=true";
      String USER = "myUser";
      String PASSWORD = "myPwd";

      System.setProperty("javax.net.ssl.keyStore", "C:\\Development\\cacertsSSL.jks");
      System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
      System.setProperty("javax.net.ssl.trustStore", "C:\\Development\\cacertsSSL.jks");
      System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

      Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
      System.out.println(conn);
    } catch (Exception e) {
      System.out.println("Connection Failed! Check output console");
      e.printStackTrace();
      return;
    }
  }

以下是连接错误Stack Trace

java.sql.SQLException: Access denied for user 'myuser'@'myHost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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:282)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at com.test.Test1.getMysqlConnection(Test1.java:152)
at com.test.Test1.main(Test1.java:25)

答案:  以下是它的工作原理:

  String URL = "jdbc:mysql://myHost:3307/myDB?useSSL=true";
  String USER = "myUser";
  String PASSWORD = "myPwd";

  System.setProperty("javax.net.ssl.trustStore", "C:\\Development\\cacertsSSL.jks");
  System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

在这种情况下,只有TrustStore就足够了,在我的情况下只使用了SSL。 感谢。

1 个答案:

答案 0 :(得分:2)

您输入的用户名或密码错误,或者您无法以该用户身份连接该主机。但这也表明SSL部分正常运行。