前言
最近安裝了一個 mysql 8.0
版本的數(shù)據(jù)庫,在程序中連接的時候可謂是狀況不斷。之前也會遇到一些問題,這里就對使用 JDBC 連接mysql 會出現(xiàn)的問題做一個匯總。
在此之前說明一下環(huán)境:
開發(fā)工具:IDEA
mysql版本: 8.0.12 for Win64 on x86_64 (MySQL Community Server - GPL)
mysql驅(qū)動包:8.0.12
驅(qū)動包URL 的改變
異常信息
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.
原因
通過異常我們可以發(fā)現(xiàn),新的驅(qū)動url是com.mysql.cj.jdbc.Driver
,經(jīng)過在網(wǎng)上查閱資料發(fā)現(xiàn)觉吭,從 mysql6
開始,驅(qū)動包開始使用新的驅(qū)動 url仆邓。如果使用舊的 5.0 版本的驅(qū)動包鲜滩,則不用驅(qū)動URL伴鳖,但是如果使用舊的驅(qū)動可能會出現(xiàn)一些意想不到的問題。所以還是建議將驅(qū)動包升級绒北,然后改變 驅(qū)動 URL 的值黎侈。
解決方法
將驅(qū)動 URL 由com.mysql.jdbc.Driver
換成 com.mysql.cj.jdbc.Driver
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.
原因
對警告信息翻譯如下。
不建議在沒有服務器身份驗證的情況下建立SSL連接闷游。根據(jù)MySQL 5.5.45+峻汉,如果未設置顯式選項,則默認情況下必須建立5.6.26+和5.7.6+要求的SSL連接脐往。對于不使用SSL的現(xiàn)有應用程序休吠,ValuyServer證書屬性設置為“false”。您需要通過設置useSSL=false來顯式禁用SSL业簿,或者設置useSSL=true并提供用于服務器證書驗證的信任庫`瘤礁。
解決方法
一般在開發(fā)中基本不需要使用 SSL 連接,在連接字符串后添加useSSL=false
參數(shù)就行梅尤。但是如果真的有 SSL 連接的需要柜思,則在驅(qū)動 URL 后添加useSSL=true
參數(shù)。
jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8&useSSL=false
時區(qū)問題
異常信息
java.sql.SQLException: The server time zone value '?D1ú±ê×?ê±??' 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.
原因
同樣也是由于版本升級后巷燥,新的版本數(shù)據(jù)庫和系統(tǒng)之間有了時區(qū)差異赡盘,需要指定時區(qū)serverTimezone
解決方法
-
連接字符串后添加參數(shù)
&serverTimezone=GMT%2B8
,最終連接字符串如下:jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
-
修改數(shù)據(jù)庫時間缰揪。先通過命令行連上數(shù)據(jù)庫陨享,依次輸入命令及其輸出如下
mysql> show variables like "%time_zone"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | | | time_zone | SYSTEM | +------------------+--------+ 2 rows in set, 1 warning (0.04 sec) mysql> set global time_zone="+8:00"; Query OK, 0 rows affected (0.01 sec)
XML 配置文件中 & 的轉(zhuǎn)義
異常信息
org.mybatis.generator.exception.XMLParserException: XML Parser Error on line 16: 對實體 "useSSL" 的引用必須以 ';' 分隔符結尾。
原因
這是我在使用mybatis generator
時出現(xiàn)的錯誤钝腺。當時我想在連接字符串后加上useSSL
參數(shù)抛姑,但是由于在 XML
文件中,&
是被禁止的艳狐,所以需要使用 &
的時要用它的轉(zhuǎn)義&
來代替定硝。
解決方法
將連接字符串中的 &
符號改成&
詳細連接字符串參考
jdbc:mysql://127.0.0.1:3306/dbname?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=GMT%2B8&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true
當然如果是使用 XML 作為配置文件,需要將 連接字符串中的 &
符號改成&