1. 程式人生 > >MySQL升級8.0後連線不上資料庫

MySQL升級8.0後連線不上資料庫

將MySQL 升級為 8.0 後,無法連線到資料庫,首先報錯資料庫驅動需要修改,其次要禁用 SSL 連線。

警告資訊如下:

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. 

解決辦法如下:

1,修改資料庫配置檔案

druid.driver=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/babsport12?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
druid.username=root
druid.password=123456

注意:

  • MySQL 8.0 中驅動包由 “com.mysql.jdbc.Driver” 改為 “com.mysql.cj.jdbc.Driver”
  • 要顯式地禁用 SSL,useSSL=false
  • 新增時區 serverTimezone(MySQL jdbc 6.0 版本以上必須新增時區引數),UTC 為全球標準時間,但我們需要使用北京時區,設定serverTimezone=Asiz/Shanghai

2,修改 pom 配置

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.12</version>
</dependency>

修改完成後,更新依賴。

以上步驟完成後,MySQL 資料庫就可以正常連線了。