在 SpringBoot 中使用 MySQL 运行 docker-compose:数据库未连接到 Spring 应用程序
P粉323224129
P粉323224129 2023-08-30 12:33:00
[MySQL讨论组]

我在 SpringBoot 中有一个微服务(产品服务),在 MySQL 中有一个数据库,并且希望在 Docker 中运行我的应用程序

我可以在本地运行该服务,没有任何错误。但是当我启动 Docker (docker-compose up --build) 时,它显示了一些连接错误。

我在 application.properties、Dockerfile、docker-compose、pom.xml 和错误中显示了我的代码。经过一番修改和询问朋友,错误依然存在。如果有人能帮助我,我将不胜感激:)

在 MySQL Workbench 中,我设置了一个与 URL localhost:3306 的连接,并在其中创建了一个 testdb 模式。

这是我的application.properties

spring.datasource.url= jdbc:mysql://localhost:3306/testdb
spring.datasource.username= abc
spring.datasource.password= def
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect

#Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update

这是 Dockerfile

FROM maven:3.8.3-openjdk-17 AS build
WORKDIR /app

COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
COPY src src

RUN mvn install -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

FROM openjdk:17-jdk-slim
VOLUME /tmp
ARG DEPENDENCY=/app/target/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.helloworld.EcommerceApplication"]
EXPOSE 8080

这是 docker-compose.yml

version: "3.9"
services:
  product-service:
    container_name: product-service
    build:
      context: .
      dockerfile: Dockerfile
    links:
      - database
    depends_on:
      - database
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/testdb
      #- SPRING_DATASOURCE_USERNAME=abc
      #- SPRING_DATASOURCE_PASSWORD=def
      - SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.MySQL5InnoDBDialect
      - SPRING_JPA_HIBERNATE_DDL-AUTO=update
      - SPRING_JPA_SHOW-SQL= true
    ports:
      - 8080:8080
    networks:
      - internal

  database:
    platform: linux/x86_64
    image: mysql:8.0
    container_name: mysql
    #ports:
    #  - "3306:3307"
    environment:
      MYSQL_DATABASE: testdb
      MYSQL_ROOT_USER: abc
      MYSQL_ROOT_PASSWORD: def 

    networks:
      - internal

networks:
  internal:
    name: internal

这是我在 pom.xml 中的依赖项

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>6.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.25.0-GA</version>
        </dependency>
    </dependencies>

我总结了这些代码中的错误

产品服务 |引起原因:com.mysql.cj.exceptions.CJCommunicationsException:通信链路故障

`product-service    | Caused by: java.net.ConnectException: Connection refused`

`product-service    | 2023-03-14 08:45:26.585  WARN 1 --- [           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/a
utoconfigure/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`

`product-service    | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/b
oot/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`

`product-service    | Caused by: 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
`

`product-service    | Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
`

`product-service    | 2023-03-14 08:50:17.188 ERROR 1 --- [           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
`

`product-service    | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
`

产品服务 | ...省略52个常见框架

P粉323224129
P粉323224129

全部回复(1)
P粉722409996

在 docker-compose 文件中,您的数据库容器被命名为database,并且在网络中访问此容器时应使用相同的名称作为主机名

spring.datasource.url= jdbc:mysql://database:3306/testdb
and
SPRING_DATASOURCE_URL=jdbc:mysql://database:3306/testdb

而不是

spring.datasource.url= jdbc:mysql://localhost:3306/testdb
and 
SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/testdb
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号