Java 连接到 MySQL 数据库时关于 SSL 连接的警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34189756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:23:32  来源:igfitidea点击:

Warning about SSL connection when connecting to MySQL database

javassldatabase-connectionmysql-error-1064

提问by Milos86

With the two classes below, I've tried connect to a MySQL database. However, I always get this error:

使用下面的两个类,我尝试连接到 MySQL 数据库。但是,我总是收到此错误:

Wed Dec 09 22:46:52 CET 2015 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.
Wed Dec 09 22:46:52 CET 2015 警告:不建议在没有服务器身份验证的情况下建立 SSL 连接。根据 MySQL 5.5.45+、5.6.26+ 和 5.7.6+ 要求,如果未设置显式选项,则必须默认建立 SSL 连接。为了符合不使用 SSL 的现有应用程序,verifyServerCertificate 属性设置为“false”。您需要通过设置 useSSL=false 来显式禁用 SSL,或者设置 useSSL=true 并为服务器证书验证提供信任库。

This is the test class with the mainmethod:

这是带有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 Databaseclass:

这是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();
            }
        }
    }
}

回答by Priyank Gosalia

Your connection URL should look like the below,

您的连接 URL 应如下所示,

jdbc:mysql://localhost:3306/Peoples?autoReconnect=true&useSSL=false

This will disable SSL and also suppress the SSL errors.

这将禁用 SSL 并抑制 SSL 错误。

回答by Jon

An alternative method would be:

另一种方法是:

Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "milos23);
properties.setProperty("useSSL", "false");

try (Connection conn = DriverManager.getConnection(connectionUrl, properties)) {
...
} catch (SQLException e) {
...
}

回答by Ahmad.ak

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 Windowsand follow the instructions how to set it

. . 或者 。. 下载适用于 Windows 的 OpenSSL并按照说明进行设置

回答by KARTHIKEYAN.A

Use this to solve the problem in hive while making connection with MySQL

使用这个解决hive中连接MySQL的问题

<property>
   <name>javax.jdo.option.ConnectionURL</name>
   <value>jdbc:mysql://localhost/metastore?createDatabaseIfNotExist=true&amp;autoReconnect=true&amp;useSSL=false</value>
   <description>metadata is stored in a MySQL server</description>
</property>

回答by Peter DeGregorio

How about using SSL but turning off server verification (such as when in development mode on your own computer):

如何使用 SSL 但关闭服务器验证(例如在您自己的计算机上处​​于开发模式时):

jdbc:mysql://localhost:3306/Peoples?verifyServerCertificate=false&useSSL=true

回答by Tomal

This was OK for me:

这对我来说没问题:

this.conn = (Connection)DriverManager
    .getConnection(url + dbName + "?useSSL=false", userName, password);

回答by harun ugur

you need to user your mysql path like this:

你需要像这样使用你的mysql路径:

<property name="url" value="jdbc:mysql://localhost:3306/world?useSSL=true"/>

回答by ArifMustafa

Mention the urllike:

提到url类似:

jdbc:mysql://hostname:3306/hibernatedb?autoReconnect=true&useSSL=false

But in xml configuration when you mention &sign, the IDE shows below error:

但是在 xml 配置中,当您提到&sign 时,IDE 显示以下错误:

The reference to entity "useSSL" must end with the ';' delimiter.

对实体“useSSL”的引用必须以“;”结尾 分隔符。

And then you have to explicitly use the &amp;instead of &to be determined as &by xmlthereafter in xmlyou have to give the url in xml configuration like this:

然后,你必须明确地使用&amp;,而不是&被确定为&通过xml其后xml你得给在这样的xml配置的网址:

<property name="connection.url">jdbc:mysql://hostname:3306/hibernatedb?autoReconnect=true&amp;useSSL=false</property>

回答by Khachornchit Songsaen

I found this warning too then I fixed it by using SSL=false suffix to the connection string like this example code.

我也发现了这个警告,然后我通过使用 SSL=false 后缀来修复它,就像这个示例代码一样。

Example:

例子:

connectionString = "jdbc:mysql://{server-name}:3306/%s?useUnicode=yes&characterEncoding=UTF-8&useSSL=false"

回答by Aliaksandr Kavalenka

I use this property for hibernate in config xml

我在配置 xml 中将此属性用于休眠

<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/bookshop?serverTimezone=UTC&amp;useSSL=false
</property>

without - serverTimezone=UTC- it doesn't work

没有 - serverTimezone=UTC- 它不起作用