使用一些来自互联网的指导,github的仓库,教程和诸如此类的东西,我试图使用react-spring-mysql堆栈运行一个应用程序。从我对mysql容器的理解来看,它拒绝了连接,但我不知道为什么。
这是我的application.properties:
spring.datasource.url=jdbc:mysql://${DB_HOSTNAME}:${DB_PORT}/${DB_DATABASE}?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto = create-drop
spring.datasource.max-wait=20000
spring.datasource.max-active=50
spring.datasource.max-idle=20
spring.datasource.min-idle=15
spring.datasource.max_allowed_packet=33554432
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-request-size=50MB
spring.servlet.multipart.max-file-size=50MB
字符串
这是spring项目根目录中的Dockerfile:
FROM maven:3.9-eclipse-temurin-17-alpine as build
WORKDIR /app
COPY pom.xml ./
COPY src src/
RUN mvn clean install -Dmaven.test.skip=true
# Run the app in a minimal java container
FROM eclipse-temurin:17-jre-alpine
VOLUME /tmp
EXPOSE 8080
ARG JAR_FILE=/app/target/*.jar
COPY --from=build ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
型
这是我的docker-compose:
services:
airclone_mysql:
image: mysql:8.2
container_name: airclone_mysql
restart: always
ports:
- 3307:3306
environment:
- MYSQL_ROOT_PASSWORD=airclone1
- MYSQL_USER=airclone
- MYSQL_PASSWORD=airclone1
- MYSQL_DATABASE=airclone
volumes:
- type: volume
source: mysql-data
target: /var/lib/mysql
networks:
- localnet
airclone_backend:
depends_on:
- airclone_mysql
container_name: airclone_backend
build:
context: ./backend
dockerfile: Dockerfile
environment:
- DB_HOSTNAME=airclone_mysql
- DB_PORT=3307
- DB_USERNAME=airclone
- DB_PASSWORD=airclone1
- DB_DATABASE=airclone
ports:
- "8080:8080"
networks:
- localnet
airclone_frontend:
depends_on:
- airclone_backend
container_name: airclone_frontend
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "5173:5173"
networks:
- localnet
volumes:
mysql-data:
networks:
localnet:
driver: bridge
型
我错过了什么?
容器日志中错误的最后一部分说:
2023-12-12 16:50:55 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
2023-12-12 16:50:55 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2023-12-12 16:50:55 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2023-12-12 16:50:55 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:62) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:150) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:166) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.NativeSession.connect(NativeSession.java:121) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:833) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 ... 156 common frames omitted
2023-12-12 16:50:55 Caused by: java.net.ConnectException: Connection refused
2023-12-12 16:50:55 at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
2023-12-12 16:50:55 at java.base/sun.nio.ch.Net.pollConnectNow(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/sun.nio.ch.NioSocketImpl.connect(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/java.net.SocksSocketImpl.connect(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at java.base/java.net.Socket.connect(Unknown Source) ~[na:na]
2023-12-12 16:50:55 at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-j-8.1.0.jar!/:8.1.0]
2023-12-12 16:50:55 ... 158 common frames omitted
型
1条答案
按热度按时间bpsygsoo1#
在Docker Compose中确保后端在MySQL之后启动
看起来你的后端服务器可能试图在MySQL服务完全启动和运行之前启动。为了解决这个问题并确保你的后端只在MySQL准备好之后才启动,你可以在Docker Compose文件中利用
healthcheck
指令。下面是一个示例Docker Compose文件,演示了如何对MySQL使用健康检查,并确保后端服务仅在MySQL健康时启动:
字符串
在此配置中:
healthcheck
指令用于MySQL服务。它定期检查MySQL服务器是否响应。backend
服务有一个depends_on
指令,指定它依赖于mysql
服务。这确保了在MySQL服务健康之前,后端服务不会启动。根据特定的MySQL配置调整MySQL服务的
environment
部分中的值。通过合并这些健康检查,您可以缓解与MySQL服务准备就绪之前启动后端相关的问题,确保更可靠的设置。
我希望这对你有帮助!如果你有进一步的问题或遇到任何问题,请随时询问。