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

Spring Boot Docker环境下无法创建连接池问题排查求助

Hey there! Let's work through this connection pool issue you're hitting when running your Spring Boot app in Docker. I've dealt with similar problems before, so let's break down the most likely causes and fixes step by step.

1. Fix Configuration Conflicts & Incomplete Settings

You’ve got both application.properties and application.yml in your project, which can lead to unexpected behavior since Spring Boot loads these files in a specific priority order. Plus, there are a couple of red flags in your config:

  • Duplicate datasource settings: Both files define the same MySQL URL, username, and password—pick one file to keep all your configs (YAML is more readable for nested settings) to avoid conflicts.
  • Incomplete YAML: Your application.yml cuts off at initiali...—make sure this line is completed (e.g., initialSize: 5 for connection pool initial size). Truncated YAML will break configuration parsing entirely.
  • Generic Hibernate dialect: org.hibernate.dialect.Dialect is too vague. Use a dialect matching your MySQL version:
    • MySQL 8+: org.hibernate.dialect.MySQL8Dialect
    • MySQL 5.x: org.hibernate.dialect.MySQL5InnoDBDialect
  • Outdated JDBC driver: com.mysql.jdbc.Driver is deprecated for MySQL 8+. Switch to com.mysql.cj.jdbc.Driver instead.

Here’s a cleaned-up example application.yml to use:

spring:
  datasource:
    url: jdbc:mysql://<mysql-host>:3306/pai?useSSL=false&serverTimezone=UTC
    driverClassName: com.mysql.cj.jdbc.Driver
    username: pai
    password: kasztan
    hikari:
      maximum-pool-size: 10
      connection-timeout: 30000
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect

2. Resolve Docker Network Accessibility

This is the most common culprit for connection issues in Docker:

  • If MySQL is on your host machine: Inside a Docker container, localhost refers to the container itself, not your host. Replace localhost with:
    • host.docker.internal (Docker Desktop for Windows/macOS)
    • Your host’s internal IP address (e.g., 192.168.1.100 on Linux)
  • If MySQL is in another Docker container: Make sure both containers are on the same Docker network. Create a network first:
    docker network create app-network
    
    Run your MySQL container attached to this network:
    docker run --name mysql-container --network app-network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=pai -e MYSQL_USER=pai -e MYSQL_PASSWORD=kasztan -d mysql:8.0
    
    Then update your datasource URL to use the MySQL container’s name as the hostname: jdbc:mysql://mysql-container:3306/pai

3. Verify MySQL is Accessible

Double-check that your MySQL instance is reachable and credentials are valid:

  • Test the connection from your host machine using the MySQL client:
    mysql -h <mysql-host> -u pai -p kasztan -D pai
    
  • If MySQL is in a container, exec into it to verify the pai database exists and the user has permissions:
    docker exec -it mysql-container mysql -u root -p
    # Run these commands inside MySQL:
    SHOW DATABASES;
    SELECT user, host FROM mysql.user WHERE user = 'pai';
    
    Ensure the pai user is allowed to connect from your Spring Boot container’s IP or % (any host, for development only).

4. Check Container Logs for Exact Errors

Always look at your Spring Boot container logs to get the precise error—this will narrow down the issue instantly:

docker logs <your-spring-boot-container-name>

Look for exceptions like:

  • CannotCreatePoolingConnectionException: Connection pool can’t initialize (likely network/credential issue)
  • SQLTransientConnectionException: Temporary connection failure (check MySQL status)
  • AccessDeniedException: Invalid username/password or missing permissions

5. Validate Gradle Dependencies

Make sure your build.gradle has the correct MySQL driver dependency (for MySQL 8+):

plugins {
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.mysql:mysql-connector-j:8.0.33' // Updated driver group/artifact
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Try these steps one by one—start with checking the logs and network setup, since those are the most frequent fixes. If you hit a specific error message, we can dive deeper into that!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:35:44