简体   繁体   English

Docker-compose 初始化数据库

[英]Docker-compose initialize DB

I have a docker-compose file我有一个 docker-compose 文件

version: '3.7'

services:        
  db:
      image: mysql:latest
      container_name: mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 12345
      volumes:
        - ./docker/db:/docker-entrypoint-initdb.d
      ports:
        - "3306:3306"

and a MySQL script to initialize the DB.和一个 MySQL 脚本来初始化数据库。

CREATE DATABASE `aaa`;
USE `aaa`;


CREATE TABLE `building` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `start_date` date DEFAULT NULL,
  `end_date` date DEFAULT NULL,
  `open` tinyint(4) DEFAULT NULL,
  `address_type` varchar(30) DEFAULT NULL,
  `address_name` varchar(45) DEFAULT NULL,
  `address_number` varchar(45) DEFAULT NULL,
  `cap` char(5) DEFAULT NULL,
  `city` varchar(45) DEFAULT NULL,
  `province` varchar(45) DEFAULT NULL,
  `state` varchar(45) DEFAULT NULL,
  `req_amount` float DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Actually Docker ignore the file INIT_DB.SQL as stated:实际上 Docker 忽略文件 INIT_DB.SQL 如下所述:

mysql | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/DB_INIT.SQL

I want to initialize the DB with that file.我想用那个文件初始化数据库。 I've also tried that: tomcat + mysql + war using docker-compose.yml .我也试过: tomcat + mysql + war 使用 docker-compose.yml

Where's the mistake?错在哪里?

This works for me:这对我有用:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - "3306:3306"

    environment:
      TZ: "America/Halifax"
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: formulary

    volumes:
    - ./mysql-init.sql:/docker-entrypoint-initdb.d/mysql-init.sql
    - ./mysql.cnf:/etc/mysql/conf.d/mysql.cnf

The only difference between what you put and what I put is that I put the source file/target file (not just the directories).你放的和我放的唯一的区别是我放了源文件/目标文件(不仅仅是目录)。 As well, are you sure ./docker/db contains your file?另外,您确定 ./docker/db 包含您的文件吗?

got the same errror with the comments below the first answer (..is a directory).在第一个答案(..是一个目录)下面的评论中得到了同样的错误。 what actually worked for me;什么对我有用;

postgres:
  volumes:
    - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d/

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

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