You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.7.14无法连接MySQL 8.0.3问题求助

问题描述

已尝试其他帖子提及的所有解决方法,但仍无法解决MySQL连接失败问题,以下是详细信息:

环境版本

  • Spring Boot版本:2.7.14
  • Java版本:11
  • MySQL版本:8.0.3

pom.xml 依赖片段

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

application.properties 配置

spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

错误日志

2023-07-12 22:47:56.845 ERROR 15012 --- [  restartedMain] 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.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:175) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:446) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:239) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:188) ~[mysql-connector-j-8.0.33.jar:8.0.33]
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar:na]

排查与解决步骤

  1. 确认MySQL服务状态
    Windows执行net start mysql,Linux执行systemctl status mysqld,确保MySQL服务正在运行。未启动则先启动服务再测试连接。

  2. 修正Hibernate方言
    MySQL 8.x版本需对应使用MySQL8方言,将配置中的spring.jpa.properties.hibernate.dialect改为org.hibernate.dialect.MySQL8Dialect,避免版本不兼容问题。

  3. 匹配JDBC驱动版本
    当前驱动版本8.0.33与MySQL 8.0.3差距过大,存在兼容性风险。在pom.xml中指定与MySQL版本一致的驱动:

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.3</version>
        <scope>runtime</scope>
    </dependency>
    
  4. 验证连接参数有效性

    • 手动创建demo数据库,确保数据库存在;
    • 用Navicat/MySQL Workbench测试root/root账号能否连接localhost:3306,排除账号密码或权限问题;
    • 简化JDBC URL,改为jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useSSL=false,减少冗余参数冲突。
  5. 检查端口与防火墙
    Windows执行netstat -ano | findstr :3306,Linux执行lsof -i :3306,确认3306端口未被占用。同时关闭本地防火墙或给MySQL端口放行。

内容的提问来源于stack exchange,提问作者harry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 11:49:56