java连接mysql禁用ssl_java - 连接到MySQL数据库时有关SSL连接的警告

java - 连接到MySQL数据库时有关SSL连接的警告

通过以下两个类,我尝试连接到MySQL数据库。 但是,我总是得到这个错误:

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

这是使用Database方法的测试类:

public class TestDatabase {

public static void main(String[] args) {

Database db = new Database();

try {

db.connect();

} catch (Exception e) {

e.printStackTrace();

}

db.close();

}

}

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

}

}

}

}

10个解决方案

483 votes

您的连接网址应如下所示,

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

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

Priyank Gosalia answered 2019-02-16T13:19:30Z

109 votes

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

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

Peter DeGregorio answered 2019-02-16T13:20:04Z

18 votes

另一种方法是:

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) {

...

}

虽然,我不认为需要自动重新连接设置来删除警告。

Jon answered 2019-02-16T13:20:44Z

12 votes

提到xml喜欢:

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

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

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

然后你必须明确使用xml而不是xml由xml确定为xml,之后在xml中你需要在xml配置中给出这样的url:

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

ArifMustafa answered 2019-02-16T13:21:29Z

8 votes

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

harun ugur answered 2019-02-16T13:22:01Z

8 votes

在与MySQL建立连接时使用它来解决配置单元中的问题

javax.jdo.option.ConnectionURL

jdbc:mysql://localhost/metastore?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false

metadata is stored in a MySQL server

KARTHIKEYAN.A answered 2019-02-16T13:22:26Z

8 votes

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

例:

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

John Pluto Solutions answered 2019-02-16T13:22:54Z

6 votes

这对我来说没问题:

this.conn = (Connection)DriverManager

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

Tomal answered 2019-02-16T13:23:20Z

1 votes

新版本的mysql-connector默认建立SSL连接...解决它:

下载mysql-connector的旧版本,例如mysql-connector-java-5.0.8.zip

。。要么。。下载OpenSSL for Windows并按照说明如何设置它

Ahmad.ak answered 2019-02-16T13:24:00Z

0 votes

我在config xml中将此属性用于hibernate

jdbc:mysql://localhost:3306/bookshop?serverTimezone=UTC&useSSL=false

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

Aliaksandr Kavalenka answered 2019-02-16T13:24:32Z

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值