简体   繁体   English

Docker组成mysql:import .sql

[英]Docker-compose mysql: import .sql

My docker-compose script successfully manages to run a mysql/mariadb service, and copies my "init.sql" script containing the database schema to "/docker-entrypoint-initdb.d". 我的docker-compose脚本成功管理了运行mysql / mariadb服务,并将包含数据库架构的“ init.sql”脚本复制到“ /docker-entrypoint-initdb.d”。 However, the sql script is never executed like it should according to the docs . 但是,从未按照docs的要求执行sql脚本。

There is no error, nothing in the logs. 没有错误,日志中没有任何内容。 I checked that the sql file is copied at the right place in the container. 我检查了sql文件是否已复制到容器中的正确位置。 I checked that the script runs with no errors on an empty database. 我检查了该脚本在空数据库上是否没有错误运行。

What am I missing? 我想念什么? Is it even visible in the logs if the script was executed (and for some reason did nothing)? 如果执行了脚本,日志是否甚至可见(由于某种原因什么也不做)?

( Edit: docker-compose version: 1.8.1, image mariadb:10.1.21) 编辑: docker-compose版本:1.8.1,image 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

Looking at the source of the docker-entrypoint.sh file of the mariadb image, the script which is supposed to execute your init.sql , it becomes clear that 查看mariadb映像的docker-entrypoint.sh文件的源代码,该脚本应该执行您的init.sql ,很明显

  1. the script will only be executed when the container didn't contain a database before (line 76 of the script), and 仅当容器之前不包含数据库时,脚本才会执行(脚本的第76行),并且
  2. there will a log output informing you that you script was called. 将会有一个日志输出通知您脚本被调用。

To achieve this with docker-compose you have to stop the service, remove the container (to get rid of the database) and start it again: 要使用docker-compose实现此目的,您必须停止服务,删除容器(以摆脱数据库)并重新启动它:

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

Here is an example of the image's behavior, only with a .sh file instead of .sql : 这是图像行为的示例,仅使用.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.
[...]

You see somewhere in the log output the call and its stdout of the hello.sh script is buried. 您会在日志输出中的某处看到该调用及其hello.sh脚本的标准输出已被掩埋。 On subsequent starts of the container the script is not executed because the database is already created (in the local mariadb volume): 在容器的后续启动中,由于已创建数据库(在本地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

Maybe a little late, but I have found another more accurate solution. 也许有些晚,但是我找到了另一个更准确的解决方案。

Through depends_on and healthcheck , don't run backend until mysql dump has finished. 通过depends_onhealthcheck ,直到mysql dump完成后才运行后端。 The only detail is that you must run it with version 2.1. 唯一的细节是您必须在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM