docker compose:spring boot 连接到 mysql 数据库被拒绝

docker compose: spring boot connection to mysql database refused(docker compose:spring boot 连接到 mysql 数据库被拒绝)
本文介绍了docker compose:spring boot 连接到 mysql 数据库被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm pretty new to Docker and I need to startup my Angular/SpringBoot/MySQL project with docker-compose on the docker toolbox. I copied a docker yml file into my project which used the same technologies and changed the paths inside of it to match my project. However when I try docker-compose, it throws the following error:

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 看起来像:

the docker-compose.yml looks like:

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:

the 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

任何建议都会有所帮助!

any suggestion would be helpful!

推荐答案

你需要像这样改变你的连接:

you need to change your connecion like this:

jdbc:mysql://database:3306/db_example

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

and add this to your docker-compose under springapi service:

links:
   - database

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

on the other hand you may use wait-for-it.sh to check if DB is up by add a command section under springapi service:

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

这篇关于docker compose:spring boot 连接到 mysql 数据库被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)