第一类错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
这个错误是指:连接超时
原因:mysql5将其连接的等待时间(wait_timeout)缺省为8小时。
解决方法:
前提得先进到mysql工作台:
1,先开mysql,输入 show global variables like 'wait_timeout'; 这句,就会显示出时间了,
mysql﹥ show global variables like 'wait_timeout';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| wait_timeout | 28800 |
+---------------+---------+
1 row in set (0.00 sec)
解释:Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。这就是问题的所。将会造成上面的异常。
2 set global wait_timeout=604800;
3 set global interactive_timeout=604800;
输入以上两句就可以解决这个问题了,
【有需要重启则重启数据库,或者重启电脑连接数据库生效】
第二类错误:The connection property 'zeroDateTimeBehavior' only accepts values of the form: 'exception', 'round'
原因:
JDBC url=jdbc:mysql://xxx.aliyuncs.com:3306/xxxxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
看看是不是多写空格
第三类错误:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized
MySQL乱码问题:MySQL 8.0以上版本(MySQL连接驱动和版本都是8.0以上)的时候出现的问题错误
解决:
问数据库的Url后面加上以下的语句即可:
?serverTimezone=GMT%2B8
如:jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
第四类错误:【最坑的】
Spring整合mybatis并使用driud数据库连接池,启动测试类就报”MySQLNonTransientConnectionException: Could not create connection to database server.
如果你的配置文件确认没错的话,不妨看看pom文件MySQLb版本问题,我的是5.1.2版本报了错,换成8.0.12版本重启,成功了!
参考配置文件:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
driver: com.mysql.cj.jdbc.Driver
有问题可以参考这篇:https://blog.csdn.net/beyond9305/article/details/80330125
有需自转,是否解决问题给我个反馈。