Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my docker-entrypoint-initdb.d script (as specified in docker-compose.yml) executed to initialize a fresh MySQL instance?

I found out about the /docker-entrypoint-initdb.d directory from this answer, and also read the "Initializing a fresh instance" section of the "How to use this image" MySQL documentation. But when I run docker-compose up in the directory containing the docker-compose.yml file below, my database isn't initialized.


services:

# Use root/root as MySQL user/password credentials
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_DATABASE: db
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/init:/docker-entrypoint-initdb.d/:ro

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

I confirmed the ./mysql/init directory contains a file named init.sql. And I confirmed that after I empty the ./mysql/data directory and run docker-compose up, that a db database is created. But the database is not populated, unless I manually execute the script in Adminer. (I click on "Import", then choose the file and press the Execute button.)

I looked for messages in the console output after running docker-compose up that indicate an attempt to run init.sql and can't find anything.

Update: The MySQL version is 8.0.19.

like image 712
Daryl Spitzer Avatar asked Feb 01 '20 21:02

Daryl Spitzer


1 Answers

Devil hides in details...

You have a double definition of root in your env vars. root user is created by default with password from MYSQL_ROOT_PASSWORD. You then ask to create a second "normal" user... with the exact same name and password (i.e. with MYSQL_USER and MYSQL_PASSWORD)

If you look carefully at your startup log, you will see an error

db_1       | ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'

This actually stops further processing of your init files in docker-entrypoint-initdb.d and goes on with the rest of the image startup process (i.e. restarting mysql after initialization on temporary server).

Simply drop MYSQL_USER and MYSQL_PASSWORD in your env vars, or set a different user than root and you will immediately see your init files processed (don't forget to empty your data dir again).

like image 165
Zeitounator Avatar answered Sep 22 '22 16:09

Zeitounator