泊坞窗撰写:春季启动到mysql数据库的连接被拒绝

时间:2019-06-25 12:14:26

标签: mysql spring-boot docker docker-compose

我是Docker的新手,我需要在docker工具箱上使用docker-compose启动Angular / SpringBoot / MySQL项目。我将docker yml文件复制到了使用相同技术的项目中,并更改了其中的路径以匹配我的项目。但是,当我尝试docker-compose时,会引发以下错误:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
[...] 84 common frames omitted
Caused by: java.net.ConnectException: Connection refused (Connection refused)

docker-compose.yml如下:

version: '3'

services:
  database:
    image: mysql
    container_name: database
    environment:
      MYSQL_ROOT_PASSWORD: ****
      MYSQL_DATABASE: db_example
      MYSQL_USER: springuser
      MYSQL_PASSWORD: ****
    ports:
      - 3306:3306
    volumes:
      - db_exampleData:/var/lib/mysql

  springapi:
    image: openjdk:10-jre-slim
    container_name: springapi
    ports:
      - 8443:8443
    depends_on:
      - database
    volumes:
      - ./backend/target/backend-0.1.0.jar:/application.jar
    command: ["java", "-jar", "application.jar"]

  angular:
    image: nginx:alpine
    container_name: angular
    ports:
      - 4200:80
    depends_on:
      - springapi
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./frontend/my-app/dist/my-app:/usr/share/nginx/html

volumes:
  db_exampleData:

application.properties:

spring.jpa.hibernate.ddl-auto=none
spring.jpa.open-in-view=false
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=****

server.port=8443

任何建议都会有所帮助!

1 个答案:

答案 0 :(得分:2)

您需要像这样更改连接:

jdbc:mysql://database:3306/db_example

并将其添加到您在springapi服务下的docker-compose中:

links:
   - database

另一方面,您可以使用wait-for-it.sh通过在springapi服务下添加命令部分来检查数据库是否已启动:

command: ["path/to/wait-for-it.sh", "database:3306", "-t", "6000", "--", "YOUR ACTUAL COMMAND"]