使用java connector/j和ssl连接到rds中的mysql

8ulbf1ek  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(371)

我正在努力获取我的java代码(使用 HikariCP )与我的 databaseAWS RDS 使用 SSL . 我可以和 database 使用 MySQL Workbench ,我只是不知道如何配置 MySQL Connector/J 5.1.45 驱动程序访问 database 使用 SSL .
现在这是我的 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 根,因为它在我的 resources 文件夹来自 maven . 我真的很想提供我自己的 truststore 文件到 MySQL Connector/J 这样这个程序就可以在不需要配置 truststore 这对我来说非常有用,因为我的代码在本地运行 GlassFish 但是在云端 AWS Lambda 谁知道明天它会跑到哪里)。
当我尝试使用此配置获取连接时,出现以下错误:
com.mysql.jdbc.exceptions.jdbc4.mysqlnontransientconnectionexception:无法打开file://truststore [托管库]
一开始我以为
trustore url 是错的,但如果我改成这样:

dataSource.clientCertificateKeyStoreUrl=file:///truststore

错误更改为:
com.mysql.jdbc.exceptions.jdbc4.mysqlnontransientconnectionexception:无法打开文件:///truststore[\truststore(系统找不到指定的文件)]
这对我来说意味着 url 他是对的。
我不知道我做错了什么,也不知道如何解决这个问题,信任库是用以下命令生成的:

'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:无法打开file://truststore [托管库]
是:
java.net.unknownhostexception:信任库
我不知道这意味着什么
database url 是正确的,因为它和我在 MySQL Workbench .

mnemlml8

mnemlml81#

如果您使用的是maven,请确保您使用的-djavax.net.ssl.truststore=/home/vagrant具有正确的证书,或者使用正确的值配置maven\u opts,例如:-djavax.net.ssl.truststore=/dev,还可以将其放在intellij的runner部分

相关问题