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

Spring Boot+Gradle项目数据库连接报错:Could not obtain connection to query metadata

数据库连接错误 "Could not obtain connection to query metadata" 排查与修复

问题背景

使用Spring Boot 2.7.3、Gradle、Lombok开发Java 17应用,配置MySQL数据库连接时启动报错:Could not obtain connection to query metadata。相关配置如下:

1. resources/db.migration 建表SQL

CREATE TABLE users
(
    id        int                                NOT NULL AUTO_INCREMENT,
    role      enum('CUSTOMER','TRAINER','ADMINISTRATION') NOT NULL,
    email     varchar(45)                        NOT NULL,
    password  varchar(200)                       NOT NULL,
    firstName varchar(45)                        NOT NULL,
    lastName  varchar(45)                        NOT NULL,
    PRIMARY KEY (id),
    UNIQUE(email)
);

2. application.properties 配置

spring.datasource.url=jdbc:mysql://localhost:3306/hiddenName
spring.datasource.username=hiddenUsername
spring.datasource.password=hiddenPassword

3. build.gradle 依赖配置

plugins {
    id 'org.springframework.boot' version '2.7.3'
    id 'io.spring.dependency-management' version '1.0.13.RELEASE'
    id 'java'
}

group = 'HiddenName'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'ch.qos.logback:logback-classic:1.2.11'
    testImplementation 'org.projectlombok:lombok:1.18.22'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.assertj:assertj-core:3.20.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.assertj:assertj-core:3.20.2'

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    implementation 'mysql:mysql-connector-java:8.0.30'
    implementation 'ch.qos.logback:logback-classic:1.2.11'
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.10'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'com.h2database:h2'
}

test {
    useJUnitPlatform()
}

问题排查与修复步骤

1. 验证基础数据库连接

首先排除最基础的连接问题:

  • 确认MySQL服务正在本地3306端口运行,可通过命令行测试连接:
    mysql -h localhost -P 3306 -u hiddenUsername -p
    
    输入密码后,若能成功登录,再检查hiddenName数据库是否存在,不存在则执行:
    CREATE DATABASE hiddenName;
    
  • 若连接失败,检查账号密码是否正确、数据库权限是否开放(比如本地账号是否允许localhost连接)。

2. 修正数据库连接配置

MySQL 8.0+需要指定时区、SSL等参数,更新application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/hiddenName?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=hiddenUsername
spring.datasource.password=hiddenPassword
# 显式指定MySQL 8.0驱动类,避免自动适配问题
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 禁用JPA自动建表,避免与Flyway迁移冲突
spring.jpa.hibernate.ddl-auto=none
# 可选:开启SQL日志便于排查
spring.jpa.show-sql=true

3. 清理Gradle重复依赖

原build.gradle存在大量重复依赖(如spring-boot-starter-web、lombok、junit等),易导致类冲突,整理后的依赖如下:

plugins {
    id 'org.springframework.boot' version '2.7.3'
    id 'io.spring.dependency-management' version '1.0.13.RELEASE'
    id 'java'
}

group = 'HiddenName'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // 核心Web、JPA、Validation依赖
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    // MySQL驱动
    implementation 'mysql:mysql-connector-java:8.0.30'

    // Lombok支持
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    // 日志框架
    implementation 'ch.qos.logback:logback-classic:1.2.11'

    // OpenAPI文档支持
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.10'

    // 测试依赖
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    testImplementation 'org.assertj:assertj-core:3.20.2'
    testRuntimeOnly 'com.h2database:h2'

    // 添加Flyway数据库迁移依赖(因存在db.migration目录)
    implementation 'org.springframework.boot:spring-boot-starter-flyway'
}

test {
    useJUnitPlatform()
}

关键调整:

  • 移除所有重复依赖,避免版本冲突
  • 将Lombok依赖移至compileOnly和annotationProcessor(主代码也需使用Lombok)
  • 添加spring-boot-starter-flyway依赖,匹配db.migration目录的迁移需求

4. 配置Flyway(可选)

若首次使用Flyway且数据库已有表,需添加以下配置到application.properties,避免基线检查报错:

spring.flyway.baseline-on-migrate=true

验证修复

  1. 执行Gradle清理编译:
    ./gradlew clean build
    
  2. 启动MySQL服务,确认hiddenName数据库存在
  3. 启动Spring Boot应用,查看日志:
    • 应出现Successfully connected to database相关日志
    • Flyway会自动执行db.migration目录下的建表SQL

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 13:58:13