
在使用 SpringBoot+jdbcTemplate 连接 MySQL 数据库时出现如下报错:
1 | Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���ʱ��' 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. |
报错提示很明确,JDBC driver 需要添加 serverTimezone 配置项。所以在配置 JDBC 连接时在 url 中添加参数即可:
1 | spring: |
问题是之前使用 SpringBoot1.5.8 时不会有这样的问题,升级到 SpringBoot2.2.5 却有这样的问题。
后来查阅资料才知道,MySQL 的 JDBC 驱动从 6.0 版本以上就必须设置 serverTimezone 参数。而 SpringBoot1.5.8 使用的是 5.1.44 版本,SpringBoot2.2.5 使用的是 8.0.19 版本。看来以后项目升级版本还是要谨慎,这就是潜在的坑啊。
在设置 serverTimezone 时也要注意,我们是在东八区,所以需要设置成 GMT%2B8 或 Asia/Shanghai,不要设置成 GMT 或 UTC 了。