Running docker-compose with MySQL in SpringBoot: Database not connected to Spring application
P粉323224129
P粉323224129 2023-08-30 12:33:00
0
1
582
<p>我在 SpringBoot 中有一个微服务(产品服务),在 MySQL 中有一个数据库,并且希望在 Docker 中运行我的应用程序</p> <p>我可以在本地运行该服务,没有任何错误。但是当我启动 Docker (docker-compose up --build) 时,它显示了一些连接错误。</p> <p>我在 application.properties、Dockerfile、docker-compose、pom.xml 和错误中显示了我的代码。经过一番修改和询问朋友,错误依然存在。如果有人能帮助我,我将不胜感激:)</p> <p>在 MySQL Workbench 中,我设置了一个与 URL <code>localhost:3306</code> 的连接,并在其中创建了一个 <code>testdb</code> 模式。</p> <p>这是我的<code>application.properties</code></p> <pre class="brush:php;toolbar:false;">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</pre> <p>这是 Dockerfile</p> <pre class="brush:php;toolbar:false;">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 &amp;&amp; (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 [&quot;java&quot;,&quot;-cp&quot;,&quot;app:app/lib/*&quot;,&quot;com.example.helloworld.EcommerceApplication&quot;] EXPOSE 8080</pre> <p>这是 docker-compose.yml</p> <pre class="lang-yaml prettyprint-override"><code>version: &quot;3.9&quot; 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: # - &quot;3306:3307&quot; environment: MYSQL_DATABASE: testdb MYSQL_ROOT_USER: abc MYSQL_ROOT_PASSWORD: def networks: - internal networks: internal: name: internal </code></pre> <p>这是我在 pom.xml 中的依赖项</p> <pre class="lang-xml prettyprint-override"><code> <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> </code></pre> <p>我总结了这些代码中的错误</p> <p><code>产品服务 |引起原因:com.mysql.cj.exceptions.CJCommunicationsException:通信链路故障</code></p> <pre class="brush:php;toolbar:false;">`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. `</pre> <p>产品服务 | ...省略52个常见框架</p>
P粉323224129
P粉323224129

reply all(1)
P粉722409996

In the docker-compose file, your database container is named database and the same name should be used as the hostname when accessing this container in the network

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

instead of

spring.datasource.url= jdbc:mysql://localhost:3306/testdb
and 
SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/testdb
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template