docker-compose mysql init sql is not executed(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 不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:docker-compose mysql init sql 不执行
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
