[英]Connect to MySQL in RDS using Java Connector/J and SSL
我正在努力让我的Java代码(使用HikariCP
)使用SSL
与AWS RDS
上的database
连接。 我可以使用MySQL Workbench
与database
连接,只是找不到如何配置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
证书从此处生成的,它位于我的classpath
根目录中,因为它位于maven
resources
文件夹中。 我真的很想将自己的truststore
文件提供给MySQL Connector/J
驱动程序,这样就可以移动该程序而无需从环境配置truststore
(这对我来说非常有用,因为我的代码在GlassFish
上本地运行,但在云上运行在AWS Lambda
,谁知道明天将在哪里运行)。
当我尝试使用此配置建立连接时,出现以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法打开文件:// truststore [truststore]
起初,我认为我的trustore
url
错误,但是如果我将其更改为以下内容:
dataSource.clientCertificateKeyStoreUrl=file:///truststore
错误更改为:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法打开文件:/// 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
使用的相同。
如果使用的是Maven,请确保使用-Djavax.net.ssl.trustStore = / home / vagrant和正确的证书,或者使用适当的值配置MAVEN_OPTS,例如:-Djavax.net.ssl.trustStore = / dev,也将其放在Intellij的运行器部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.