背景知识:

我有一个运行在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 JDBC驱动程序5.1.33版本与UTC时区兼容,必须在连接字符串中显式指定serverTimezone。

spring.datasource.url = jdbc:mysql://localhost:3306/quartz_demo?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

其他回答

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

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

祝你好运。

这对我很管用。

在DBeaver 6.0上:进入“连接设置”>驱动程序属性>服务器时区>设置UTC时间。

另外,在春季启动配置中,必须设置下面的属性。

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

如果你使用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将自动重新构建项目。

在应用程序内按位置为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>

只需在应用程序中使用以下代码修改连接字符串。属性文件。


spring.datasource.url=jdbc:mysql://localhost:3301/Db?
   useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=
   false&serverTimezone=UTC