背景知识:

我有一个运行在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.

为什么会这样?


当前回答

我通过配置MySQL解决了这个问题。

SET GLOBAL time_zone = '+3:00';

其他回答

如果你使用Maven,你可以在pom.xml中设置另一个MySQL连接器版本(我也有同样的错误,所以我从6.0.2更改为5.1.39):

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

正如在另一个回答中报告的那样,这个问题在6.0.3或更高版本中已经修复,所以您可以使用更新版本:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.3</version>
</dependency>

保存pom.xml文件后,Maven将自动重新构建项目。

我在数据库端执行了以下命令。

mysql> SET @@global.time_zone = '+00:00';

mysql> SET @@session.time_zone = '+00:00';

mysql> SELECT @@global.time_zone, @@session.time_zone;

我使用的服务器版本:8.0.17 - MySQL社区服务器- GPL

来源:https://community.oracle.com/thread/4144569?start=0&tstart=0

在我的例子中,它是一个测试环境,我必须让一个现有的应用程序在没有任何配置更改的情况下工作,如果可能的话,没有任何MySQL配置更改。 我能够通过遵循@vinnyjames的建议并将服务器时区更改为UTC来修复这个问题:

ln -sf /usr/share/zoneinfo/UTC /etc/localtime
service mysqld restart

这样做对我来说已经足够解决问题了。

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/resultout? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root",""))

这实际上是这个问题的解决方案,但不要只是复制粘贴到您的程序中。如果你只看这一行,你会发现'resultout',这是我的数据库的名称,你必须写下你的。

有三个字符串组件,第一个是url,第二个是用户名,第三个是密码。在上述段落中,我们清除了,url。第二个和第三个字符串组件,你的用户名和密码,你必须相应地改变。

谢谢

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

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

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