连接到 MySQL 数据库时关于 SSL 连接的警告 [英] Warning about SSL connection when connecting to MySQL database

查看:48
本文介绍了连接到 MySQL 数据库时关于 SSL 连接的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>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 并为服务器证书验证提供信任库.

这是带有 main 方法的测试类:

公共类TestDatabase {公共静态无效主(字符串 [] args){数据库 db = new Database();尝试 {数据库连接();} 捕获(异常 e){e.printStackTrace();}db.close();}}

这是Database 类:

import java.sql.Connection;导入 java.sql.DriverManager;导入 java.sql.SQLException;公共类数据库{私人连接骗局;公共无效连接()抛出异常{if(con != null) 返回;尝试 {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {throw new Exception("没有数据库");}String connectionURL = "jdbc:mysql://localhost:3306/Peoples";con = DriverManager.getConnection(connectionURL, "root", "milos23");}公共无效关闭(){if(con != null){尝试 {关闭();} catch (SQLException e) {e.printStackTrace();}}}}

解决方案

您的连接 URL 应如下所示,

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

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

With the two classes below, I've tried connect to a MySQL database. 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. 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.

This is the test class with the main method:

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:

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

解决方案

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.

这篇关于连接到 MySQL 数据库时关于 SSL 连接的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆