简体   繁体   English

在Docker容器中初始化MySQL数据

[英]Initialize MySQL data in Docker Container

I tried to create Node.js Container and MySQL Container by docker-compose, so I wrote below files. 我试图通过docker-compose创建Node.js容器和MySQL容器,所以我在下面编写了文件。

tree tree

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

○./docker-compose.yml ○。/搬运工-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 ○。/ MySQL的/ Dockerfile

FROM mysql:latest

RUN mkdir -p /var/log/mysql/

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

○./mysql/init/create.sql ○。/ 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 . 然后我使用sudo docker-compose up --build -d并通过mysql -h 127.0.0.1 -u user -p访问MySQL。 And in database test I INSERT one tuple to users table. 在数据库test我向用户表插入一个元组。

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 . 我以为./mysql/init/create.sql每当我运行sudo docker-compose up --build -d ./mysql/init/create.sql sudo docker-compose up --build -d时都会执行,因此一旦我通过sudo docker-compose down删除容器并通过sudo docker-compose up --build -d重新创建容器sudo docker-compose up --build -d But when I checked test.users , there are three tuples(create.sql inserts TWO tuples). 但是当我检查test.users ,有三个元组(create.sql插入两个元组)。

My thought ( ./mysql/init/create.sql is executed whenever I run sudo docker-compose up --build -d ) is wrong? 我的想法(每当我运行sudo docker-compose up --build -d ./mysql/init/create.sql sudo docker-compose up --build -d时都会执行./mysql/init/create.sql )吗?

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 . /docker-entrypoint-initdb.d中的脚本仅在MySQL数据目录不存在且正在启动容器以实际运行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. 如果要重新运行它,则需要使Docker Compose删除并重新创建数据卷。 I believe docker-compose rm will do it; 我相信docker-compose rm会做到的; simply down and up won't. 简单地downup不会。

(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.) (如果您的应用程序具有更复杂的迁移系统,则在入口点脚本中运行迁移似乎相当普遍,而且我觉得我最近看到的SO示例都围绕Python Django框架进行,尽管特定命令,这是一种有用的通用技术。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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