简体   繁体   English

为什么我的 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?

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 .我从这个答案中发现了/docker-entrypoint-initdb.d目录,还阅读了“如何使用这个图像”MySQL 文档的“初始化一个新实例”部分。 But when I run docker-compose up in the directory containing the docker-compose.yml file below, my database isn't initialized.但是当我在包含下面docker-compose.yml文件的目录中运行docker-compose up时,我的数据库没有初始化。


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 .我确认./mysql/init目录包含一个名为init.sql的文件。 And I confirmed that after I empty the ./mysql/data directory and run docker-compose up , that a db database is created.我确认在我清空./mysql/data目录并运行./mysql/data docker-compose up ,创建了一个db数据库。 But the database is not populated, unless I manually execute the script in Adminer.但是没有填充数据库,除非我在 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.我在运行init.sql docker-compose up后在控制台输出中查找消息,这些消息表明尝试运行init.sql并且找不到任何东西。

Update: The MySQL version is 8.0.19.更新: MySQL 版本为 8.0.19。

Devil hides in details...魔鬼藏在细节里……

You have a double definition of root in your env vars.您的环境变量中有root的双重定义。 root user is created by default with password from MYSQL_ROOT_PASSWORD . root用户默认使用来自MYSQL_ROOT_PASSWORD密码创建。 You then ask to create a second "normal" user... with the exact same name and password (ie with MYSQL_USER and MYSQL_PASSWORD )然后您要求创建第二个“普通”用户......使用完全相同的名称和密码(即使用MYSQL_USERMYSQL_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 (ie restarting mysql after initialization on temporary server).这实际上会停止进一步处理docker-entrypoint-initdb.d中的 init 文件,并继续进行其余的映像启动过程(即在临时服务器上初始化后重新启动 mysql)。

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).只需将MYSQL_USERMYSQL_PASSWORD放在您的环境变量中,或者设置一个不同于root用户,您将立即看到您的 init 文件已处理(不要忘记再次清空您的数据目录)。

暂无
暂无

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

相关问题 docker 权限被拒绝 mysql 映像中的 shell 脚本放置在 docker-entrypoint-initdb.d - docker permission denied for shell script in mysql image placed at docker-entrypoint-initdb.d 无法使用 mysql docker 容器在 docker-entrypoint-initdb.d 中创建数据库 - Can't create db in docker-entrypoint-initdb.d with mysql docker container docker-compose up invalid mode /docker-entrypoint-initdb.d/ - 无法为服务数据库创建容器 - docker-compose up invalid mode /docker-entrypoint-initdb.d/ - cannot create container for service database 如何等待docker-entrypoint-initdb.d在Shell脚本/ Bash中加载数据库? - How to wait for docker-entrypoint-initdb.d to load DB in Shell Script/Bash? 自定义mysql高山映像未从作为卷安装的docker-entrypoint-initdb.d加载init.sql - Custom mysql alpine image not loading init.sql from docker-entrypoint-initdb.d mounted as a volume docker-entrypoint-initdb 中的 MySQL 脚本不执行 - MySQL scripts in docker-entrypoint-initdb are not executed sql转储是否被加密到docker容器docker-entrypoint-initdb.d中? - Are sql dumps moved into docker container `docker-entrypoint-initdb.d` encrypted? 通过 docker-compose.yml 运行 php 脚本 - Run php script by docker-compose.yml docker-compose.yml问题nodejs和mysql - docker-compose.yml issue nodejs and mysql 当容器从另一个容器中启动时,docker-entrypoint-initdb.d 为空 - docker-entrypoint-initdb.d is empty when container is started from within another container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM