如何确保docker容器中的MySQL使用docker-entrypoint-initdb.d中的脚本进行初始化?

我的dockerfile:

FROM mysql/mysql-server:5.7 COPY ./create_db.sql /docker-entrypoint-initdb.d COPY ./create_tables.sql /docker-entrypoint-initdb.d COPY ./insert_types.sql /docker-entrypoint-initdb.d COPY ./my.cnf /etc/mysql/conf.d RUN chmod -R 755 /docker-entrypoint-initdb.d EXPOSE 1307 

输出mysql容器的日志:

 docker logs 8742 [Entrypoint] MySQL Docker Image 5.7.20-1.1.2 [Entrypoint] Starting MySQL 5.7.20-1.1.2 

这似乎…短。

(假冒btw密码)

容器中init文件夹的内容; 以下命令来自容器内部:

 bash-4.2# ls -alh docker-entrypoint-initdb.d total 20K drwxr-xr-x 1 root root 4.0K Nov 3 20:09 . drwxr-xr-x 1 root root 4.0K Nov 3 21:02 .. -rwxr-xr-x 1 root root 268 Nov 3 16:01 create_db.sql -rwxr-xr-x 1 root root 911 Nov 1 18:08 create_tables.sql -rwxr-xr-x 1 root root 363 Nov 1 18:08 insert_types.sql 

创build脚本的内容:

 bash-4.2# cat docker-entrypoint-initdb.d/create_db.sql SELECT 'creating database duplicate_identifiers' as 'logging'; create database duplicate_identifiers; create user 'deduplicator'@'%' IDENTIFIED BY 'ajsdfhaosidufjlkjljlui08!'; GRANT ALL PRIVILEGES ON events.* TO 'deduplicator'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES; 

根密码设置为:

 bash-4.2# echo $MYSQL_ROOT_PASSWORD whiskey 

但我无法使用该连接:

 bash-4.2# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 

Mysql存在,但数据库从未创build。

 bash-4.2# mysql -u root Welcome to the MySQL monitor. ... mysql> use duplicate_identifiers; ERROR 1049 (42000): Unknown database 'duplicate_identifiers' 

我很难过 我如何让MySQL使用初始化脚本来运行和创build我的数据库?

如果docker-entrypoint.sh未完全执行,那可能是因为包装docker-entrypoint-initdb.d脚本调用的if条件。

 if [ "$1" = 'mysqld' ]; then if [ ! -d "$DATADIR/mysql" ]; then 

因此,首先testing并打印这些值,看看for f in /docker-entrypoint-initdb.d/*; do中的for f in /docker-entrypoint-initdb.d/*; do是否正常for f in /docker-entrypoint-initdb.d/*; do for f in /docker-entrypoint-initdb.d/*; do根本不叫。