0

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?

1 Answer 1

1

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

Append to your sql file:

GRANT ALL on wp2.* to wp@'%';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.