本文共 1276 字,大约阅读时间需要 4 分钟。
Spring boot 连接mysql数据库时,在部分环境中会出现The server time zone value 'EDT' is unrecognized异常。
com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'EDT' 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:mysql://ip:port/database_name?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
为了解决这个问题,按照网上资料,在url中添加serverTimezone=GMT
jdbc:mysql://ip:port/database_name?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT
问题解决,应用启动正常。
但是,为了节约资源,进行测试,应用和数据库部署在同一台机器上,所以,不存在数据库和应用所在服务器时区不统一的问题。因此,进一步探索原因。
在服务器中,我是用date命令查看时区如下
Tue Apr 14 23:01:35 EDT 2020
其时区为EDT,但是数据库中可用的时区列表为(通过执行 ll /usr/share/zoneinfo 查看)
数据库可使用的时区不存在EDT,这也就是异常提示信息中的“The server time zone value 'EDT' is unrecognized ”时区EDT不能识别的原因。而上述解决方案中,在连接中添加serverTimezone=GMT之所以能够解决问题,是因为GMT这个时区存在于数据库可用时区列表中,应用在向数据库中添加时间数据时,数据库可以识别时区信息。由此可见,serverTimezone指定其他存在于列表中的时区也是可以,比如,将serverTimezone设置为UCT、UTC之类的。
数据库存储时间之所以会存在时区问题可参照廖雪峰老师网站的博客
参考资料:
转载地址:http://pfktn.baihongyu.com/