当前位置:  开发笔记 > 编程语言 > 正文

Docker-compose mysql:import .sql

如何解决《Docker-composemysql:import.sql》经验,为你挑选了1个好方法。



1> fzgregor..:

看一下docker-entrypoint.shmariadb图像文件的来源,应该执行你的脚本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脚本的stdout已被隐藏.在随后启动容器时,不会执行脚本,因为已经创建了数据库(在本地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

推荐阅读
贾志军
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有