如何使用SSL JDBC连接到MySQL服务器

问题描述 投票:3回答:1

嗨,我正在尝试使用java远程登录我的MySQL数据库。我创建了一个需要SSL的远程帐户。我按照本指南做了这个:https://www.digitalocean.com/community/tutorials/how-to-configure-ssl-tls-for-mysql-on-ubuntu-16-04

现在,一旦我尝试连接到DB Java,就会抛出此错误:

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

所以我将其添加到我的网址:

       + "&useSSL=true"
      + "&requireSSL=true";

但现在它要求我给它一个trustStoreand和trustStorePassword。我在哪里或如何找到/创建这些?

我的MySQL服务器在VPS ubuntu服务器上运行。

java mysql ssl jdbc keytool
1个回答
2
投票

您可以使用keytool命令创建和导入服务器的证书:

keytool -import -alias mysql -file server-cert.pem -keystore mycerts -storepass changeit

其中server-cert.pem是mysql的证书(您可以按照此链接下的说明获取:http://do.co/2FaIK8F,mycerts将是您的trustStore(将由命令创建),trustStorePassword是changeit(当然,选择一个)

server-cert.pem是教程中提到的文件,其中包含服务器的证书。

然后,您需要使用以下代码将trustStore添加到Java:

System.setProperty("javax.net.ssl.trustStore", "mycerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

changeit是您在keytool命令中选择的密码,其中mycerts是trustStore的路径。

© www.soinside.com 2019 - 2024. All rights reserved.