本文翻译自:Warning about SSL connection when connecting to MySQL database
With the two classes below, I've tried connect to a MySQL database. 在下面的两个类中,我尝试连接到MySQL数据库。 However, I always get this error: 但是,我总是会收到此错误:
Wed Dec 09 22:46:52 CET 2015 WARN: Establishing SSL connection without server's identity verification is not recommended. 2015年12月09日星期三22:46:52警告:不建议在没有服务器身份验证的情况下建立SSL连接。 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. 根据MySQL 5.5.45 +,5.6.26 +和5.7.6+的要求,如果未设置显式选项,则默认情况下必须建立SSL连接。 For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. 为了与不使用SSL的现有应用程序兼容,将verifyServerCertificate属性设置为'false'。 You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 您需要通过设置useSSL = false来显式禁用SSL,或者设置useSSL = true并为服务器证书验证提供信任库。
This is the test class with the main
method: 这是带有main
方法的测试类:
public class TestDatabase {
public static void main(String[] args) {
Database db = new Database();
try {
db.connect();
} catch (Exception e) {
e.printStackTrace();
}
db.close();
}
}
This is the Database
class: 这是Database
类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
private Connection con;
public void connect() throws Exception{
if(con != null) return;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new Exception("No database");
}
String connectionURL = "jdbc:mysql://localhost:3306/Peoples";
con = DriverManager.getConnection(connectionURL, "root", "milos23");
}
public void close(){
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
#1楼
参考:https://stackoom.com/question/2JSJg/连接到MySQL数据库时有关SSL连接的警告
#2楼
Your connection URL should look like the below, 您的连接网址应如下所示,
jdbc:mysql://localhost:3306/Peoples?autoReconnect=true&useSSL=false
This will disable SSL and also suppress the SSL errors. 这将禁用SSL并抑制SSL错误。
#3楼
An alternative method would be: 一种替代方法是:
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "milos23);
properties.setProperty("useSSL", "false");
properties.setProperty("autoReconnect", "true");
try (Connection conn = DriverManager.getConnection(connectionUrl, properties)) {
...
} catch (SQLException e) {
...
}
Although, I don't think the auto-reconnect setting is needed to remove the warning. 虽然,我认为不需要自动重新连接设置即可删除警告。
#4楼
the new versions of mysql-connector establish SSL connection by default ...to solve it: 新版本的mysql-connector默认情况下建立SSL连接...以解决该问题:
Download the older version of mysql-connector such as mysql-connector-java-5.0.8.zip 下载较旧版本的mysql-connector,例如mysql-connector-java-5.0.8.zip
. 。 . 。 or . 要么 。 . 。 Download OpenSSL for Windows and follow the instructions how to set it 下载适用于Windows的OpenSSL并按照说明进行设置
#5楼
Use this to solve the problem in hive while making connection with MySQL 与MySQL建立连接时,用它来解决蜂巢中的问题
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost/metastore?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false</value>
<description>metadata is stored in a MySQL server</description>
</property>
#6楼
如何使用SSL但关闭服务器验证(例如在您自己的计算机上处于开发模式时):
jdbc:mysql://localhost:3306/Peoples?verifyServerCertificate=false&useSSL=true