背景知识:

我有一个运行在Tomcat 7上的Java 1.6 web应用程序。数据库为MySQL 5.5。之前,我使用Mysql JDBC驱动程序5.1.23连接到DB。一切工作。我最近升级到Mysql JDBC驱动程序5.1.33。升级后,Tomcat在启动应用程序时会抛出这个错误。

WARNING: Unexpected exception resolving reference
java.sql.SQLException: The server timezone value 'UTC' is unrecognized or represents
  more than one timezone. You must configure either the server or JDBC driver (via
  the serverTimezone configuration property) to use a more specifc timezone value if
  you want to utilize timezone support.

为什么会这样?


当前回答

我没有修改任何代码就解决了这个问题。只需进入系统时间设置和设置时区。在我的情况下,默认时区是UTC,我将其更改为我的本地时区。重启所有服务后,一切都恢复正常了。

其他回答

我只需要在application.properties上添加serverTimeZone=UTC就可以了。 spring.datasource.url = jdbc: mysql: / / localhost / db吗?serverTimezone = UTC

连接字符串应该像这样设置:

jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

如果你在xml文件(如persistence.xml, standalone-full.xml等)中定义连接,你应该使用&或者使用CDATA块。

这是mysql-connector-java从5.1.33到5.1.37版本的一个错误。 我已经在这里报告了:http://bugs.mysql.com/bug.php?id=79343

编辑: 这已经从mysql-connector-java 5.1.39修正

这是在loadtimezonemapping方法中的TimeUtil类中出现的一个拼写错误,该方法会引发NPE定位/com/mysql/jdbc/TimeZoneMapping。属性文件。如果你看一下代码,文件应该位于TimeUtil类装入器中,而不是TimeZone:

TimeUtil.class.getResourceAsStream(TIME_ZONE_MAPPINGS_RESOURCE);

参数useLegacyDatetimeCode允许在使用日期时自动纠正客户端和服务器时区之间的差异。因此,它帮助您精确地不必在每个部分指定时区。虽然使用serverTimeZone参数是一种变通方法,并且在发布补丁的同时,您可以像我一样尝试自己更正代码。

如果它是一个独立的应用程序,您可以尝试简单地添加 纠正com/mysql/jdbc/TimeUtil类到你的代码,要小心 与罐子装载顺序。这可以帮助: https://owenou.com/2010/07/20/patching-with-class-shadowing-and-maven.html 如果它是一个web应用程序,更简单的解决方案是创建自己的应用程序 Mysql-connector-java-5.1.37-patched.jar,直接替换。class 放进原来的罐子里。

上面的程序将生成该时区错误。

在您的数据库名称之后,您必须添加这个:?useTimezone=true&serverTimezone=UTC。一旦你完成了这些,你的代码就可以正常工作了。

祝你好运。

我迟到了,但如果你正在努力克服以下错误并使用数据源(javax.sql.DataSource):

The server time zone value 'CEST' is unrecognized or represents more than one time zone.

设置以下行以消除错误:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerTimezone("UTC");