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

Docker Compose中Spring Boot应用无法连接MySQL问题排查

Spring Boot容器化后无法连接MySQL问题排查

我的配置

docker-compose.yml

version: '3.9'

services:
  book_service_db:
    image: mysql:8.0
    command: --default-authentication-plugin=caching_sha2_password
    environment:
      MYSQL_ROOT_PASSWORD: r0dRig$o!
      MYSQL_DATABASE: book_service
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    ports:
      - "3306:3306"
    networks:
      app_network:
        ipv4_address: 172.20.0.2
    container_name: book_service_db
    restart: always
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      retries: 10
      start_period: 60s  # Ensures MySQL is fully started before dependent services try to connect

  book-service:
    build:
      context: ./../book-service
      dockerfile: Dockerfile
    ports:
      - "8081:8080"
    networks:
      app_network:
        ipv4_address: 172.20.0.5
    depends_on:
      book_service_db:
        condition: service_healthy  # Ensures book-service starts only when MySQL is ready
    container_name: book_service
    restart: always

networks:
  app_network:
    ipam:
      config:
        - subnet: 172.20.0.0/24
    driver: bridge

book-service的Dockerfile

FROM openjdk:17-jdk-slim

WORKDIR /app

COPY /target/*.jar /app/app.jar

EXPOSE 8080

RUN apt-get update && apt-get install -y iputils-ping net-tools dnsutils default-mysql-client

ENTRYPOINT ["java", "-jar", "app.jar"]

application.properties

# Application details
spring.application.name=Book-Service
server.port=8082

# MySQL Configuration
spring.datasource.url=jdbc:mysql://book_service_db:3306/book_service?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.datasource.username=admin
spring.datasource.password=admin

# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
spring.sql.init.data-locations=classpath:data.sql

问题现象

执行docker-compose up时,应用无法连接数据库,报错如下:

2025-03-16 15:07:07 Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
.
.
.
2025-03-16 15:07:07 Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
.
.
.
2025-03-16 15:07:07 Caused by: org.hibernate.HibernateException: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)

已尝试的操作

  • 从应用容器成功ping通数据库:
    ping book_service_db
    
  • 从容器内部手动连接数据库成功,证实凭据正确:
    mysql -h book_service_db -u admin -p
    
  • 尝试修改MySQL命令使用不同认证插件,均无效:
    command: --default-authentication-plugin=mysql_native_password --bind-address=0.0.0.0
    command: --default-authentication-plugin=mysql_native_password
    command: --default-authentication-plugin=caching_sha2_password
    

疑问

为何Spring Boot应用在Docker中无法连接MySQL?数据库可从容器访问,但Hibernate抛出方言错误。是数据库配置或健康检查时机有问题吗?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 20:53:14