[英]Spring Boot + docker-compose + MySQL: Connection refused
I'm trying to set up a Spring Boot application that depends on a MySQL database called teste in docker-compose.我正在尝试设置一个 Spring Boot 应用程序,该应用程序依赖于 docker-compose 中名为teste的 MySQL 数据库。 After issuing docker-compose up
, I'm getting:发出docker-compose up
,我得到:
Caused by: java.net.ConnectException: Connection refused (Connection refused)
I'm running on Linux Mint, my docker-compose version is 1.23.2, my Docker version is 18.09.0.我在 Linux Mint 上运行,我的 docker-compose 版本是 1.23.2,我的 Docker 版本是 18.09.0。
application.properties应用程序属性
# JPA PROPS
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.datasource.url=jdbc:mysql://db:3306/teste?useSSL=false&serverTimezone=UTC
spring.datasource.username=rafael
spring.datasource.password=password
spring.database.driverClassName =com.mysql.cj.jdbc.Driver
docker-compose.yml docker-compose.yml
version: '3.5'
services:
db:
image: mysql:latest
environment:
- MYSQL_ROOT_PASSWORD=rootpass
- MYSQL_DATABASE=teste
- MYSQL_USER=rafael
- MYSQL_PASSWORD=password
ports:
- 3306:3306
web:
image: spring-mysql
depends_on:
- db
links:
- db
ports:
- 8080:8080
environment:
- DATABASE_HOST=db
- DATABASE_USER=rafael
- DATABASE_NAME=teste
- DATABASE_PORT=3306
and the Dockerfile和 Dockerfile
FROM openjdk:8
ADD target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Docker compose always starts and stops containers in dependency order, or sequential order in the file if not given. Docker compose始终以依赖关系顺序或文件中的顺序顺序(如果未给出)启动和停止容器。 But docker-compose do not guarantee that it will wait till the dependency container is running. 但是docker-compose不能保证它会一直等到依赖容器运行之后。 You can refer here for further details. 您可以在此处参考以获取更多详细信息。 So the Problem here is that your database is not ready when your spring-mysql
container try to access database. 所以这里的问题是当您的spring-mysql
容器尝试访问数据库时,数据库尚未准备就绪。 So the recommended solution is you could use wait-for-it.sh or similar script to wrap your spring-mysql
app starting ENTRYPOINT
. 因此,推荐的解决方案是您可以使用wait-for-it.sh或类似的脚本来包装spring-mysql
应用程序,以ENTRYPOINT
。
As example if you use wait-for-it.sh
your ENTRYPOINT
in your Dockerfile should change to following after copying above script to your project root: 例如,如果您使用wait-for-it.sh
,则在将上述脚本复制到项目根目录后, ENTRYPOINT
中的ENTRYPOINT应该更改为以下内容:
ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-jar", "app.jar"]
And two other important thing to consider here is: 这里还要考虑的另外两个重要事项是:
Your config looks nice, I would just recommend: 您的配置看起来不错,我只建议您:
links: db
. 删除links: db
。 It has no value in user-defined bridge
networking 在user-defined bridge
网络中没有任何价值 docker-compose
- all ports are exposed automatically inside user-defined bridge
network. 除非您要从docker-compose
外部进行连接-否则所有端口都将自动暴露在user-defined bridge
网络内部,除非为db移除端口暴露给db。 I think the problem is that database container takes more time to start than web. 我认为问题在于数据库容器比网络启动需要更多的时间。 depends_on
just controls the order, but does not guarantee you database readiness. depends_on
仅控制顺序,但不能保证您已准备好数据库。 If possible, set several connection attempts or put socket-wait procedure in your web container. 如果可能,请在Web容器中设置几次连接尝试或进行套接字等待过程。
in my case, after days of retried, i found that change the version of the mysql image fix this question.就我而言,经过几天的重试,我发现更改 mysql 映像的版本可以解决此问题。 i was used image: mysql:5.7 at first, and got connection refused error.我最初使用 image: mysql:5.7,并得到连接拒绝错误。 then i tried image: mysql:latest, got another syntax error.然后我尝试了图像:mysql:latest,又出现了另一个语法错误。 at the last i tried image: mysql:8, fixed!!!最后我尝试了图像:mysql:8,已修复!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.