本文主要是介绍The server time zone value ‘xx‘ is unrecognized or represents more than one time zone(SpringBoot错误),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错误描述
SpringBoot连接MySQL数据库运行报错
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 specific time zone value if you want to utilize time zone support.
错误原因
时区问题,服务器的时区的识别或代表多个时区。如果您想利用时区支持,您必须配置服务器或JDBC驱动程序(通过serverTimezone
配置属性)使用一个更具体的时区值如果你想利用时区支持。
解决办法
在配置文件的时候添加时区信息就可以解决问题了。
在连接数据库的url后面添加
serverTimezone=GMT%2B8 # 添加时区,GMT%2B8是东八区(也就是北京时间)serverTimezone=UTC # UTC,简称世界统一时间,跟北京时间相比,比北京早8个小时
【注意**】建议使用
serverTimezone=GMT%2B8
设置为东八区,如果使用serverTimezone=UTC
,那么在编译器上执行的SQL语句,会先以UTC时区进行存储,发送到MySQL数据库,然后MySQL以本地时区进行转换,最终导致执行时间比从编译器上的执行时间早8个小时,也就是造成同一条SQL语句,在MySQL上执行,与编译器执行,结果相差8个小时。
如果没有自定义设置时区,MySQL数据库默认使用的时区是系统时区CST,这个CST又有好多种解释,分别为如下。而MySQL服务器又把CST理解成了美国中部时区(),结果就使用了美国中部时间的时区,而不是后端系统服务器的时区。
application.yml
spring:datasource: # hikaridriver-class-name: com.mysql.cj.jdbc.Driver # mysql 8username: rootpassword: rooturl: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 # serverTimezone=GMT%2B8表示东8区,不然报错time zone
这篇关于The server time zone value ‘xx‘ is unrecognized or represents more than one time zone(SpringBoot错误)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!