未执行docker-compose mariadb docker-entrypoint-initdb.d sql

kq0g1dla  于 2022-11-08  发布在  Docker
关注(0)|答案(2)|浏览(222)

我试图让我的docker db容器在创建时自动用数据集填充数据库。根据mariadb documentation,卷中有一个docker-entrypoint-initdb.d文件夹可以用于此目的。
我设置了我的docker-compose.yml文件来镜像我在StackOverflow上找到的示例,但是仍然无法执行我的SQL脚本。

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内容:

  1. $ docker-compose down -v我发现这实际上并没有删除数据库卷。运行docker volume ls仍然会显示过去的卷。
  2. $ docker volume prune此命令清除卷。运行此命令后运行docker volume ls将显示一个空列表。
  3. $ rm -rf ./cms/conf/mysql这将清除db容器所做的任何实际文件系统更改。
  4. $ docker-compose up -d --build重建容器。这将触发我的初始化SQL脚本。
    Docker运行,并且成功创建了db容器。我可以访问在localhost:8080上运行的adminer容器。我可以使用docker-compose.yml文件中指定的密码以root用户身份登录。
    Adminer界面显示了标准的MySQL数据库(information_schemamysqlperformance_schema),还显示了在docker-compose.yml文件的MYSQL_DATABASE值中定义的wordpress数据库。但是,我的init.sql数据库却找不到。
    我发现其他一些类似的StackOverflow问题也有类似的问题。我已经按照公认的答案,仍然无法得到任何SQL脚本得到执行。
    1.如何在init.sql文件中设置数据库、表和预弹出?
  5. Issue with docker compose initial DB setup once
  6. mySQL init scripts not running with docker-compose
  7. Create database on docker-compose startup
    这是我的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

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

1bqhqjot

1bqhqjot1#

我遇到了一个相同的问题,我更仔细地调查了日志。我不确定下面的错误是否相关,但我决定摆脱它。

ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'

root用户似乎是默认创建的,因此我将该用户的environment变量更改为user,而不是root

MYSQL_USER: user

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

bvjxkvbb

bvjxkvbb2#

我必须重置数据卷,否则进入脚本将不会触发。在这种情况下,您必须删除所有内容:
sudo rm -rf /cms/conf/mysql/data/

相关问题