简体   繁体   中英

Initialize MySQL data in Docker Container

I tried to create Node.js Container and MySQL Container by docker-compose, so I wrote below files.

tree

.
├── docker-compose.yml
├── mysql
│   ├── Dockerfile
│   ├── conf
│   │   ├── default_authentication.cnf
│   │   └── my.cnf
│   ├── data
│   └── init
│       └── create.sql
└── nodejs
    ├── Dockerfile
    └── app

○./docker-compose.yml

version: '3'
services: 
  webserver:
    build: nodejs
    image: node-express-dev:1.0
    container_name: nodejs
    tty: true
    volumes:
      - ./nodejs/app:/app
    ports:
      - "8080:3000"
  db:
    build: mysql
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/init:/docker-entrypoint-initdb.d
      - dbata:/var/lib/mysql
      - ./mysql/conf:/etc/mysql/conf.d
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_USER: user
      MYSQL_PASSWORD: mysql
volumes:
  dbata:
    driver_opts:
      type: none
      device: /home/.../mysql/data
      o: bind

○./mysql/Dockerfile

FROM mysql:latest

RUN mkdir -p /var/log/mysql/

RUN touch /var/log/mysql/mysqld.log

○./mysql/init/create.sql

SET CHARSET UTF8;
DROP DATABASE IF EXISTS test;
CREATE DATABASE test DEFAULT CHARACTER SET utf8;

use test;

SET CHARACTER_SET_CLIENT = utf8;
SET CHARACTER_SET_CONNECTION = utf8;

DROP TABLE IF EXISTS users;
CREATE TABLE users(
id INT,
name VARCHAR(20),
byear INT
);

INSERT INTO users VALUES (1, 'user1', 1995);
INSERT INTO users VALUES (2, 'user2', 1995);

GRANT ALL ON *.* TO 'user';

Then I use sudo docker-compose up --build -d and access to MySQL by mysql -h 127.0.0.1 -u user -p . And in database test I INSERT one tuple to users table.

I thought ./mysql/init/create.sql is executed whenever I run sudo docker-compose up --build -d , so once I remove containers by sudo docker-compose down and recreate containers by sudo docker-compose up --build -d . But when I checked test.users , there are three tuples(create.sql inserts TWO tuples).

My thought ( ./mysql/init/create.sql is executed whenever I run sudo docker-compose up --build -d ) is wrong?

The scripts in /docker-entrypoint-initdb.d are only run if the MySQL data directory doesn't exist and the container is being launched to actually run mysqld . See this part of the entrypoint script and "Initializing a fresh instance" in the image documentation .

If you want to re-run it, you need to cause Docker Compose to delete and recreate the data volume. I believe docker-compose rm will do it; simply down and up won't.

(If your application has a more sophisticated migration system, it seems to be fairly common to run the migrations in an entrypoint script, and I feel like the SO examples I've seen recently have all been around the Python Django framework, though beyond the specific command it's a useful generic technique.)

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