Docker Compose mysql 导入 .sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36617682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:43:01  来源:igfitidea点击:

Docker Compose mysql import .sql

mysqldockerdocker-compose

提问by GafferG

I'm having trouble importing an .sql dump file with docker-compose. I've followed the docs, which apparently will load the .sql file from docker-entrypoint-initdb.d. However, when I run docker-compose up, the sql file is not copied over to the container.

我在使用 docker-compose 导入 .sql 转储文件时遇到问题。我遵循了文档,它显然会从 docker-entrypoint-initdb.d 加载 .sql 文件。但是,当我运行时docker-compose up,sql 文件不会复制到容器中。

I've tried stopping the containers with -vfflag, but that didn't work either. Am I doing something wrong in my .yml script?

我试过用-vf标志停止容器,但这也不起作用。我在 .yml 脚本中做错了什么吗?

I have dump.sql in the directory database/db-dump/ in the root where my compose file is.

我在我的撰写文件所在的根目录中的目录 database/db-dump/ 中有 dump.sql。

frontend:
  image: myimage
  ports:
   - "80:80"
  links:
   - mysql
mysql:
  image: mysql
  ports:
   - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: rootpass
    MYSQL_USER: dbuser
    MYSQL_PASSWORD: userpass
    MYSQL_DATABASE: myimage_db
  volumes:
   - ./database/db-dump:/docker-entrypoint-initdb.d

回答by Sridhar Sg

This worked for me,

这对我有用,

version: '3.1'

services:

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./mysql-dump:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: ecommerce

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

mysql-dump must be a directory. All the .sql's in the directory will be imported.

mysql-dump 必须是一个目录。将导入目录中的所有 .sql。

回答by GafferG

After many attempts with the volumes setting i found a workaround

在对卷设置进行多次尝试后,我找到了一个解决方法

I created another image based on mysql with the following in the Dockerfile

我在 Dockerfile 中使用以下内容创建了另一个基于 mysql 的图像

FROM mysql:5.6

ADD dump.sql /docker-entrypoint-initdb.d

Then removed the volumes from compose and ran the new image

然后从撰写中删除卷并运行新图像

frontend:
  image: myimage
  ports:
   - "80:80"
  links:
   - mysql
mysql:
  image: mymysql
  ports:
   - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: rootpass
    MYSQL_USER: dbuser
    MYSQL_PASSWORD: userpass
    MYSQL_DATABASE: myimage_db

This way the dump is always copied over and run on startup

这样转储总是被复制并在启动时运行

回答by Mvochoa

This appears on the documentation page of Docker MySQL image: https://hub.docker.com/_/mysql/

这出现在 Docker MySQL 镜像的文档页面上:https: //hub.docker.com/_/mysql/

Initializing a fresh instance

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sqland .sql.gzthat are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directoryand provide custom imageswith contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASEvariable.

初始化一个新实例

当容器第一次启动时,将使用提供的配置变量创建和初始化具有指定名称的新数据库。此外,它会执行文件(可扩展).sh.sql并且.sql.gz是在/docker-entrypoint-initdb.d找到。文件将按字母顺序执行。您可以通过将 SQL 转储装载到该目录中并提供带有贡献数据的自定义图像来轻松填充您的 mysql 服务。默认情况下,SQL 文件将导入到MYSQL_DATABASE变量指定的数据库中。

回答by ufukomer

Mysql database dump schema.sqlis resides in the /mysql-dump/schema.sqldirectory and it creates tables during the initialization process.

Mysql数据库转储schema.sql文件是驻留在/mysql-dump/schema.sql目录和它在初始化过程中创建表。

docker-compose.yml:

docker-compose.yml:

mysql:
    image: mysql:5.7
    command: mysqld --user=root
    volumes:
      - ./mysql-dump:/docker-entrypoint-initdb.d
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

回答by Andre Myrtil

I was having a similar issue with mysql where I would mount a local directory at /configs/mysql/data containing a mydatabasedump.sql file via docker-compose to the docker-entrypoint-initdb.d volume, the file would get loaded on to the container but not execute or populate the database when the container initialized. My intial docker-compose.yml looke like this:

我在 mysql 中遇到了类似的问题,我将在 /configs/mysql/data 中通过 docker-compose 将包含 mydatabasedump.sql 文件的本地目录挂载到 docker-entrypoint-initdb.d 卷,该文件将被加载到容器,但在容器初始化时不执行或填充数据库。我的初始 docker-compose.yml 看起来像这样:

#docker-compose.yml
version: '3'
services:
    db:
        build: ./build/mysql/ #this is pointing to my Dockerfile
        container_name: MYSQL_Database
        restart: always
        environment:
        MYSQL_PORT: 3306
        MYSQL_ROOT_PASSWORD: admin
        MYSQL_DATABASE: my_app_database
        MYSQL_USER: admin
        MYSQL_PASSWORD: admin
    volumes:
        - ./configs/mysql/data:/docker-entrypoint-initdb.d:

I found two working solutions for this problem:

我为这个问题找到了两个可行的解决方案:

The first came after I logged in the running container and confirmed that mydatabasedump.sq file was present and executable in the container's docker-entrypoint-initdb.d directory; I created and added a bash script to my local /configs/mysql/data directory called dump.sh that excuted after the container was initialized. It contains a single mysql command that copies my_database_dump.sql to my_app_database. The bash script looks like this

第一个是在我登录正在运行的容器并确认 mydatabasedump.sq 文件在容器的 docker-entrypoint-initdb.d 目录中存在并且可执行之后;我创建了一个 bash 脚本并将其添加到名为 dump.sh 的本地 /configs/mysql/data 目录中,该脚本在容器初始化后执行。它包含一个将 my_database_dump.sql 复制到 my_app_database 的 mysql 命令。bash 脚本如下所示

    #!/bin/bash
    #dump.sh
    mysql -uadmin -padmin my_app_database < my_database_dump.sql 
    #end of dump.sh

I executed this script via my Dockerfile in the ENTRYPOINT directive like this:

我通过我的 Dockerfile 在 ENTRYPOINT 指令中执行了这个脚本,如下所示:

    #Dockerfile
    FROM mysql:5.5
    ENTRYPOINT [ "dump.sh" ]
    EXPOSE 80
    #end of Dockerfile

After realizing the initial issue was due to the volumes being mouted after the cotainer is built and therefore not intilizing the database with the dump file (or executing any scripts in that directory) at boot time, the second solution was simply to move the volumes directive in my compose-file above the built directive. This worked and allowed me to remove the dump.sh scrip and the DOCKERENTRY directive in my Dockerfile. The modified docker-compose.yml looks like this

在意识到最初的问题是由于在构建 cotainer 后卷被移动,因此在启动时没有使用转储文件(或在该目录中执行任何脚本)来初始化数据库,第二个解决方案是简单地移动卷指令在构建指令上方的我的撰写文件中。这有效并允许我删除 Dockerfile 中的 dump.sh 脚本和 DOCKERENTRY 指令。修改后的 docker-compose.yml 看起来像这样

#docker-compose.yml
version: '3'
services:
    db:
        volumes:
          - ./configs/mysql/data:/docker-entrypoint-initdb.d
       build: ./build/mysql/ #this is pointing to my Dockerfile
         container_name: MYSQL_Database
         restart: always
     environment:
         MYSQL_PORT: 3306
         MYSQL_ROOT_PASSWORD: admin
         MYSQL_DATABASE: my_app_database
         MYSQL_USER: admin
         MYSQL_PASSWORD: admin