Docker-compose mysql:导入 .sql
Posted
技术标签:
【中文标题】Docker-compose mysql:导入 .sql【英文标题】:Docker-compose mysql: import .sql 【发布时间】:2017-06-27 11:25:56 【问题描述】:我的 docker-compose 脚本成功运行 mysql/mariadb 服务,并将包含数据库架构的“init.sql”脚本复制到“/docker-entrypoint-initdb.d”。但是,sql 脚本永远不会像应有的那样执行according to the docs。
没有错误,日志中没有任何内容。我检查了 sql 文件是否复制到容器中的正确位置。我检查了脚本在空数据库上运行时没有错误。
我错过了什么?如果脚本被执行(并且由于某种原因什么也没做),它甚至在日志中可见吗?
(编辑: docker-compose 版本:1.8.1,图像 mariadb:10.1.21)
# docker-compose.yml
version: '2'
volumes:
data-volume:
services:
mysql:
image: mariadb
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: pwd
MYSQL_DATABASE: users_db
volumes:
- data-volume:/var/lib/mysql
- ./resources/docker-sql/init.sql:/docker-entrypoint-initdb.d/init.sql
backend:
image: myapp
ports:
- "8000:80"
depends_on:
- mysql
links:
- mysql
【问题讨论】:
根据docker-entrypoint.sh
的来源(应该执行init.sql
的脚本),如果执行,日志中应该有一行(第154 行)。你能添加docker logs
的输出吗?此外,您的脚本只会在容器第一次启动时执行(第 76 行)!您必须在两次尝试之间删除它。
哦,我确实不得不删除容器。谢谢! docker-compose stop
, docker-compose rm
, docker-compose up
.
你们谁来写一个正式的答案? :)
请注意,没有打印出与实际运行的 sql 脚本相关的日志。也许有一个详细的选项。但不再是问题了。如果@fzgregor 没有写出答案,我明天再写。
【参考方案1】:
查看 mariadb 图像的docker-entrypoint.sh
文件的来源,应该执行您的init.sql
的脚本,很明显
-
该脚本只会在容器之前不包含数据库时执行(脚本的第 76 行),并且
会有一个日志输出通知您调用了您的脚本。
要使用docker-compose
实现此目的,您必须停止服务,移除容器(以删除数据库)并重新启动它:
docker-compose stop
docker-compose rm
docker-compose start
这是图像行为的示例,仅使用.sh
文件而不是.sql
:
$ cat hello.sh
echo "This is output of the hello script"
$ docker run -it -v `pwd`/hello.sh:/docker-entrypoint-initdb.d/hello.sh -v mariadb_test:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 mariadb
Initializing database
2017-02-09 14:13:54 140617005938624 [Note] /usr/sbin/mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 63 ...
2017-02-09 14:13:54 140617005938624 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2017-02-09 14:13:54 140617005938624 [Note] InnoDB: The InnoDB memory heap is disabled
2017-02-09 14:13:54 140617005938624 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
[...]
2017-02-09 14:14:03 139836212086528 [Note] InnoDB: Dumping buffer pool(s) not yet started
2017-02-09 14:14:03 139836971521984 [Note] Plugin 'FEEDBACK' is disabled.
2017-02-09 14:14:03 139836971521984 [Warning] 'user' entry 'root@830dbd0908f3' ignored in --skip-name-resolve mode.
2017-02-09 14:14:03 139836971521984 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode.
2017-02-09 14:14:03 139836971521984 [Note] mysqld: ready for connections.
Version: '10.1.21-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 0 mariadb.org binary distribution
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
GENERATED ROOT PASSWORD: dau6voh4eej2jooRohpiop4eh6ahl7Uz
2017-02-09 14:14:05 139836970654464 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode.
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/hello.sh
This is output of the hello script
2017-02-09 14:14:05 139836970351360 [Note] mysqld: Normal shutdown
2017-02-09 14:14:05 139836970351360 [Note] Event Scheduler: Purging the queue. 0 events
2017-02-09 14:14:05 139836195301120 [Note] InnoDB: FTS optimize thread exiting.
2017-02-09 14:14:05 139836970351360 [Note] InnoDB: Starting shutdown...
2017-02-09 14:14:05 139836970351360 [Note] InnoDB: Waiting for page_cleaner to finish flushing of buffer pool
2017-02-09 14:14:07 139836970351360 [Note] InnoDB: Shutdown completed; log sequence number 1616829
2017-02-09 14:14:07 139836970351360 [Note] mysqld: Shutdown complete
MySQL init process done. Ready for start up.
[...]
您在日志输出中的某处看到hello.sh
脚本的调用及其标准输出被掩埋。在容器的后续启动中,脚本不会执行,因为数据库已经创建(在本地 mariadb 卷中):
$ docker run -it -v `pwd`/hello.sh:/docker-entrypoint-initdb.d/hello.sh -v mariadb_test:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 mariadb
2017-02-09 14:19:13 140155189532608 [Note] mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 1 ...
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: The InnoDB memory heap is disabled
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Compressed tables use zlib 1.2.8
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using Linux native AIO
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using SSE crc32 instructions
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Completed initialization of buffer pool
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Highest supported file format is Barracuda.
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: 128 rollback segment(s) are active.
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Waiting for purge to start
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.34-79.1 started; log sequence number 1616839
2017-02-09 14:19:13 140154429736704 [Note] InnoDB: Dumping buffer pool(s) not yet started
2017-02-09 14:19:13 140155189532608 [Note] Plugin 'FEEDBACK' is disabled.
2017-02-09 14:19:13 140155189532608 [Note] Server socket created on IP: '::'.
2017-02-09 14:19:13 140155189532608 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode.
2017-02-09 14:19:13 140155189532608 [Note] mysqld: ready for connections.
Version: '10.1.21-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
【讨论】:
其他有问题的人,docker-compose stop docker-compose rm
命令非常关键,因为它们可以帮助您重新开始。否则您对撰写文件的更改可能不会生效。
@GideonMaina 您缺少 && 符号。 docker-compose stop && docker-compose rm
。我用 docker-compose down && docker-compose up
为我工作。【参考方案2】:
可能有点晚了,但我找到了另一个更准确的解决方案。
通过depends_on
和healthcheck
,在mysql 转储完成之前不要运行后端。唯一的细节是您必须使用 2.1 版运行它。
# docker-compose.yml
version: '2.1'
volumes:
data-volume:
services:
mysql:
image: mariadb
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: pwd
MYSQL_DATABASE: users_db
depends_on:
backend:
condition: service_healthy
volumes:
- data-volume:/var/lib/mysql
- ./resources/docker-sql/init.sql:/docker-entrypoint-initdb.d/init.sql
backend:
image: myapp
ports:
- "8000:80"
depends_on:
- mysql
links:
- mysql
healthcheck:
test: "/usr/bin/mysql --user=root --password=you_pass --database=your_db --execute \"SELECT * FROM last_table_of_your_dump;\""
interval: 10s
timeout: 3s
retries: 10
【讨论】:
以上是关于Docker-compose mysql:导入 .sql的主要内容,如果未能解决你的问题,请参考以下文章
docker-compose 构建mongodb并导入基础数据示例
使用 docker-compose 时导入 keycloak 配置文件
Docker-Compose Postgresql 导入转储