docker-compose mysql init sql未执行 [英] docker-compose mysql init sql is not executed

查看:106
本文介绍了docker-compose mysql init sql未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个mysql docker容器并执行init sql脚本.不幸的是SQL脚本没有执行.我在做什么错了?

I am trying to set up a mysql docker container and execute init sql script. Unfortunately the sql script is not executed. What am I doing wrong?

version: '3.3'
services:
  api:
    container_name: 'api'
    build: './api'
  ports:
    - target: 8080
      published: 8888
      protocol: tcp
      mode: host
  volumes:
    - './api:/go/src/app'
  depends_on:
    - 'mysql'
 mysql:
  image: 'mysql:latest'
  container_name: 'mysql'
  volumes:
    - ./db_data:/var/lib/mysql:rw
    - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
  restart: always
  environment:
    MYSQL_USER: test
    MYSQL_PASSWORD: test
    MYSQL_ROOT_PASSWORD: test
    MYSQL_DATABASE: test
  ports:
    - '3306:3306'
volumes:
  db_data:

我用docker-compose up -d --build

推荐答案

许多容器化的应用程序,尤其是有状态的应用程序,都有一种运行init脚本(如此处的sql脚本)的方式应该只运行一次 .

Many containerized applications, especially stateful ones, have a way of running init scripts (like the sql scripts here) and they are supposed to run only once.

并且由于它们是有状态的,因此对于容器是否在容器重新启动时运行init脚本而言,卷是事实的来源.

And since they are stateful, the volumes are a source of truth for the containers on whether to run the init scripts or not on container restart.

就像您这样,删除用于绑定安装的文件夹或使用新的命名卷,应重新运行存在的所有init脚本.

Like in your case, deleting the folder used for bind mount or using a new named volume should re-run any init scripts present.

这篇关于docker-compose mysql init sql未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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