在使用mysql的6.0.x以上的jar的时候,需要在代码url的链接里面指定serverTimezone。否则就会出现异常。
如果在url中未指定serverTimezone
url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8
则会出现: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 此异常
因为之前这种问题确实没有遇到过,因为google后,找到的答案是在url后面加上参数?serverTimezone=utc
eg:url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false
此时虽然服务可以正常启动了,程序不报错了,但是又会有新的问题发生,就是我们在用java代码插入到数据库时间的时候却出现了问题。
比如在java代码里面插入的时间为:2019-04-12 13:29:56
但是在数据库里面显示的时间却为:2019-04-12 05:29:56
仔细一想,UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。所以应该是:UTC + (+0800) = 本地(北京)时间
解决方案:url的时区使用中国标准时间,因此需要修改时区:
serverTimezone=Asia/Shanghai