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

Spring Boot连接XAMPP MySQL数据库失败,求可行解决方案

解决Spring Boot连接XAMPP MySQL通信链路失败问题

问题描述

使用Spring Boot和XAMPP中的MySQL开发待办事项列表时,出现数据库通信链路失败错误,核心报错信息:

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.

相关配置及错误日志如下:

错误日志

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:174) ~[mysql-connector-java-8.0.29.jar:8.0.29]
    org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.22.jar:5.3.22]

    
com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-java-8.0.29.jar:8.0.29]
    ... 52 common frames omitted

2022-07-29 10:22:46.429  WARN 7864 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
2022-07-29 10:22:46.430 ERROR 7864 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : 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.
2022-07-29 10:22:46.447 ERROR 7864 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
2022-07-29 10:22:46.449  WARN 7864 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution 

application.properties配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/todos_list?useSSL=false
server.error.whitelabel.enabled=false
spring.datasource.username=root
spring.datasource.password=
spring.jpa.show-sql = true
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>Todoslist</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Todoslist</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

解决方案

1. 确认XAMPP MySQL服务状态

  • 打开XAMPP控制面板,确保MySQL服务已启动,未启动则点击"Start"按钮启动。
  • 检查MySQL端口:点击MySQL模块的"Config" -> "my.ini",查找port配置项,确认端口为3306;若端口不同,同步修改application.properties中的端口号。

2. 更新数据库连接URL参数

MySQL 8.0+必须指定时区,否则会导致连接失败,修改spring.datasource.url为:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/todos_list?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
  • serverTimezone=UTC:解决MySQL 8.0后的时区不匹配问题
  • allowPublicKeyRetrieval=true:允许客户端从服务器获取公钥,避免连接权限拦截

3. 验证数据库账号权限

  • 打开XAMPP的PHPMyAdmin,用root账号(空密码)尝试登录,确认账号密码有效;若登录失败,重置MySQL密码后更新spring.datasource.password。
  • 确保root账号拥有todos_list数据库的读写权限,必要时在PHPMyAdmin中给root账号授予该数据库的全部权限。

4. 检查数据库是否存在

  • 在PHPMyAdmin中确认todos_list数据库已创建;若未创建,手动创建后再启动应用。
  • 若希望Spring Boot自动创建表结构,可添加JPA配置:
spring.jpa.hibernate.ddl-auto=update

5. 优化MySQL驱动依赖(可选)

Spring Boot 2.7.x推荐使用新版MySQL驱动,可将pom.xml中的依赖替换为:

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

原mysql-connector-java 8.0.29版本与Spring Boot 2.7.2兼容,此步骤为可选优化。

验证步骤

  1. 重启XAMPP MySQL服务
  2. 重新启动Spring Boot应用
  3. 查看控制台日志,确认数据库连接成功

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 13:15:30