Spring连接Mysql时遇到的TimeZone的问题

Posted 原始锋芒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring连接Mysql时遇到的TimeZone的问题相关的知识,希望对你有一定的参考价值。

我的mysql版本是 8.0.18

使用的Driver是 com.mysql.cj.jdbc.Driver

当我们连接时没有指定时区,mysql会使用设置中time_zone所设置的时区,这个值默认是SYSTEM,也就是指向system_time_zone这个变量,在中国,这个值显示是CST

mysql> show variables like "%time_zone%";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+

CST可以代表四个时区!!这是一个有歧义的名字!坏就坏在这里了。java会以为这个CST是美国中部时间 (Central Standard Time )。这样就导致了JDBC获取TimeStamp时的时区出错!。

解决方法有两种:

  1. 连接时指定时区,加参数serverTimezone=Aisa/Shanghai
  2. 改mysql变量,从SYSTEM改为+08:00,方法是set global time_zone = \'+08:00\';

相关文章: https://www.cnblogs.com/bgo-tech/p/6929701.html

以上是关于Spring连接Mysql时遇到的TimeZone的问题的主要内容,如果未能解决你的问题,请参考以下文章

Spring数据,MySQL,连接在8小时不活动后死亡

mysql jdbc timezone

IDEA连接mysql:Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezon

带有Spring引导的Docker中的Mysql连接错误

pycharm连接mysql报错!Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTi

DataGrip软件Mysql连接失败Server returns invalid timezone. Go to 'Advanced' tab and set 'server