引言
在使用IDEA(IntelliJ IDEA)进行Java开发时,连接MySQL数据库是常见的操作。然而,许多开发者会遇到一个令人头疼的问题——时区不一致导致的连接失败。(Mysql版本问题导致)
<property name="url" value="jdbc:mysql:///your_shujuku_name"/>
在idea的项目中,配置xml的数据库connection时,由于Mysql版本8以下的基本都是写以上代码即可,但对于Mysql8及8以上的更高版本需要注意失时区问题。
问题来源:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: 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.
### Cause: java.sql.SQLException: 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.
看到idea的报错内容显示这些就可以断定是:服务器的时区设置与IDEA期望的时区不一致,导致连接失败。
解决方案:
简单的临时解决办法就是将数据库connection的url处改为:
<property name="url" value="jdbc:mysql://localhost:3306/your_shujuku_name?serverTimezone=UTC"/>
重要的在后面的 serverTimezone=UTC
之后重新测试即可成功。
其他情况:
MySQL驱动版本: 确保你使用的MySQL驱动版本与数据库版本兼容。可以在 pom.xml 中添加以下依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
这里注意噢,一定要和你自己的数据库版本相兼容才行,不要从某个地方直接拿来复制粘贴上就完事了,你可以把粘贴来的<version>处将版本号删除,键盘键入一个英文.即可出现提示,选择相应符合的版本即可。
结语
通过以上步骤,你应该能够成功解决IDEA连接MySQL数据库时区不一致的问题。希望本文对你有所帮助,如果在操作过程中遇到任何问题,欢迎留言讨论。祝你开发顺利!