今天遇到个问题,使我们OA报的错误,连接MySQL的数据源不行了,不知道为啥,后台日志是这样的。
Caused by: com.mysql.cj.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.
at sun.reflect.GeneratedConstructorAccessor520.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
看这个描述吧,像是时区什么的问题,但是我就是创建个数据源,和时间还有关?
然后我就开始了百度,找到了一些文章,还真是时区的问题。
解决方案
既然是时区的问题,那就有几种方式来修改,
1个是直接修改配置文件,但是需要重启服务:
在my.ini文件中,在[mysqld]下,增加时区配置
# Set default time zone
default-time-zone = '+08:00'
另一种方案是通过SQL在线修改,但是重启服务后会失效
set global time_zone='+8:00';
flush privileges;
我们可以通过命令查看当前的参数配置:
show variables like '%time_zone%';
没修改之前,我这里显示的是SYSTEM
如果是代码或者工具配置,可以通过修改URL的方式来解决,就是增加时区的参数
jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC
jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
修改完之后,记得使用 select now() 来测试下
select now();