【问题标题】:How do I make sure MySQL in docker container is initialized using scripts in docker-entrypoint-initdb.d?如何确保使用 docker-entrypoint-initdb.d 中的脚本初始化 docker 容器中的 MySQL?
【发布时间】:2017-11-03 21:24:11
【问题描述】:

我的码头文件:

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

这似乎……很短。

(顺便说一句下面的假密码)

容器中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

创建脚本的内容:

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;

设置了root密码:

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 存在,但从未创建数据库。

bash-4.2# mysql -u root
Welcome to the MySQL monitor. ...

mysql> use duplicate_identifiers;
ERROR 1049 (42000): Unknown database 'duplicate_identifiers'

我被难住了。如何让 MySQL 使用初始化脚本来运行和创建我的数据库?

【问题讨论】:

    标签: mysql docker docker-compose


    【解决方案1】:

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

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

    所以先测试并打印这些值,看看for f in /docker-entrypoint-initdb.d/*; do根本没有被调用是否正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      相关资源
      最近更新 更多