使用java 1.8.0_162将JDBC转换为mysql 5.7.21;不支持的记录版本Unknown-0.0

时间:2018-02-23 12:27:50

标签: mysql ssl jdbc sslhandshakeexception

我正在尝试使用JDBC连接到mysql。 我在Windows 10上生成了如下键:

winpty openssl pkcs12 -export -inkey ssl_cert/client-key.pem -in ssl_cert/client-cert.pem -out client.packet

keytool -importkeystore -deststorepass <password> -destkeypass <password> -destkeystore mysqldb.jks -srckeystore client.packet -srcstoretype PKCS12 -srcstorepass <password> -alias 1 

keytool -importcert -alias mysqlCA -trustcacerts -file ssl_cert/ca.pem -keystore mysqldb.jks 

我的JDBC代码如下所示:

System.setProperty("javax.net.ssl.trustStore", "ks-production-mysqldb.jks");
            System.setProperty("javax.net.ssl.trustStorePassword", "<password> ");
            System.setProperty("javax.net.ssl.trustStoreType", "JKS");

            System.setProperty("javax.net.ssl.keyStore", "mysqldb.jks");
            System.setProperty("javax.net.ssl.keyStorePassword", "<password> ");
            System.setProperty("javax.net.ssl.keyStoreType", "JKS");

            String dbURL = "jdbc:mysql://localhost:1234/sb?"
            + "verifyServerCertificate=true&useSSL=true&requireSSL=true";
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);

我收到以下错误:

  

引起:javax.net.ssl.SSLException:不支持的记录版本   未知-0.0 at   sun.security.ssl.InputRecord.checkRecordVersion(InputRecord.java:552)     at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:565)     在sun.security.ssl.InputRecord.read(InputRecord.java:529)at   sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)at at   sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)     在   sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)     在   sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)     在   org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:444)     ......还有7个

我用Google搜索并发现它可能是由于服务器和java之间的SSL协议不同,他们说必须已经解决了Java 7或更高版本。 但我仍然得到错误?我错过了什么?

连接到localhost之前的端口转发:1234:

try {
        JSch jsch = new JSch();
        jsch.addIdentity(privateKey);
        logger.info("Establishing connection to " + sshHost + " by user " + sshUser);
        session = jsch.getSession(sshUser, sshHost, 22);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        int assignedPort = session.setPortForwardingL(localPort, remoteHost, remotePort);
        logger.info("assigned Port = " + assignedPort);
    } catch (JSchException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return session;

openssl命令输出:

 $ openssl s_client -connect localhost:1234
CONNECTED(00000003)
140108247099296:error:140790E5:SSL routines:ssl23_write:ssl handshake failure:s23_lib.c:177:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 289 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : 0000
    Session-ID:
    Session-ID-ctx:
    Master-Key:
    Key-Arg   : None
    Krb5 Principal: None
    PSK identity: None
    PSK identity hint: None
    Start Time: 1519473350
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)
---

2 个答案:

答案 0 :(得分:0)

没有发布解决方案,所以对于任何面临此问题的人:

我们使用MariaDB,并且在尝试了所有方法后,将mariaDB java客户端驱动程序版本升级到最新版本已解决了该问题。希望对您有所帮助。

答案 1 :(得分:0)

至少 u181 升级到 JDK 8。它适用于显示相同错误的 ReST 连接。