背景知识:

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

为什么会这样?


当前回答

连接字符串应该像这样设置:

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

如果你在xml文件(如persistence.xml, standalone-full.xml等)中定义连接,你应该使用&或者使用CDATA块。

其他回答

你可以在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

当我尝试在windows上使用spring boot项目时,我遇到了同样的问题。

数据源url应该是:

spring.datasource.url = jdbc: mysql: / / localhost /数据库?useUnicode = true&useJDBCCompliantTimezoneShift = true&useLegacyDatetimeCode = false&serverTimezone = UTC

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/resultout? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root",""))

这实际上是这个问题的解决方案,但不要只是复制粘贴到您的程序中。如果你只看这一行,你会发现'resultout',这是我的数据库的名称,你必须写下你的。

有三个字符串组件,第一个是url,第二个是用户名,第三个是密码。在上述段落中,我们清除了,url。第二个和第三个字符串组件,你的用户名和密码,你必须相应地改变。

谢谢

我得到的错误类似于你的,但我的服务器时区值是'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中输入以下代码并执行查询

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

[答案来源链接]

[解决方案截图]

我在[mysqld]部分添加了mysql配置文件 default_time_zone = ' + 03:00 ' 重启mysql服务器: Sudo服务mysql重启

其中+03:00我的UTC时区。

在我的操作系统ubuntu 16.04上配置文件的路径:

/etc/mysql/mysql.conf.d/mysqld.cnf

警告:如果你所在的时区有夏季和冬季时间。如果要更改时间,必须在配置中更改utc。一年两次(通常)或用sudo设置crontab。

我的url jdbc连接

"jdbc:mysql://localhost/java"