背景知识:

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

其他回答

你可以在Maven依赖中使用MySQL连接器,

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

然后需要在应用程序中设置正确的参数。属性文件,

spring.datasource.url=jdbc:mysql://localhost:3306/UserReward?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password=testpassword
# MySQL driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

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

SET GLOBAL time_zone = '+3:00';

这对我很管用。

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

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

jdbc: mysql: / / localhost: / ?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以解决错误。

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

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