docker docker入口点initdb.d sql未执行

docker docker入口点initdb.d sql未执行,docker,docker-compose,mariadb,adminer,Docker,Docker Compose,Mariadb,Adminer,我试图让docker db容器在创建时自动使用数据集填充数据库。根据,卷中有一个docker entrypoint initdb.d文件夹可用于此目的 我将我的docker compose.yml文件设置为镜像在StackOverflow上找到的示例,但仍然无法执行我的SQL脚本。以下是我的docker compose.yml文件的相关部分: version: '3.6' services: db: image: mariadb:10.5.4-focal container

我试图让docker db容器在创建时自动使用数据集填充数据库。根据,卷中有一个
docker entrypoint initdb.d
文件夹可用于此目的

我将我的
docker compose.yml
文件设置为镜像在StackOverflow上找到的示例,但仍然无法执行我的SQL脚本。以下是我的
docker compose.yml
文件的相关部分:

version: '3.6'
services:

  db:
    image: mariadb:10.5.4-focal
    container_name: db
    volumes:
      - ./cms/conf/mysql/data/:/var/lib/mysql/:rw
      - ./cms/sql/:/docker-entrypoint-initdb.d/:ro
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_USER=root
      - MYSQL_PASSWORD=password
      - MYSQL_DATABASE=wordpress
    restart: always

  adminer:
    image: adminer:4.7.7-standalone
    container_name: adminer
    links:
      - db
    ports:
      - 8080:8080
    restart: always
docker compose.yml
文件旁边,我有一个
cms/sql/init.sql
文件,其内容如下:

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE `test`;
在每次测试迭代之前,我会清理所有旧的/缓存的Docker内容:

  • $docker compose down-v
    我发现这实际上并没有删除数据库卷。运行
    docker卷ls
    仍会显示过去存在的卷
  • $docker volume prune
    这将清除卷。运行后运行
    docker volume ls
    ,将显示一个空列表
  • $rm-rf./cms/conf/mysql
    这将清除数据库容器所做的任何实际文件系统更改
  • $docker compose up-d--build
    以重建容器。这将触发我的初始化SQL脚本
  • Docker运行,并且成功创建了
    db
    容器。我能够访问运行在
    localhost:8080
    上的
    adminer
    容器。我可以使用
    docker compose.yml
    文件中指定的密码作为
    root
    用户登录

    管理员界面显示了标准的MySQL数据库(
    information\u schema
    MySQL
    performance\u schema
    ),另外还显示了
    docker compose.yml
    文件的
    MySQL\u database
    值中定义的
    wordpress
    数据库。但是从
    init.sql
    中找不到我的
    test
    数据库

    我发现其他一些类似的StackOverflow问题也有类似的问题。我已经遵循了公认的答案,但仍然无法执行任何SQL脚本

  • 这是我的
    db
    容器中的Docker日志。我没有看到任何看起来奇怪的东西;没有关于初始化等的错误消息:

    2020-07-15 19:21:34+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.4+maria~focal started.
    
    2020-07-15 19:21:35+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    
    2020-07-15 19:21:35+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.4+maria~focal started.
    
    2020-07-15 19:21:35+00:00 [Note] [Entrypoint]: Initializing database files
    
    
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
    
    To do so, start the server, then issue the following commands:
    
    
    '/usr/bin/mysqladmin' -u root password 'new-password'
    
    '/usr/bin/mysqladmin' -u root -h password 'new-password'
    
    
    Alternatively you can run:
    
    '/usr/bin/mysql_secure_installation'
    
    
    which will also give you the option of removing the test
    
    databases and anonymous user created by default. This is
    
    strongly recommended for production servers.
    
    
    See the MariaDB Knowledgebase at https://mariadb.com/kb or the
    
    MySQL manual for more instructions.
    
    
    Please report any problems at https://mariadb.org/jira
    
    
    The latest information about MariaDB is available at https://mariadb.org/.
    
    You can find additional information about the MySQL part at:
    
    https://dev.mysql.com
    
    Consider joining MariaDB's strong and vibrant community:
    
    https://mariadb.org/get-involved/
    
    
    2020-07-15 19:21:47+00:00 [Note] [Entrypoint]: Database files initialized
    
    2020-07-15 19:21:47+00:00 [Note] [Entrypoint]: Starting temporary server
    
    2020-07-15 19:21:47+00:00 [Note] [Entrypoint]: Waiting for server startup
    
    2020-07-15 19:21:47 0 [Note] mysqld (mysqld 10.5.4-MariaDB-1:10.5.4+maria~focal) starting as process 107 ...
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Using Linux native AIO
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Uses event mutexes
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Number of pools: 1
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Using SSE4.2 crc32 instructions
    
    2020-07-15 19:21:47 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Initializing buffer pool, total size = 134217728, chunk size = 134217728
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Completed initialization of buffer pool
    
    2020-07-15 19:21:47 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
    
    2020-07-15 19:21:48 0 [Note] InnoDB: 128 rollback segments are active.
    
    2020-07-15 19:21:48 0 [Note] InnoDB: Creating shared tablespace for temporary tables
    
    2020-07-15 19:21:48 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
    
    2020-07-15 19:21:49 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
    
    2020-07-15 19:21:49 0 [Note] InnoDB: 10.5.4 started; log sequence number 45041; transaction id 21
    
    2020-07-15 19:21:49 0 [Note] Plugin 'FEEDBACK' is disabled.
    
    2020-07-15 19:21:49 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
    
    2020-07-15 19:21:49 0 [Note] InnoDB: Buffer pool(s) load completed at 200715 19:21:49
    
    2020-07-15 19:21:49 0 [Warning] 'user' entry 'root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:49 0 [Warning] 'user' entry '@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:49 0 [Warning] 'proxies_priv' entry '@% root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:49 0 [Note] Reading of all Master_info entries succeeded
    
    2020-07-15 19:21:49 0 [Note] Added new Master_info '' to hash table
    
    2020-07-15 19:21:49 0 [Note] mysqld: ready for connections.
    
    Version: '10.5.4-MariaDB-1:10.5.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 0 mariadb.org binary distribution
    
    2020-07-15 19:21:49+00:00 [Note] [Entrypoint]: Temporary server started.
    
    Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
    
    Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
    
    Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
    
    2020-07-15 19:21:58 5 [Warning] 'proxies_priv' entry '@% root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:58+00:00 [Note] [Entrypoint]: Creating database wordpress23
    
    2020-07-15 19:21:58+00:00 [Note] [Entrypoint]: Creating user root
    
    ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
    
    2020-07-15 19:21:34+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.4+maria~focal started.
    
    2020-07-15 19:21:35+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    
    2020-07-15 19:21:35+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.4+maria~focal started.
    
    2020-07-15 19:21:35+00:00 [Note] [Entrypoint]: Initializing database files
    
    
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
    
    To do so, start the server, then issue the following commands:
    
    
    '/usr/bin/mysqladmin' -u root password 'new-password'
    
    '/usr/bin/mysqladmin' -u root -h password 'new-password'
    
    
    Alternatively you can run:
    
    '/usr/bin/mysql_secure_installation'
    
    
    which will also give you the option of removing the test
    
    databases and anonymous user created by default. This is
    
    strongly recommended for production servers.
    
    
    See the MariaDB Knowledgebase at https://mariadb.com/kb or the
    
    MySQL manual for more instructions.
    
    
    Please report any problems at https://mariadb.org/jira
    
    
    The latest information about MariaDB is available at https://mariadb.org/.
    
    You can find additional information about the MySQL part at:
    
    https://dev.mysql.com
    
    Consider joining MariaDB's strong and vibrant community:
    
    https://mariadb.org/get-involved/
    
    
    2020-07-15 19:21:47+00:00 [Note] [Entrypoint]: Database files initialized
    
    2020-07-15 19:21:47+00:00 [Note] [Entrypoint]: Starting temporary server
    
    2020-07-15 19:21:47+00:00 [Note] [Entrypoint]: Waiting for server startup
    
    2020-07-15 19:21:47 0 [Note] mysqld (mysqld 10.5.4-MariaDB-1:10.5.4+maria~focal) starting as process 107 ...
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Using Linux native AIO
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Uses event mutexes
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Number of pools: 1
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Using SSE4.2 crc32 instructions
    
    2020-07-15 19:21:47 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Initializing buffer pool, total size = 134217728, chunk size = 134217728
    
    2020-07-15 19:21:47 0 [Note] InnoDB: Completed initialization of buffer pool
    
    2020-07-15 19:21:47 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
    
    2020-07-15 19:21:48 0 [Note] InnoDB: 128 rollback segments are active.
    
    2020-07-15 19:21:48 0 [Note] InnoDB: Creating shared tablespace for temporary tables
    
    2020-07-15 19:21:48 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
    
    2020-07-15 19:21:49 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
    
    2020-07-15 19:21:49 0 [Note] InnoDB: 10.5.4 started; log sequence number 45041; transaction id 21
    
    2020-07-15 19:21:49 0 [Note] Plugin 'FEEDBACK' is disabled.
    
    2020-07-15 19:21:49 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
    
    2020-07-15 19:21:49 0 [Note] InnoDB: Buffer pool(s) load completed at 200715 19:21:49
    
    2020-07-15 19:21:49 0 [Warning] 'user' entry 'root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:49 0 [Warning] 'user' entry '@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:49 0 [Warning] 'proxies_priv' entry '@% root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:49 0 [Note] Reading of all Master_info entries succeeded
    
    2020-07-15 19:21:49 0 [Note] Added new Master_info '' to hash table
    
    2020-07-15 19:21:49 0 [Note] mysqld: ready for connections.
    
    Version: '10.5.4-MariaDB-1:10.5.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 0 mariadb.org binary distribution
    
    2020-07-15 19:21:49+00:00 [Note] [Entrypoint]: Temporary server started.
    
    Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
    
    Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
    
    Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
    
    2020-07-15 19:21:58 5 [Warning] 'proxies_priv' entry '@% root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:21:58+00:00 [Note] [Entrypoint]: Creating database wordpress
    
    2020-07-15 19:21:58+00:00 [Note] [Entrypoint]: Creating user root
    
    ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
    
    2020-07-15 19:21:59+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.4+maria~focal started.
    
    2020-07-15 19:22:00+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    
    2020-07-15 19:22:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.5.4+maria~focal started.
    
    2020-07-15 19:22:01 0 [Note] mysqld (mysqld 10.5.4-MariaDB-1:10.5.4+maria~focal) starting as process 1 ...
    
    2020-07-15 19:22:01 0 [Note] mysqld: Aria engine: starting recovery
    
    recovered pages: 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (0.3 seconds); tables to flush: 4 3 2 1 0
    
    (0.0 seconds);
    
    2020-07-15 19:22:01 0 [Note] mysqld: Aria engine: recovery done
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Using Linux native AIO
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Uses event mutexes
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Number of pools: 1
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Using SSE4.2 crc32 instructions
    
    2020-07-15 19:22:01 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Initializing buffer pool, total size = 134217728, chunk size = 134217728
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Completed initialization of buffer pool
    
    2020-07-15 19:22:01 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Starting crash recovery from checkpoint LSN=45069
    
    2020-07-15 19:22:01 0 [Note] InnoDB: 128 rollback segments are active.
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Creating shared tablespace for temporary tables
    
    2020-07-15 19:22:01 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
    
    2020-07-15 19:22:02 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
    
    2020-07-15 19:22:02 0 [Note] InnoDB: 10.5.4 started; log sequence number 45081; transaction id 21
    
    2020-07-15 19:22:02 0 [Note] Plugin 'FEEDBACK' is disabled.
    
    2020-07-15 19:22:02 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
    
    2020-07-15 19:22:02 0 [Note] InnoDB: Buffer pool(s) load completed at 200715 19:22:02
    
    2020-07-15 19:22:02 0 [Note] Server socket created on IP: '::'.
    
    2020-07-15 19:22:02 0 [Warning] 'proxies_priv' entry '@% root@5666e0d8e52e' ignored in --skip-name-resolve mode.
    
    2020-07-15 19:22:02 0 [Note] Reading of all Master_info entries succeeded
    
    2020-07-15 19:22:02 0 [Note] Added new Master_info '' to hash table
    
    2020-07-15 19:22:02 0 [Note] mysqld: ready for connections.
    
    Version: '10.5.4-MariaDB-1:10.5.4+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
    
    [编辑] 我登录到
    db
    容器的CLI,并验证我的init.sql脚本是否位于docker entrypoint initdb.d文件夹中:

    $ cd docker-entrypoint-initdb.d
    $ ls
    init.sql
    $ pwd
    /docker-entrypoint-initdb.d
    

    有人知道还有什么要检查吗?

    我面临着同样的问题,我更仔细地调查了日志。 我不确定以下错误是否相关,但我决定将其清除

    ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'
    
    似乎默认情况下创建了
    root
    user,因此我将用户的
    environment
    变量更改为
    user
    ,而不是
    root

    MYSQL_USER: user
    

    之后,我构建并运行了
    docer compose.yml
    文件和成功执行的
    SQL
    脚本。

    您可以登录到容器中,看看
    /docker entrypoint initdb.d
    是否真的包含该SQL文件吗?@Distdev,是的!我确实登录到CLI界面并运行了一个
    cd/docker入口点initdb.d
    ,然后
    ls-al
    ,我的
    init.sql
    文件是那里唯一的文件。奇怪的是,你的日志上说了一些崩溃恢复。通常这不会发生。即使在完成卷和数据清理之后,您是否每次都有它?它似乎每次都会发生。在清除卷之后,我刚刚进行了另一个新的循环,但仍然收到
    2020-07-15 19:55:48 0[注意]InnoDB:从检查点LSN=45069开始崩溃恢复。你认为这是一个更深层次的问题吗?是的,我以前见过这个问题一次(它也没有执行init-SQLs),但是它是通过删除卷和从头开始容器来解决的。所以,如果这对你没有帮助的话——我几乎没有主意了。也许你可以用mysql图像而不是mariadb做类似的测试,只是为了检查它的行为?