1. 程式人生 > 其它 >SpringBoot配置資料來源的驅動版本和時區問題

SpringBoot配置資料來源的驅動版本和時區問題

一、問題描述:

  1、使用的SpringBoot版本為2.2.2RELEASE

<spring-boot.version>2.2.2.RELEASE</spring-boot.version>

  2、引入mysql驅動的依賴時,預設版本為8.0.18

<version>8.0.18</version>

  3、在配置檔案中對資料庫的配置:

datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/shop?useUnicode=true&characterEncoding=UTF-8
username: root
password: '123456'

二、測試報錯

  org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is 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.

  Caused by: 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個小時

三、解決方法

  在datasource的url中最後再加上serverTimezone屬性serverTimezone=UTC或者serverTimezone=GMT%2B8

datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost/shop?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: '123456'