gpt4 book ai didi

java - 无法使用 Docker compose 将 Spring Boot 连接到 MySql

转载 作者:行者123 更新时间:2023-12-01 18:16:37 26 4
gpt4 key购买 nike

我创建了一个 docker compose 文件,用于将 MySql 连接到 SpringBoot 应用程序。但我收到此错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_242]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_242]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.46.jar!/:5.1.46]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353) ~[HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562) [HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.1.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)

我的 Dockerfile,我使用 Maven 安装创建了一个 .jar 文件:

FROM openjdk:8
EXPOSE 8083
ADD target/ProjectCrud.jar ProjectCrud.jar
ENTRYPOINT ["java", "-jar", "ProjectCrud.jar"]

docker-compose.yml:

version: '3'
services:
docker-mysql:
restart: always
container_name: docker-mysql
image: mysql
environment:
MYSQL_DATABASE: pfa
MYSQL_ROOT_PASSWORD: aze123qsd
MYSQL_USER: root
MYSQL_ROOT_HOST: '%'
volumes:
- ./sql:/docker-entrypoint-initdb.d
networks:
- spring-db

ports:
- "3306:3306"
healthcheck:
test: "/usr/bin/mysql --user=root --password=root--execute \"SHOW DATABASES;\""
interval: 2s
timeout: 20s
retries: 10


sign-up-app:
build: ./
expose:
- "10222"
ports:
- 10222:10222
environment:
WAIT_HOSTS: mysql:3306
depends_on:
- docker-mysql
networks:
- spring-db
networks:
spring-db:

我的应用程序属性:

    spring.datasource.username=root
spring.datasource.password=aze123qsd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://docker-mysql:3306/pfa?
useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.port=8089

我尝试使用 docker 默认 ip 在本地计算机中运行我的 spring boot 应用程序,同时在此 docker-compose.yml 文件中仅运行 MySql 容器,并且它运行良好。但是当我尝试 docker 撰写文件时。我收到此错误

最佳答案

这可能是因为 contextLoads() 测试失败,导致 Jar 的构建失败。我遇到了类似的问题。您可以引用this

这可能是因为测试在本地运行,并且无法将 docker-mysql 解析为引用。它只能被同一网络中的容器引用。您可以修复此覆盖测试属性,并使它们使用本地主机的数据库或内存数据库。

尝试在测试类上使用它:@SpringBootTest(properties = {"spring.datasource.url=jdbc:mysql://localhost:3306/dbname"})

关于java - 无法使用 Docker compose 将 Spring Boot 连接到 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com