使用MySQL 最新JDBC驱动包报错The server time zone value is unrecognized or represents

使用MySQL 最新JDBC驱动包报错The server time zone value is unrecognized or represents

The server time zone value is unrecognized or represents

今天在配置IntelliJ IDEA 2019.1 x64连接到MySQL 5.5.56时,使用了MySQL最新的JDBC驱动8.0.15,在测试连接时报错了。

报错如下:

java.lang.RuntimeException:
 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.NativeConstructorAccessorImpl.newInstance0(Native Method)	
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)	
 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:61)	
 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)	
 at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)	
 at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2241)	
 at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2265)	
 at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)	
 at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:868)	
 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:830)	
 at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)	
 at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)	
 at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)	
 at com.intellij.database.remote.jdbc.impl.RemoteDriverImpl.connect(RemoteDriverImpl.java:41)	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	
 at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)	
 at sun.rmi.transport.Transport$1.run(Transport.java:200)	
 at sun.rmi.transport.Transport$1.run(Transport.java:197)	
 at java.security.AccessController.doPrivileged(Native Method)	
 at sun.rmi.transport.Transport.serviceCall(Transport.java:196)	
 at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)	
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)	
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)	
 at java.security.AccessController.doPrivileged(Native Method)	
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)	
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)	
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)	
 at java.lang.Thread.run(Thread.java:748)
  (no stack trace).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..

错误信息里写的很清楚,The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone。使用mysql connector/J 8.0.13之前的驱动时,从来没有关注过这个参数。这次竟然栽在它身上了。

解决方法

1.如果使用的是连接串,直接在最后面加上serverTimezone=UTC或serverTimezone=UTC,如下:

jdbc:mysql://localhost:3306/your_databse&serverTimezone=UTC
或者
jdbc:mysql://localhost:3306/your_databse&serverTimezone=GMT

唯一需要注意的是 UTC 和 GMT 必须大写,不然还会报错。

2.如果你使用的是管理工具,像我比较懒,直接在IntelliJ IDEA 2019.1 x64里面管理数据库,直接按下图操作就行。

使用MySQL 最新JDBC驱动包报错The server time zone value is unrecognized or represents使用MySQL 最新JDBC驱动包报错The server time zone value is unrecognized or represents