SpringBoot連接MySQL數據庫時候報時區錯誤的解決辦法

新建Springboot項目連接Mysql數據庫時候經常會遇到如下錯誤:

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.

從上面一段話,我們可以看出是由於數據庫服務器時區無法識別或者超過一個時區導致,所以我們只需要給指定一個時區即可解決,有兩種解決辦法。

1、數據庫設置時區

//查看數據庫時區信息
show variables like '%time_zone%'
//設置時區爲東八區時區
set global time_zone = '+8:00'

 root用戶登錄數據庫執行sql語句即可(在數據庫連接工具中查詢執行也可)。

2、項目中配置文件中數據庫連接url中加&serverTimezone=GMT%2B8

只需要加上如下圖配置即可:

配置奉上: 

datasource:
    url: jdbc:mysql://192.168.1.63:3306/mybatistest?        
         useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

以上兩個辦法親測有效。

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