【问题标题】:Connect to MySQL in RDS using Java Connector/J and SSL使用 Java Connector/J 和 SSL 连接到 RDS 中的 MySQL
【发布时间】:2018-06-24 16:14:21
【问题描述】:

我正在努力让我的 java 代码(使用 HikariCP)使用 SSLAWS RDS 上的 database 连接。我可以使用MySQL Workbenchdatabase 连接,但我只是不知道如何配置MySQL Connector/J 5.1.45 驱动程序以使用database 访问database

目前这是我的HikariCP 属性文件:

jdbcUrl=jdbc:mysql://mypath.jqwejrkq4568833.us-east-1.rds.amazonaws.com:3306/myschema
username=myusername
password=mypassword
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.useLocalTransactionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.cacheServerConfiguration=true
dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false
dataSource.clientCertificateKeyStoreUrl=file://truststore
dataSource.clientCertificateKeyStorePassword=123456
dataSource.useSSL=true
dataSource.verifyServerCertificate=true
dataSource.requireSSL=true

truststore 文件是使用RDS certificate from here 生成的,它位于我的classpath 根目录上,因为它位于来自mavenresources 文件夹中。我真的很想将我自己的truststore 文件提供给MySQL Connector/J 驱动程序,这样就可以移动这个程序而无需从环境中配置truststore(这对我来说非常有用,因为我的代码在@987654342 上本地运行@ 但在AWS Lambda 上的云上,谁知道它明天会在哪里运行)。

当我尝试使用此配置建立连接时,我收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: 无法打开文件://truststore [truststore]

一开始我以为我的trustore url 错了,但如果我把它改成这样:

dataSource.clientCertificateKeyStoreUrl=file:///truststore

错误变为:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot open file:///truststore [\truststore (系统找不到指定的文件)]

这表明第一个url 是正确的。

我不知道我做错了什么或如何解决这个问题,`truststore 是使用以下命令生成的:

'C:\Program Files\Java\jre1.8.0_151\bin\keytool.exe' -importcert -alias AwsRdsMySqlCACert -file rds-combined-ca-bundle.pem -keystore truststore -storepass 123456

我不知道我做错了什么,这引出了一个问题:

那么我的truststore 文件有什么问题?


编辑:

深入挖掘我可以找到Exception的原因:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: 无法打开文件://truststore [truststore]

是:

java.net.UnknownHostException: 信任库

我不知道这是什么意思,因为我的 database url 是正确的,因为它与我在 MySQL Workbench 中使用的相同。

【问题讨论】:

    标签: java mysql ssl jdbc mysql-connector


    【解决方案1】:

    如果您使用 Maven,请确保您使用 -Djavax.net.ssl.trustStore=/home/vagrant 和正确的证书,或者使用正确的值配置 MAVEN_OPTS,例如:-Djavax.net.ssl .trustStore=/dev,同样放在 Intellij 的 runner 部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-20
      • 2013-12-28
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多