[英]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_USER
和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 (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_USER
和MYSQL_PASSWORD
放在您的环境变量中,或者设置一个不同于root
用户,您将立即看到您的 init 文件已处理(不要忘记再次清空您的数据目录)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.