Communications link failure, The last packet sent successfully to the server was 0 milliseconds ago.

文章讲述了在SpringBoot应用中遇到HikariPool初始化异常,原因是由于使用服务名称mysql无法在Windows环境下与MySQL服务器建立连接。解决方案是将数据源URL中的mysql改为localhost,并添加serverTimezone=UTC参数,以适应Windows环境。此问题表明不同环境下的服务发现和配置需要具体问题具体分析。
摘要由CSDN通过智能技术生成

1. 问题描述

ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
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.

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

2. Spring Boot yml 配置

server:
  port: 8089
spring:
  datasource:
    url: jdbc:mysql://mysql:3306/test?useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true

3. 解决方法

将 MySQL 配置 url: jdbc:mysql://mysql:3306/test?useSSL=false 修改为 url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC,即:将其中的 mysql 修改为 localhost

出错原因:
运行环境为 Windows,所以通过服务名称 mysql 无法与 MySQL 通信并建立连接(Docker 中该方式可正常运行),修改为 localhost 后可正常运行。

4. 小结

出现与 MySQL 连接失败的原因有多种,通过尝试网上其他方法并未解决问题。通过排查,最终发现是无法通过 mysql 识别和获取 MySQL 数据库的地址而导致连接失败(该方式适用于 Docker 而不适用于 Windows)。虽然并不是什么“高深”的问题,半天没找到“症结”还是挺烦人😂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值