如何在Docker Compose中初始化MySql数据库 [英] How to init MySql Database in Docker Compose

查看:408
本文介绍了如何在Docker Compose中初始化MySql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我在Spring中开发了一个使用mysql 8数据库的微服务.必须初始化该数据库(创建数据库,一些表和数据).在主机上,我使用data.sql和schema.sql脚本初始化了数据库.问题是,我必须设置:

Scenario: I developed a microservice in Spring which uses a mysql 8 database. This db has to be initalized (create a Database, some tables and data). On my host machine I initialized the database with data.sql and schema.sql script. The Problem is, that I have to set:

spring.datasource.initialization-mode=always

第一次执行.这将以我想要的方式初始化我的数据库.为了以后的运行,我必须注释该命令.非常丑陋的解决方案,但是我找不到更好的解决方案,并且我现在没有对此

for the first execute. This initializes my db the way I want to. For future runs I have to comment this command. Very ugly soltion but I could not find a better one and I got no reponse right now to this question. I thought for testing it is ok but I definetly have to improve that. Currently I want to run my service with docker by a docker compose.

预期:这是docker-compose文件.相当简单.在docker世界中,我是一个新手,所以我想一步一步来.

Expected: This is the docker-compose file. Fairly simple. I'm totally new in the world of docker and so I want to go on step by step.

version: '3' 
services:usermanagement-service:
build:
  ./UserManagementService
restart:
  on-failure
ports:
  - "7778:7778"
links:
  - mysqldb
depends_on:
  - mysqldb   mysqldb:
build:
  ./CustomMySql
volumes:
  - ./mysql-data:/var/lib/mysql
restart:
  on-failure
environment:
  MYSQL_ROOT_PASSWORD: root
  MYSQL_DATABASE: userdb
  MYSQL_USER: testuser
  MYSQL_PASSWORD: testuser
expose:
  - "3600"

我期望我的数据库是由用户初始化的,而在第一次运行时,我的微服务会使用数据初始化db.因此,在下次开始撰写之前,我必须注释该命令并重建图像.(我知道,很丑)

I was expecting, that my database gets initialized with a user and that in the first run my microservice initializes the db with data. So before the next start of compose I have to comment the command and rebuild the image. (I know , ugly)

问题:因此,除了这个难看的解决方案之外,我还遇到了运行时问题.在 docker-compose up 上,我的微服务比数据库的初始化速度快.因此,它尝试调用db导致en错误.由于失败时重新启动,微服务再次出现.现在它可以工作了,因为数据库的初始化已经完成.

Problem: So besides this ugly solution I run into runtime problems. On docker-compose up my Microservice is faster than the init of the database. So it tries to call the db what results in en error. Because of the restart on failure the microservice comes up again. Now it works because the init of the db has finished.

解决方案:我搜索了www,发现它似乎是一个已知问题,可以在 wait-for-it.sh 文件中解决.这必须包含在Dockerfile中的COPY中.因此,我不是专家,但我正在寻找一个好的解决方案:

Solution: I searched the www and for it seems like a known problem which might be solved within a wait-for-it.sh file. This has to be included with COPY in the Dockerfile. So I'm no expert but I am searching for a good solution to either:

  • 在spring内初始化数据库,使我的服务一直等到mysql准备就绪
  • 或在第一次运行时通过卷从容器中初始化数据库,当然可以解决此初始化问题.

我不知道什么是最佳实践,我和我将非常感谢您对如何建立最佳实践的帮助.

I don't know what is best practice here I and I would be very thankful for some help how to build this up.

推荐答案

仅在首次运行时加载sql文件:

您可以使用下面的撰写文件

You can use the below compose file

version: '2.1'
services:

  usermanagement-service:
    build: ./UserManagementService
    restart: on-failure
    ports:
      - "7778:7778"
    depends_on:
      mysqldb:
        condition: service_healthy

  mysqldb:
    image: mysql
    volumes:
      - ./mysql-data:/var/lib/mysql
      - ./mysql-init-files:/docker-entrypoint-initdb.d
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: userdb
      MYSQL_USER: testuser
      MYSQL_PASSWORD: testuser
    ports:
      - "3600:3306"
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

您必须将 data.sql schema.sql 文件放在 ./docker-entrypoint-initdb.d 目录下,使用<代码>卷 以获取更多信息.

You have to place your data.sql and schema.sql files under ./docker-entrypoint-initdb.d directory using Volumes for more info.

SQL文件.(即您的情况 ./mysql-data 文件夹应为空

SQL files in this folder will be loaded only if the DB's data directory is empty (the very first run of db service). (i.e) in your case ./mysql-data folder should be empty

第二个问题:

您可以使用 healthcheck service_healthy ,而不是使用 wait-for-it.sh .一旦 mysqldb 成功执行 ["CMD","mysqladmin","ping",-h","localhost"],此处将启动 usermanagement-service 在指定的时间间隔内.有关运行状况检查和Depends_on的更多详细信息,请在此处.

Instead of using wait-for-it.sh, you can use healthcheck and service_healthy. Here usermanagement-service will be started once mysqldb successfully executes ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] in the specified interval. For more details on healthcheck and depends_on, refer here.

此处获得 test 命令.

这篇关于如何在Docker Compose中初始化MySql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆