【问题标题】:Docker-compose / init.sql doesn't get executed correctlyDocker-compose / init.sql 没有正确执行
【发布时间】:2020-04-06 00:19:53
【问题描述】:

我正在尝试通过docker-compose 设置一个本地 MySQL 实例,它看起来像这样:

version: '3'

services:
  database:
    image: mysql:latest
    ports:
      - '3306:3306'
    command: '--init-file /data/application/init.sql'
    command: 'mysqld --default-authentication-plugin=mysql_native_password'
    volumes:
      - './init.sql:/data/application/init.sql'
      - './data:/var/lib/mysql'
    env_file:
      - ./.env
    expose:
      - '3306'
    # cap_add:
    #   - SYS_NICE
    environment:
      - MYSQL_ROOT_PASSW="${MYSQL_ROOT_PASSWORD}"
      - MYSQL_DATABASE="${MYSQL_DATABASE}"
      - MYSQL_USER="${MYSQL_USER}"
      - MYSQL_ROOT_USER="${MYSQL_ROOT_USER}"
      - MYSQL_PASSWORD="${MYSQL_PASSWORD}"
    # network_mode: host

使用以下 init.sql 脚本,它应该负责创建数据库和设置表:

CREATE DATABASE IF NOT EXISTS test;
USE test;

CREATE TABLE IF NOT EXISTS `listings`(
    id INTEGER
    ,PRIMARY KEY (id)
);

容器构建并正常启动:

Attaching to so-feed-parse_database_1
database_1  | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
database_1  | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
database_1  | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
database_1  | 2019-12-12 14:58:39+00:00 [Note] [Entrypoint]: Initializing database files
database_1  | 2019-12-12T14:58:39.976524Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1  | 2019-12-12T14:58:39.976669Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 46
database_1  | 2019-12-12T14:58:41.624212Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
database_1  | 2019-12-12 14:58:44+00:00 [Note] [Entrypoint]: Database files initialized
database_1  | 2019-12-12 14:58:44+00:00 [Note] [Entrypoint]: Starting temporary server
database_1  | 2019-12-12T14:58:44.542746Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1  | 2019-12-12T14:58:44.542845Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 95
database_1  | 2019-12-12T14:58:45.012478Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
database_1  | 2019-12-12T14:58:45.016362Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
database_1  | 2019-12-12T14:58:45.054899Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
database_1  | 2019-12-12 14:58:45+00:00 [Note] [Entrypoint]: Temporary server started.
database_1  | 2019-12-12T14:58:45.128416Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
database_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
database_1  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
database_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
database_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
database_1  | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Creating database "test"
database_1  | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Creating user "utest"
database_1  | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Giving user "utest" access to schema "test"
database_1  | 
database_1  | 2019-12-12 14:58:47+00:00 [Note] [Entrypoint]: Stopping temporary server
database_1  | 2019-12-12T14:58:47.355373Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.18).
database_1  | 2019-12-12T14:58:49.162768Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.18)  MySQL Community Server - GPL.
database_1  | 2019-12-12 14:58:49+00:00 [Note] [Entrypoint]: Temporary server stopped
database_1  | 
database_1  | 2019-12-12 14:58:49+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
database_1  | 
database_1  | 2019-12-12T14:58:49.662509Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
database_1  | 2019-12-12T14:58:49.662675Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 1
database_1  | 2019-12-12T14:58:50.034163Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
database_1  | 2019-12-12T14:58:50.036925Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
database_1  | 2019-12-12T14:58:50.062397Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
database_1  | 2019-12-12T14:58:50.222174Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

现在,在登录服务器后,show databases; 给出以下输出(尝试use test; 输出unknown database

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| "test"             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

手动执行初始化脚本就像一个魅力,不知道它是否可能取决于--init-file 命令。


编辑: this 问题中的 OP 遵循类似的方法,不使用 command: '--init-file',而是将 init.sql 文件安装在 ./init.sql:/docker-entrypoint-initdb.d/init.sql 上。 重新洗牌后,我的 docker-compose.yml 围绕 db 现在已正确创建。我不太清楚为什么会发生这种情况......

【问题讨论】:

  • 您是否故意使用了多个command 行?
  • 是的,我不得不切换默认的身份验证插件,因为没有该命令我遇到了协议冲突。
  • ....如果将两条线合并成一条线会发生什么?我认为不支持多个commands
  • 并没有真正的帮助,显然我在滥用--init-file。对于 MySQL init,它应该总是在第一次运行时挂载和执行。 Here 更多关于这个问题的信息。

标签: mysql docker docker-compose


【解决方案1】:

参考这里https://onexlab-io.medium.com/docker-compose-mysql-initdb-4c3388047dea

注意: mysql5.7 MYSQL_SERVICE_USER 如果将 MYSQL_SERVICE_USER 设置为user,则不会执行启动

【讨论】:

  • 自从我上次做这个已经有一段时间了。想想我遇到的问题之一可能与你笔记中的评论有关。谢谢你的时间,赞成这个。
【解决方案2】:

'./init.sql:/data/application/init.sql' 路径不正确,应该映射到/docker-entrypoint-initdb.d,作为在/docker-entrypoint-initdb.d 下查找文件的入口点。

最好从文档中查看此警告。

警告:

/docker-entrypoint-initdb.d 中的脚本仅在您运行时运行 以空的数据目录启动容器;任何 容器启动时,预先存在的数据库将保持不变。一 常见的问题是,如果您的 /docker-entrypoint-initdb.d 之一 脚本失败(这将导致入口点脚本退出)和 您的协调器使用已经初始化的容器重新启动容器 数据目录,它不会继续你的脚本。

postgres Initialization scripts

【讨论】:

  • 连接mysql的问题为什么还要讲Postgres?我遇到了同样的问题,无法通过将我的 init.sql 放置到 /docker-entrypoint-initdb.d 来应用此解决方案(如果重要的话确实存在)
猜你喜欢
  • 2020-03-17
  • 2020-06-12
  • 1970-01-01
  • 2019-03-13
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2019-10-22
  • 1970-01-01
相关资源
最近更新 更多