如何在 Java 中连接到 SQL 数据库时禁用 WARN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36756739/
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 18:38:46  来源:igfitidea点击:

How to disable WARN when connecting to SQL database in Java

javamysqlssl

提问by Ethan Moore

In a Java application I'm programming, when I ask the program to retrieve data from an SQL database, it shows this in console :

在我正在编程的 Java 应用程序中,当我要求程序从 SQL 数据库中检索数据时,它在控制台中显示:

Wed Apr 20 16:57:58 MDT 2016 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 Apr 20 16:57:58 MDT 2016 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.

I know that's a lot to read, and I have SSL disabled in the SQL workbench, so I can't figure out how to get rid of the warning or if I should even be concerned about it? Thanks in advance.

我知道要读很多东西,而且我在 SQL 工作台中禁用了 SSL,所以我不知道如何摆脱警告,或者我是否应该担心它?提前致谢。

采纳答案by geek

Try to use the useSSL=false in the connection string

尝试在连接字符串中使用 useSSL=false

Example:

例子:

jdbc:mysql://xxxxxxx:3306/xxxx?autoReconnect=true&useSSL=false

jdbc:mysql://xxxxxxx:3306/xxxx?autoReconnect=true&useSSL=false

EDIT:

编辑:

I had issue today 11/20/2019 while connecting mysql version 8.0.12 using java mysql driver (com.mysql.cj.jdbc.Driver, 8.0.18) and got following error

我在今天 11/20/2019 使用 java mysql 驱动程序 (com.mysql.cj.jdbc.Driver, 8.0.18) 连接 mysql 版本 8.0.12 时遇到问题,并出现以下错误

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

线程“main”中的异常 java.sql.SQLNonTransientConnectionException:不允许公钥检索

I had to add allowPublicKeyRetrieval=trueto connection string to fix the connection issue.

我不得不添加allowPublicKeyRetrieval=true到连接字符串来解决连接问题。

Updated this answer because its related issue :)

更新了这个答案,因为它的相关问题:)

回答by William Guzman

Replace– jdbc:mysql://localhost:3306/demo

替换——jdbc:mysql://localhost:3306/demo

With– jdbc:mysql://localhost:3306/demo?useSSL=false

-jdbc:mysql://localhost:3306/demo?useSSL=false

回答by Fillipos Christou

In case someone uses MysqlDataSource, which is preferred over DriverManager you can you the setUseSSL(boolean sslstate)function :

如果有人使用MysqlDataSource,它比 DriverManager 更受欢迎,您可以使用以下setUseSSL(boolean sslstate)功能:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("USER_NAME");
dataSource.setPassword("PASSWORK");
dataSource.setServerName("localhost"); //most likely
dataSource.setDatabaseName("DATABASE_NAME");
dataSource.setUseSSL(false); //Disable SSL
try(
    Connection conn = dataSource.getConnection();
)
{
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("INSERT INTO TABLE_NAME ...");
} catch (SQLException e)
{
    e.printStackTrace();
}