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.ymlcuts off atinitiali...—make sure this line is completed (e.g.,initialSize: 5for connection pool initial size). Truncated YAML will break configuration parsing entirely. - Generic Hibernate dialect:
org.hibernate.dialect.Dialectis too vague. Use a dialect matching your MySQL version:- MySQL 8+:
org.hibernate.dialect.MySQL8Dialect - MySQL 5.x:
org.hibernate.dialect.MySQL5InnoDBDialect
- MySQL 8+:
- Outdated JDBC driver:
com.mysql.jdbc.Driveris deprecated for MySQL 8+. Switch tocom.mysql.cj.jdbc.Driverinstead.
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,
localhostrefers to the container itself, not your host. Replacelocalhostwith:host.docker.internal(Docker Desktop for Windows/macOS)- Your host’s internal IP address (e.g.,
192.168.1.100on Linux)
- If MySQL is in another Docker container: Make sure both containers are on the same Docker network. Create a network first:
Run your MySQL container attached to this network:docker network create app-network
Then update your datasource URL to use the MySQL container’s name as the hostname: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.0jdbc: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
paidatabase exists and the user has permissions:
Ensure thedocker exec -it mysql-container mysql -u root -p # Run these commands inside MySQL: SHOW DATABASES; SELECT user, host FROM mysql.user WHERE user = 'pai';paiuser 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

