为什么我的docker-entrypoint-initdb.d脚本(在docker-compose.yml中指定)没有执行以初始化新的MySQL实例? [英] Why isn't my docker-entrypoint-initdb.d script (as specified in docker-compose.yml) executed to initialize a fresh MySQL instance?

查看:88
本文介绍了为什么我的docker-entrypoint-initdb.d脚本(在docker-compose.yml中指定)没有执行以初始化新的MySQL实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此答案/docker-entrypoint-initdb.d 目录的信息>,并阅读如何使用此映像" MySQL文档的初始化新实例"部分..但是,当我在下面包含 docker-compose.yml 文件的目录中运行 docker-compose up 时,我的数据库未初始化.

 服务:#使用root/root作为MySQL用户/密码凭证D b:图片:mysql重启:总是环境:MYSQL_ROOT_PASSWORD:根MYSQL_USER:根目录MYSQL_PASSWORD:根MYSQL_DATABASE:数据库数量:-./mysql/data:/var/lib/mysql-./mysql/init:/docker-entrypoint-initdb.d/:ro管理员:图片:管理员重启:总是端口:-8080:8080 

我确认 ./mysql/init 目录包含一个名为 init.sql 的文件.并且我确认,在清空 ./mysql/data 目录并运行 docker-compose up 之后,将创建一个 db 数据库.但是,除非我在Adminer中手动执行脚本,否则不会填充数据库.(我单击导入",然后选择文件,然后按执行"按钮.)

我在运行 docker-compose up 后在控制台输出中寻找消息,这些消息表明尝试运行 init.sql 并找不到任何内容.

更新:MySQL版本是8.0.19.

解决方案

魔鬼隐藏在细节中...

您的环境变量中有 root 的双重定义.默认情况下,使用来自 MYSQL_ROOT_PASSWORD 的密码创建 root 用户.然后,您要求创建另一个具有相同名称和密码(即使用 MYSQL_USER MYSQL_PASSWORD )的普通"用户...

如果仔细查看启动日志,将会看到错误

  db_1 |第1行的错误1396(HY000):'root'@'%'操作CREATE USER失败 

这实际上会停止进一步处理 docker-entrypoint-initdb.d 中的初始化文件,并继续进行其余的图像启动过程(即在临时服务器上初始化后重新启动mysql)./p>

只需将 MYSQL_USER MYSQL_PASSWORD 放入环境变量中,或将用户设置为与 root 不同的用户,您将立即看到已处理的初始化文件(不要忘记再次清空数据目录).

I found out about the /docker-entrypoint-initdb.d directory from this answer, and also read the "Initializing a fresh instance" section of the "How to use this image" MySQL documentation. But when I run docker-compose up in the directory containing the docker-compose.yml file below, my database isn't initialized.


services:

# Use root/root as MySQL user/password credentials
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_DATABASE: db
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/init:/docker-entrypoint-initdb.d/:ro

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

I confirmed the ./mysql/init directory contains a file named init.sql. And I confirmed that after I empty the ./mysql/data directory and run docker-compose up, that a db database is created. But the database is not populated, unless I manually execute the script in Adminer. (I click on "Import", then choose the file and press the Execute button.)

I looked for messages in the console output after running docker-compose up that indicate an attempt to run init.sql and can't find anything.

Update: The MySQL version is 8.0.19.

解决方案

Devil hides in details...

You have a double definition of root in your env vars. root user is created by default with password from MYSQL_ROOT_PASSWORD. You then ask to create a second "normal" user... with the exact same name and password (i.e. with MYSQL_USER and MYSQL_PASSWORD)

If you look carefully at your startup log, you will see an error

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

This actually stops further processing of your init files in docker-entrypoint-initdb.d and goes on with the rest of the image startup process (i.e. restarting mysql after initialization on temporary server).

Simply drop MYSQL_USER and MYSQL_PASSWORD in your env vars, or set a different user than root and you will immediately see your init files processed (don't forget to empty your data dir again).

这篇关于为什么我的docker-entrypoint-initdb.d脚本(在docker-compose.yml中指定)没有执行以初始化新的MySQL实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆