对于Spring项目,mysql连接器java
已从6.0.6
迁移到8.0.11
。
因此,对于8.0.11
,问题如下:
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException:
The server time zone value 'PET' 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 utilize time zone support.
做研究之后
解决方案是更改URL
(我不想返回到以前的版本)
观察添加
在我的数据库中,我有以下内容:
mysql> select * from persona;
+-----+--------------+-------------+------------+
| id | nombre | apellido | fecha |
+-----+--------------+-------------+------------+
...
| 088 | Something | Something | 1981-07-06 |
...
+-----+--------------+-------------+------------+
当Spring
应用程序通过RowMapper从数据库进行检索时
如果mysql连接器java
返回到6.0.6
,因此mysql。jdbcUrl=jdbc:mysql://localhost:3306/web_v01?useSSL=false
(无服务器时区=UTC
)rs。getDate(“fecha”)
返回1981-07-06
(预期如何)
因此,如何使用8.0.11
修复此问题?。
当从一开始就没有声明serverTimezone
时,我希望具有相同的行为,当然避免了异常。
因此,如果考虑到声明了服务器时区
的值无关紧要,解决方案会更好。
有几个与时区相关的属性:
使用时区:在客户端和服务器时区之间转换时间/日期类型(真/假,默认为“假”)?这是遗留日期时间代码的一部分,因此该属性仅在“use LegacyDatetime Code=true”时生效默认值:假
useLegacyDatetimeCode:在结果集和语句中使用DATE/TIME/DATETIME/TIMESTAMP处理代码,以一致地处理从客户端到服务器的时区转换,然后再次返回,还是为这些数据类型使用驱动程序中的旧代码以实现向后兼容性?将此属性设置为“false”会使“useTimezone”、“useJDBCCompliantTimezoneShift”、“useGmtMillisForDatetimes”和“useFastDateParsing”的效果无效。默认值:true
服务器时区:覆盖时区检测/映射。当服务器的时区不映射到Java时区时使用
如果mysql连接器java为5.1,则应指定三个属性,如:jdbc:mysql://host:port/dbname?useTimezone=true
如果 mysql-connector-java 是 8.0,你应该指定一个属性,如下所示:jdbc:mysql://host:port/dbname?serverTimezone=GMT+08:00
尝试使用
jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=CST
对我来说,最不可能的嫌疑人是杰克逊。
如果使用Spring Boot,请将以下属性添加到application.properties
spring.jackson.time-zone=Asia/Colombo
我已经详细回答了这个问题-