背景知识:

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

为什么会这样?


当前回答

同意@bluecollarcoder的答案,但最好使用TimeZone.getDefault().getID();在连接字符串的末尾:

"jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID();  

在这种情况下,Timezone参数根据本地机器时区自动更新。

其他回答

我得到的错误类似于你的,但我的服务器时区值是'Afr。舟状骨的西部省 所以我做了这些步骤:

MyError (IntelliJ IDEA社区版):

    InvalidConnectionAttributeException: The server time zone value 'Afr. centrale Ouest' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to u....

当我把mysql服务器升级到SQL server 8.0 (MYSQL80)时,我遇到了这个问题。

这个问题最简单的解决方案是在MYSQL Workbench中编写下面的命令-

  SET GLOBAL time_zone = '+1:00'

时区之后的值将等于您所在时区的GMT+/-差值。上面的例子是北非(GMT+1:00) /或印度(GMT+5:30)。 这将解决问题。

在Mysql Workbench中输入以下代码并执行查询

[问题/问题的来源链接]

[答案来源链接]

[解决方案截图]

你可以在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配置更改。 我能够通过遵循@vinnyjames的建议并将服务器时区更改为UTC来修复这个问题:

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

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

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

我也得到了相同的运行java JDBC在NetBeans。这就是它如何修复的

我使用Xampp。在Apache的conf按钮中,我打开httpd.conf文件,并在第一行输入

# Set timezone to Europe/Athens UTC+02:00 
SetEnv TZ Europe/Athens.

在MySQL的conf按钮中,我打开了我的.ini文件,在最后一行我输入 “欧洲/雅典”

停止并启动Apache和MySQL

固定的问题。

*(当地机器时区不同,但没有问题)