0

I have installed Ubuntu using virtual box and also install apt, docker and docker-compose inside it. Now I want to create MySQL database using docker-compose.yml In which, i defined MySQL configuration including imported database file. When i execute following command it install MySQL but not create database.

sudo docker-compose up -d --force-recreate --build

docker-compose.yml

mysql:
    image: "mysql:5.7"
    network_mode: "bridge"
    volumes:
      - ${DATA_ROOT}/mysql:/var/lib/mysql
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    environment:
      - MYSQL_ROOT_PASSWORD=${ENV_PASSWORD}
      - MYSQL_USER="${ENV_USER}"
      - MYSQL_PASSWORD="${ENV_PASSWORD}"
      - MYSQL_PORT_3306_TCP_ADDR=0.0.0.0
      - MYSQL_PORT_3306_TCP_PORT=3306
    command:
      - --user=root
      - --max_allowed_packet=500M
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --max_connections=250
      - --default-authentication-plugin=mysql_native_password
    ports:
      - "3306:3306"

Also in some cases it allow me to create first time but when i remove whole image and volume in docker and try to recreate again then it given same issue.

I suppose there is some pre-installed file which restrict me to recreate it.(It's just my assumption)

1 Answer 1

1

You need to remove data directory - ${DATA_ROOT}/mysql:/var/lib/mysql if you want to initialize database with /docker-entrypoint-initdb.d as clearly mentioned in offical documentation

Usage against an existing database

If you start your mysql container instance with a data directory that already contains a database (specifically, a mysql subdirectory), the $MYSQL_ROOT_PASSWORD variable should be omitted from the run command line; it will in any case be ignored, and the pre-existing database will not be changed in any way.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for giving an answer. But i already tried this thing of remove data directory but it doesn't work. And for second portion, I try to create MySQL after remove image and also prune volume in docker.
just the mount path, then docker-compose rm -f && docker-compose up is enough
I also do that but it also not worked. I tried lot many things which are not working that's why i place this question suppose i missed something at time of reading docs. But thank yo so much for given an answer.
okay seems like the asnwer did not help in your case but it common issue that I posted the answer for, let me delete the answer.

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.