Py学习  »  DATABASE

与Java警告[复制]的MySQL连接

Amado • 4 年前 • 881 次点击  

使用下面的两个类,我尝试连接到一个mysql数据库。但是,我总是会得到这个错误:

2015年12月9日星期三22:46:52 CET警告:不建议在未经服务器身份验证的情况下建立SSL连接。根据mysql 5.5.45+、5.6.26+和5.7.6+的要求,如果未设置显式选项,则默认情况下必须建立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();
            }
        }
    }
}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40937
 
881 次点击  
文章 [ 10 ]  |  最新文章 4 年前
Ahmad.ak
Reply   •   1 楼
Ahmad.ak    7 年前

新版本的mysql connector默认情况下会建立ssl连接…要解决这个问题:

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

. . 或 . . 下载 OpenSSL for Windows 并按照说明设置

Aliaksandr Kavalenka
Reply   •   2 楼
Aliaksandr Kavalenka    5 年前

我在配置XML中将此属性用于Hibernate

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

无- 服务器时区=UTC -它不起作用

simhumileco
Reply   •   3 楼
simhumileco    6 年前

这对我来说没问题:

this.conn = (Connection)DriverManager
    .getConnection(url + dbName + "?useSSL=false", userName, password);
Nagendra KARTHIKEYAN.A
Reply   •   4 楼
Nagendra KARTHIKEYAN.A    6 年前

使用此项解决与mysql建立连接时在hive中出现的问题

<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>
Khachornchit Songsaen
Reply   •   5 楼
Khachornchit Songsaen    6 年前

我也发现了这个警告,然后我通过对连接字符串使用ssl=false后缀(如下面的示例代码)修复了它。

例子:

connectionString = "jdbc:mysql://{server-name}:3306/%s?useUnicode=yes&characterEncoding=UTF-8&useSSL=false"
Antoine
Reply   •   6 楼
Antoine    7 年前

您需要像这样使用mysql路径:

<property name="url" value="jdbc:mysql://localhost:3306/world?useSSL=true"/>
ArifMustafa
Reply   •   7 楼
ArifMustafa    6 年前

提到 url 像:

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

但是在xml配置中 & 签名,IDE显示以下错误:

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

然后你必须显式地使用 &amp; 而不是 & 确定为 & 通过 xml 此后 XML 您必须以XML配置提供URL,如下所示:

<property name="connection.url">jdbc:mysql://hostname:3306/hibernatedb?autoReconnect=true&amp;useSSL=false</property>
Jon
Reply   •   8 楼
Jon    7 年前

另一种方法是:

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

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

Peter DeGregorio
Reply   •   9 楼
Peter DeGregorio    7 年前

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

jdbc:mysql://localhost:3306/Peoples?verifyServerCertificate=false&useSSL=true
Joel Christophel Priyank Gosal
Reply   •   10 楼
Joel Christophel Priyank Gosal    7 年前

您的连接URL应该如下所示,

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

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