简体   繁体   English

Spring Boot + docker-compose + MySQL:连接被拒绝

[英]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: 这里还要考虑的另外两个重要事项是:

  • Do not use links they are deprecated you should use user-defined network instead. 不要使用不推荐使用的链接,而应使用用户定义的网络。 All services in docker-compose file will be in single user-defined network if you don't explicitly define any network. 如果您未明确定义任何网络,则docker-compose文件中的所有服务都将位于单个用户定义的网络中。 So you just have to remove the links from compose file. 因此,您只需要从撰写文件中删除链接。
  • You don't need to publish the port for docker container if you only use it inside the user-defined network. 如果仅在用户定义的网络内使用Docker容器,则无需发布该端口。

Your config looks nice, I would just recommend: 您的配置看起来不错,我只建议您:

  • Remove links: db . 删除links: db It has no value in user-defined bridge networking user-defined bridge网络中没有任何价值
  • Remove port exposing for db unless you want to connect from outside 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.

相关问题 使用docker-compose启动dockerized Spring Boot和MariaDb时出现连接拒绝错误 - Connection refused error when starting dockerized Spring Boot and MariaDb with docker-compose 如何使用 Docker-compose 在 Docker 上连接 Spring Boot 和 MySQL? - How to connect between Spring Boot and MySQL on Docker with Docker-compose? Docker 组成 Redis 和 Spring 启动应用程序:java.net.ConnectException:连接被拒绝(连接被拒绝)“, - Docker compose Redis and Spring boot app: java.net.ConnectException: Connection refused (Connection refused)", Spring Boot docker无法连接到mysql(连接被拒绝/ createCommunicationsException) - Spring boot docker Cannot connect to mysql (Connection refused / createCommunicationsException) 用于本地 Spring Boot 开发的 Docker-Compose - Docker-Compose for local Spring Boot Development Spring Boot和Docker-compose设置参数 - Spring Boot and Docker-compose setting arguments 可怕的 Java SpringBoot 应用程序未通过 Docker-compose java.net.ConnectException 连接到 MySQL:连接被拒绝 - The dreaded Java SpringBoot app not connecting to MySQL with Docker-compose java.net.ConnectException: Connection refused Docker-compose 容器连接到 MySql 不起作用 - Docker-compose container connection to MySql not working Docker-组成up springboot + postgres连接被拒绝 - Docker-compose up springboot + postgres Connection refused 如何在docker-compose中修复Jedis中的拒绝连接错误? - How to fix connection refused error in Jedis in docker-compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM