背景知识:

我有一个运行在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 bug线程的一些见解,我明白了:

the server time zone is important in particular to convert dates stored in the database to the time zone of the application server. there are other implications but this is the most noticeable one GMT x UTC time zone systems. GMT was conceived in the late 19th century and can be shifted between standard time and summer time. this property could lead to a situation where the database server shifts to summer time and the application doesn't notice it (perhaps there are other complications but I didn't research further). UTC does not vary over time (it is always within about 1 second of mean solar time at 0° longitude). serverTimeZone definition was introduced in mysql jdbc connectors versions 5.1 ahead. until version 8 it could be ignored with useLegacyDatetimeCode=true , which in conjunction with useJDBCCompliantTimezoneShift=true would make the application get the database time zone on every connection. In this mode GMT time zones such as 'British Summer Time' would be converted to the internal java/JDBC format. New time zones could be defined in a .properties file such as this one Starting with jdbc driver version 8, automatic time matching (useJDBCCompliantTimezoneShift) and legacy time format (useLegacyDatetimeCode) were removed (see mysql jdbc connector changelog). therefore setting these 2 parameters has no effect as they are completely ignored (new default is useLegacyDateTimeCode=false) In this manner setting serverTimezone became mandatory if any of the time zones (application/database servers) are not in the format 'UTC+xx' or 'GMT+xx' There is no impact of setting server time as UTC (for instance with jdbc:mysql://localhost:3306/myschema?serverTimezone=UTC, even if your application / database servers are not in this timezone. The important is for the application connection string + database to be synchronized with the same time zone. In different words, simply setting serverTimezone=UTC with a different time zone on the database server will shift any dates extracted from the database The MySQL default time zone can be set to UTC+0 with the my.ini or my.cnf files (windows / linux respectively) by adding the line default-time-zone='+00:00' (details in this StackOverflow post) Databases configured on AWS (amazon web services) are automatically assigned UTC+0 default time (see AWS help page here)

其他回答

我解决了把下面的连接字符串在URL

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

我遇到了同样的错误,在我的情况下,我将服务器端口号更改为3308,之前是3306。这将我的项目连接到MySQL数据库。

这里我们还必须更改连接代码。

Class.forName("com.mysql.cj.jdbc.Driver");
cn=(java.sql.Connection)DriverManager.getConnection("jdbc:mysql://localhost:3308/test2?zeroDateTimeBehavior=convertToNull","root","");

更改连接代码中的端口号也是必要的,如localhost:3308以解决错误。

在我的例子中,还有管理属性。

显然,要使MySQL JDBC驱动程序5.1.33版本与UTC时区兼容,必须在连接字符串中显式指定serverTimezone。

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

我正在使用mysql-connector-java-8.0.13,遇到了同样的问题。 我在命令行控制台创建了我的数据库,并在命令行上使用@Dimitry Rud的解决方案解决了这个问题:

SET GLOBAL time_zone = '-6:00';

我不需要重新启动任何东西,设置时间并立即在eclipse中运行我的代码,它连接起来没有任何问题。

这个错误应该在旧版本中得到修复,但我想我得到了这个错误,因为我在控制台中创建数据库后,我没有设置这个。我没有使用工作台或其他应用程序来管理这个,而不是控制台。

我也有同样的问题,我解决了它只附加”?serverTimezone=UTC"到我的字符串连接。

#

我的问题是:

java.sql.SQLException:服务器时区值“CEST”无法识别或表示多个时区。如果希望利用时区支持,则必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性)以使用更特定的时区值。

my dbDriver = com.mysql.jdbc.Driver

my jar = mysql-connector-java-8.0.12.jar

my java = 1.8

my tomcat = Apache Tomcat Version 8.5.32

my MySql server = MySql ver.8.0.12