Java的报错解决: Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

本文介绍了在学习MyBatis时遇到的CommunicationsLinkFailure问题,该问题导致数据库更新失败。作者提供了详细的错误日志,并指出问题可能与SSL连接有关。通过在jdbc.properties的URL中添加`useSSL=false`参数,成功解决了该问题,确保了与MySQL8服务器的连接。此解决方案适用于遇到相同警告信息的开发者。

我们在学习mybatis中会出现Communications link failure报错,我当时找了一天都没有找到合适的解决方案

Sun Aug 07 08:30:43 CST 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. 
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. 
For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. 
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
 
org.apache.ibatis.exceptions.PersistenceException: 
 Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.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.
 The error may exist in mappers/userMapper.xml
The error may involve com.example.mapper.userMapper.insertUser
The error occurred while executing an update
Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

下面是我的jdbc.properties中的内容

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
username=root
password=123456

解决方法:在url中添加useSSL=false,就可以解决,不知道你们的是否和我的一样,我的mysql是8版本的。

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC
username=root
password=123456

我感觉可能是下面这个代码的报错引起Communications link failure,在url中添加useSSL=false就是解决下面的报错的,如果你没有下面的报错,可能这个方法就不好使了

Sun Aug 07 08:30:43 CST 2022 WARN: Establishing SSL connection without server's identity verification is not recommended. 
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. 
For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. 
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
当查询超过10s出现 'Error querying database. Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure' 错误,通常是由于数据库连接中断、网络问题、数据库服务器配置等原因导致的,以下是一些可能的解决方法: ### 检查网络连接 - **确保网络稳定**:检查应用程序服务器与 MySQL 数据库服务器之间的网络连接是否正常,可使用 `ping` 命令测试网络连通性,使用 `telnet` 命令测试端口是否开放。例如: ```bash ping <数据库服务器IP> telnet <数据库服务器IP> 3306 ``` - **检查防火墙设置**:确保防火墙没有阻止应用程序服务器与数据库服务器之间的通信。需要开放 MySQL 服务器使用的端口(默认是 3306)。如果是 Linux 系统,可以使用以下命令开放端口: ```bash sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent sudo firewall-cmd --reload ``` ### 检查数据库服务器配置 - **调整连接超时时间**:在 MySQL 配置文件(通常是 `my.cnf` 或 `my.ini`)中,可以调整 `wait_timeout` 和 `interactive_timeout` 参数,增加连接的超时时间。例如: ```ini [mysqld] wait_timeout = 28800 interactive_timeout = 28800 ``` 修改后重启 MySQL 服务。 - **检查最大连接数**:确保数据库服务器的最大连接数足够。可以通过以下 SQL 语句查看和修改最大连接数: ```sql -- 查看当前最大连接数 SHOW VARIABLES LIKE 'max_connections'; -- 修改最大连接数 SET GLOBAL max_connections = 500; ``` ### 检查应用程序配置 - **调整连接池配置**:如果使用了连接池,需要调整连接池的配置参数,如最大空闲时间、最大连接数等。以 HikariCP 为例,可以在配置文件中进行如下配置: ```properties spring.datasource.hikari.maximum-pool-size=20 spring.datasource.hikari.idle-timeout=30000 ``` - **检查数据库连接 URL**:确保数据库连接 URL 正确,包括数据库服务器地址、端口、数据库名等信息。例如: ```java jdbc:mysql://<数据库服务器IP>:3306/<数据库名>?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai ``` ### 优化查询语句 - **分析慢查询**:使用 MySQL 的慢查询日志功能,找出执行时间超过 10s 的查询语句,并对其进行优化。可以通过以下步骤开启慢查询日志: - 在 `my.cnf` 或 `my.ini` 中添加以下配置: ```ini [mysqld] slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-slow.log long_query_time = 1 ``` - 重启 MySQL 服务。 - 分析慢查询日志文件,找出执行时间长的查询语句,并添加合适的索引或优化查询逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值