简体   繁体   中英

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

I created a 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 .

CREATE DATABASE IF NOT EXISTS wp2;

Then run 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.

$ 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?

Your wp user doesn't have access rights on wp2.

Append to your sql file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM