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'
浙公网安备 33010602011771号