我的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
这似乎......简短。
(虚假密码低于顺便说一句)
容器中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使用初始化脚本来运行和创建我的数据库?
答案 0 :(得分:0)
如果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
是否正常。