java - 使用 Java Connector/J 和 SSL 连接到 RDS 中的 MySQL

标签 java mysql ssl jdbc mysql-connector

我正在努力让我的 java 代码(使用 HikariCP )使用 SSL 连接到我在 AWS RDS 上的数据库。我可以使用 MySQL Workbench 连接到数据库,我只是找不到如何配置 MySQL Connector/J 5.1.45 驱动程序使用 SSL 访问 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 根目录中,因为它位于我的 resources 来自 maven 的文件夹。我真的很想为 MySQL Connector/J 驱动程序提供我自己的 truststore 文件,这样这个程序就可以移动而无需配置 truststore来自环境(这对我来说非常有用,因为我的代码在 GlassFish 上本地运行,但在 AWS Lambda 上的云端运行,谁知道它明天会在哪里运行)。

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

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot open file://truststore [truststore]

起初我以为我的trustoreurl是错误的,但是如果我把它改成这样:

dataSource.clientCertificateKeyStoreUrl=file:///truststore

错误变为:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot open file:///truststore [\truststore (The system cannot find the file specified)]

这对我来说表明第一个 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: Cannot open file://truststore [truststore]

是:

java.net.UnknownHostException: truststore

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

最佳答案

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

关于java - 使用 Java Connector/J 和 SSL 连接到 RDS 中的 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48273030/

相关文章:

java - 如何通过一个键字段从Map中获取值?

java - 通过@ManagedProperty 检索更新的值/通知其他 beans 属性的更新值

mysql - 为什么MySQL在查找不等于时会忽略空值?

ssl - 通过 SSL 将密码 ldapmodify 更改为 AD 2008 R2 失败,出现 0000052D : SvcErr: DSID-031A1248, 问题 5003 (WILL_NOT_PERFORM)

ruby-on-rails-3 - Rails SSL 仅在一个特定路径上

java - 在启用语法高亮的情况下将 Java 源代码输出为 pdf

java - Java回调中使用接口(interface)的必要性是什么?

php - 在 PHP/MySQL 中访问最后创建的行

php - sql - 子查询计算分数

go - 将 x509 certPool 转换为 pemBytes