,

SpringBoot 启动报错相关

com.mysql.cj.jdbc.exceptions.CommunicationsException: C…

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

一、高频排查顺序(从易到难)

1. MySQL 服务是否启动

  • Windows:服务里查看 MySQL80/MySQL 是否正在运行
  • Linux:systemctl status mysqld / service mysql status,没启动则 start

2. 连接地址、端口写错(最常见)

properties

# 错误示例
url=jdbc:mysql://127.0.0.1:3307/dbname
# 默认端口3306,自定义才改端口
url=jdbc:mysql://127.0.0.1:3306/dbname?useSSL=false&serverTimezone=Asia/Shanghai
  • 远程连接:不能写localhost/127.0.0.1,改用 MySQL 服务器公网 IP

3. 防火墙 / 安全组拦截端口 3306

  • 本机连本地:关闭电脑防火墙测试
  • 云服务器 (阿里云 / 华为云):安全组放行入方向 3306 端口
  • Linux 防火墙:firewall-cmd --add-port=3306/tcp --permanent && firewall-cmd --reload

4. MySQL 不允许远程访问(远程连接必查)

登录 MySQL 执行:

sql

-- 查看用户访问权限
select host,user from mysql.user;
-- % 代表允许任意IP访问,localhost只能本机
update mysql.user set host='%' where user='root';
flush privileges;

5. 时区 & 驱动参数缺失(cj 包必备)

com.mysql.cj 新版驱动必须加参数:

plaintext

?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true

缺少 serverTimezone 也会偶发通信失败。

6. MySQL 绑定 IP 限制(bind-address)

打开my.cnf/my.ini

ini

# 只允许本机
bind-address=127.0.0.1
# 修改为允许所有IP
bind-address=0.0.0.0

改完重启 MySQL。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About the Author

每个人都有自己得时区,在自己得时区里,一切都是准时的。

BlockSpare — News, Magazine and Blog Addons for (Gutenberg) Block Editor