简体   繁体   English

无法使用 mysql docker 容器在 docker-entrypoint-initdb.d 中创建数据库

[英]Can't create db in docker-entrypoint-initdb.d with mysql docker container

I created a docker-compose.yml .我创建了一个docker-compose.yml

version: "3.7"
services:
  db:
    container_name: mysql
    image: mysql
    ports:
      - 3306:3306
    volumes:
      - ./sql:/docker-entrypoint-initdb.d
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_USER : root
      MYSQL_ROOT_PASSWORD: root_pass
      MYSQL_DATABASE: wp1
      MYSQL_USER: wp
      MYSQL_PASSWORD: pass
    restart: always  

And in sql directory, I put a init.sql .sql目录中,我放了一个init.sql

CREATE DATABASE IF NOT EXISTS wp2;

Then run docker-compose up .然后运行docker-compose up

In the log there is a entry for the init script.在日志中有一个用于初始化脚本的条目。

mysql | 2020-04-08 02:17:32+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql

But the database wp2 is not created.但是没有创建数据库wp2

$ docker exec -it mysql bash
# mysql -u wp -ppass
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wp1                |
+--------------------+

How can I create a database with mysql container init script?如何使用 mysql 容器初始化脚本创建数据库?

Your wp user doesn't have access rights on wp2.您的wp用户没有 wp2 的访问权限。

Append to your sql file: Append 到您的 sql 文件:

GRANT ALL on wp2.* to wp@'%';

暂无
暂无

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

相关问题 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 脚本(在 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? sql转储是否被加密到docker容器docker-entrypoint-initdb.d中? - Are sql dumps moved into docker container `docker-entrypoint-initdb.d` encrypted? 当容器从另一个容器中启动时,docker-entrypoint-initdb.d 为空 - docker-entrypoint-initdb.d is empty when container is started from within another container docker 权限被拒绝 mysql 映像中的 shell 脚本放置在 docker-entrypoint-initdb.d - docker permission denied for shell script in mysql image placed at docker-entrypoint-initdb.d 如何等待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 命令&#39;/ bin / sh -c mysql -u wordpress -pwordpress wordpress &lt;/docker-entrypoint-initdb.d/wordpress.sql&#39;返回非零代码:1 - The command '/bin/sh -c mysql -u wordpress -pwordpress wordpress < /docker-entrypoint-initdb.d/wordpress.sql' returned a non-zero code: 1 docker-entrypoint-initdb 中的 MySQL 脚本不执行 - MySQL scripts in docker-entrypoint-initdb are not executed docker mysql initdb脚本无法连接到数据库 - docker mysql initdb script can't connect to database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM