背景知识:

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

为什么会这样?


当前回答

my.ini

最后加上这一行:

default-time-zone = '+05:30'

从终端运行此命令

>> sudo mysql -e "SET GLOBAL time_zone = ‘+5:30’;" -u root
>> sudo mysql -e "SELECT @@global.time_zone;" -u root

如果两者都不工作,请尝试使用终端从sqoop使用此请求

>> sqoop list-databases --connect "jdbc:mysql://localhost/employees?serverTimezone=UTC" --username sqoop -P

或者你可以用这个替换你的请求URL

jdbc:mysql://localhost/employees?serverTimezone=UTC

其他回答

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

SET GLOBAL time_zone = '-6:00';

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

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

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

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

运行下面的查询到mysql DB来解决这个错误

MariaDB [xxx> SET @@global.time_zone = '+00:00';
Query OK, 0 rows affected (0.062 sec)

MariaDB [xxx]> SET @@session.time_zone = '+00:00';
Query OK, 0 rows affected (0.000 sec)

MariaDB [xxx]> SELECT @@global.time_zone, @@session.time_zone;

在应用程序内按位置为spring引导应用程序设置时区。属性文件

spring.datasource.url=jdbc:mysql://localhost:3306/db?serverTimezone=Europe/Berlin

解决了CET / CEST时区的问题。xml使用maven工件

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>

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

SET GLOBAL time_zone = '+3:00';