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)。虽然并不是什么“高深”的问题,半天没找到“症结”还是挺烦人😂。