Docker-compose mysql:导入.sql

时间:2024-10-01 19:25:23

回答问题

我的 docker-compose 脚本成功地运行了 mysql/mariadb 服务,并将包含数据库模式的“”脚本复制到“/”。但是,根据文档](/_/mariadb/),sql 脚本永远不会像[那样执行。

没有错误,日志中没有任何内容。我检查了 sql 文件是否复制到容器中的正确位置。我检查了脚本在空数据库上运行时没有错误。

我错过了什么?如果脚本被执行(并且由于某种原因什么也没做),它甚至在日志中可见吗?

(编辑: docker-compose 版本:1.8.1,图像 mariadb:10.1.21)

# 

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/://
  backend:
    image: myapp
    ports:
      - "8000:80"
    depends_on:
      - mysql
    links:
      - mysql

Answers

查看 mariadb 映像的文件的来源,该脚本应该执行您的,很明显

  1. 脚本只会在容器之前不包含数据库时执行(脚本第 76 行),并且

  2. 会有一个日志输出通知你你的脚本被调用了。

要使用docker-compose实现此目的,您必须停止服务,删除容器(以摆脱数据库)并重新启动它:

docker-compose stop
docker-compose rm
docker-compose start

这是图像行为的示例,仅使用.sh文件而不是.sql:

$ cat  
echo "This is output of the hello script"
$ docker run -it -v `pwd`/:// -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/'  port: 0   binary distribution
Warning: Unable to load '/usr/share/zoneinfo/' 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/: running //
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.
[...]

您会在日志输出的某处看到脚本的调用及其标准输出被掩埋。在容器的后续启动中,不会执行脚本,因为数据库已经创建(在本地 mariadb 卷中):

$ docker run -it -v `pwd`/:// -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 () 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/'  port: 3306   binary distribution