首页 文章

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

提问于
浏览
254

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

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

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

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 回答

  • 110

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

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

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

  • 490

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

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

    另一种方法是:

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

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

  • 13

    提到 url 喜欢:

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

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

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

    然后你必须明确地使用 & 而不是 && 确定为 xml 此后在 xml 中你必须给出xml配置中的url,如下所示:

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

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

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

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

    <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>
    
  • 1

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

    Example:

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

    这对我来说没问题:

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

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

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

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

  • 8

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

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

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

相关问题