【发布时间】:2018-06-24 16:14:21
【问题描述】:
我正在努力让我的 java 代码(使用 HikariCP)使用 SSL 与 AWS RDS 上的 database 连接。我可以使用MySQL Workbench 与database 连接,但我只是不知道如何配置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 根目录上,因为它位于来自maven 的resources 文件夹中。我真的很想将我自己的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