MySQL database 8.0.12 version of common problems

使用最新版本MySQL報錯,MySQL版本8.0.12。

報錯1:
在這裏插入圖片描述
Sun Oct 14 00:45:30 CST 2018 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.
不建議在沒有服務器身份驗證的情況下建立SSL連接。根據MySQL 5.5.45+,5.6.26+和5.7.6+的SSL連接要求,如果未設置連接方式,則默認情況下必須建立SSL連接。對於不使用SSL的現有應用程序,服務器的驗證證書屬性設置爲“false”。您需要通過設置useSSL=false來顯式禁用SSL,或者設置useSSL=true並提供服務器的驗證證書。

解決方法:
1、數據庫URL連接地址添加useSSL=false,適用於測試。
2、數據庫URL連接地址添加useSSL=true,並且提供服務器的驗證證書。

jdbc.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false

報錯2

2018-10-14 00:45:30,876 ERROR [DruidDataSource.java:616] : init datasource error
java.sql.SQLException: The server time zone value ‘???ú±ê×??±??’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
服務器時區值“????±××?±?無法識別或代表一個以上的時區。如果希望利用時區支持,則必須配置服務器或JDBC驅動程序(通過serverTimezone配置屬性)以使用更具體的時區值。

解決方法:
使用的MySQL數據庫版本8.0.12,數據庫連接驅動是8.0.12。
由於數據庫和系統時區差異造成報錯,需要在數據庫URL連接地址後面加上serverTimezone=GMT。
如果需要使用gmt+8時區,需要寫成GMT%2B8,否則會被解析爲空。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.12</version>
</dependency>
jdbc.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false

報錯3:
在這裏插入圖片描述
Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
不建議使用驅動類’com.mysql.jdbc.Driver’。新的驅動程序類是’com.mysql.cj.jdbc.Driver’,驅動程序是通過SPI自動註冊的,通常是不需要手動加載驅動類。

解決方法:
MySQL數據庫8.0.12使用的驅動類爲com.mysql.cj.jdbc.Driver,不要使用舊的驅動類。

jdbc.driverClass=com.mysql.cj.jdbc.Driver

連接mysql數據庫出錯

以往版本的mysql連接中,程序中配置的mysql連接驅動爲com.mysql.jdbc.Driver,但是8.0.12版本的mysql數據庫驅動已更改,變更爲:com.mysql.cj.jdbc.Driver,所以配置文件中的數據庫驅動必須相應做更改<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>,同時maven項目的pom.xml文件中的依賴更新爲:

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

mysql8.0是不需要建立ssl連接的,你需要顯式關閉。最後你需要設置CST,代碼變變更爲:

 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?useSSL=false&serverTimezone=UTC","root","password");

最後,測試連接,一切ok!

Java連接mysql8.0.12的時候遇到一些問題,

之前一直使用的都是5.6的MySQL,所以選擇連接的時候也就下意識的使用了舊版本的連接
然後測試的時候就報錯了
一查說是連接版本問題。於是升級到最新的8.0.12,又出問題了:

再一查,原來新版的驅動配置有些變化:

  1. url連接必須設置時區

MySQL Connector/J 5.x (舊版連接)

#jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8

MySQL Connector/J 6.x (新版連接)

jdbc.url=jdbc:mysql:///test?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false

說明: 新版驅動url默認地址爲127.0.0.1:3306,所以訪問本機mysql數據庫地址可以用 /// 表示。

  1. 新的驅動類位置有了變化(不影響使用,但會報警告)

MySQL Connector/J 5.x (舊版驅動) jdbc.driver_class=com.mysql.jdbc.Driver

MySQL Connector/J 6.x (新版驅動) jdbc.driver_class=com.mysql.cj.jdbc.Driver

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章