mysql url 中时区问题

 

 

最近在使用mysql的6.0.x以上的jar的时候,需要在代码url的链接里面指定serverTimezone。就会出现异常:

1.未指定serverTimezone

xml里面配置url

1
<property name="url" value="jdbc:mysql://localhost:3306/mybatisstudy"/>

出现的异常

1
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: 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配置属性),如果您想要使用时区支持,则需要使用一个更详细的时区值。

2.网上的解决方案

在url后面加上参数?serverTimezone=utc()

 

2.1.遇到的问题

虽然上面加上时区程序不出错了,但是我们在用java代码插入到数据库时间的时候却出现了问题。

比如在java代码里面插入的时间为:2017-08-21 17:29:56

但是在数据库里面显示的时间却为:2017-08-21 09:29:56

3.根本原因

因为时区设置的问题。

UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。

UTC + (+0800) = 本地(北京)时间

4.解决方案

url的时区使用中国标准时间。

第一种使用上海时间:serverTimezone=Asia/Shanghai

第二种使用北京时间:serverTimezone=GMT%2B8

 

原文地址:https://www.cnblogs.com/lqcswy/p/11812688.html