简体   繁体   English

导入 data.sql MySQL Docker 容器

[英]Import data.sql MySQL Docker Container

If I have a data.sql, how I can import database to my mysql docker container?如果我有一个 data.sql,我如何将数据库导入我的 mysql docker 容器? How I can import database data.如何导入数据库数据。 In a dockerised world this adds a layer of complexity.在 dockerised 世界中,这增加了一层复杂性。 some methods please.请一些方法。

Here my docker-compose.yml:这是我的 docker-compose.yml:

nginx:
  build: ./nginx/
  container_name: nginx-container
  ports:
    - 80:80
  links:
    - php
  volumes_from:
    - app-data

php:
  build: ./php/
  container_name: php-container
  expose:
    - 9000
  links:
    - mysql
  volumes_from:
    - app-data

app-data:
  image: php:7.0-fpm
  container_name: app-data-container
  volumes:
    - ./www/html/:/var/www/html/
  command: "true"

mysql:
  image: mysql:latest
  container_name: mysql-container
  ports:
    - 3306:3306
  volumes_from:
    - mysql-data
  environment:
    MYSQL_ROOT_PASSWORD: secret
    MYSQL_DATABASE: name_db
    MYSQL_USER: user
    MYSQL_PASSWORD: password

mysql-data:
  image: mysql:latest
  container_name: mysql-data-container
  volumes:
    - /var/lib/mysql
  command: "true"

之后您可以导入数据库:

docker exec -i mysql-container mysql -uuser -ppassword name_db < data.sql

Mount your sql-dump under /docker-entrypoint-initdb.d/yourdump.sql utilizing a volume mount使用卷挂载在/docker-entrypoint-initdb.d/yourdump.sql下挂载 sql-dump

mysql:
  image: mysql:latest
  container_name: mysql-container
  ports:
    - 3306:3306
  volumes:
    - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
  environment:
    MYSQL_ROOT_PASSWORD: secret
    MYSQL_DATABASE: name_db
    MYSQL_USER: user
    MYSQL_PASSWORD: password

This will trigger an import of the sql-dump during the start of the container, see https://hub.docker.com/_/mysql/ under "Initializing a fresh instance"这将在容器启动期间触发 sql-dump 的导入,请参阅“初始化新实例”下的https://hub.docker.com/_/mysql/

I can't seem to make this work with the latest mysql or mysql:5.7.我似乎无法使用最新的 mysql 或 mysql:5.7 来完成这项工作。 So I use mariaDB instead.所以我改用 mariaDB。 Here is my docker-compose.yaml code.这是我docker-compose.yaml代码。

version: '3'

services:
  mysql:
    image: mariadb:10.3
    container_name: mariadb
    volumes:
      - container-volume:/var/lib/mysql
      - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: name_db
    ports:
      - "3306:3306"

volumes:
  container-volume:

Another option if you don't wanna mount a volume, but wanna dump a file from your local machine, is to pipe cat yourdump.sql .如果您不想挂载卷,但想从本地机器转储文件,另一种选择是通过管道cat yourdump.sql Like so:像这样:

cat dump.sql | docker exec -i mysql-container mysql -uuser -ppassword db_name

See: https://gist.github.com/spalladino/6d981f7b33f6e0afe6bb见: https : //gist.github.com/spalladino/6d981f7b33f6e0afe6bb

使用docker-compose导入

cat dump.sql | docker-compose exec -T <mysql_container> mysql -u <db-username> -p<db-password> <db-name>

combine https://stackoverflow.com/a/51837876/1078784 and answers in this question, I think the best answer is:结合https://stackoverflow.com/a/51837876/1078784和这个问题的答案,我认为最好的答案是:

cat {SQL FILE NAME} | docker exec -i {MYSQL CONTAINER NAME} {MYSQL PATH IN CONTAINER} --init-command="SET autocommit=0;"

for example in my system this command should look like:例如,在我的系统中,此命令应如下所示:

cat temp.sql | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"

also you can use pv to moniter progress:您也可以使用pv来监控进度:

cat temp.sql | pv | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"

And the most important thing here is "--init-command" which will speed up the import progress 10 times fast.这里最重要的是“--init-command”,它可以将导入进度加快 10 倍。

只需编写docker ps并获取容器 id,然后编写以下内容;

docker exec -i your_container_id mysql -u root -p123456 your_db_name < /Users/your_pc/your_project_folder/backup.sql

我可以用这个命令导入

docker-compose exec -T mysql mysql -uroot -proot mydatabase < ~/Desktop/mydatabase_2019-10-05.sql

you can follow these simple steps:您可以按照以下简单步骤操作:

FIRST WAY :第一种方式:

first copy the SQL dump file from your local directory to the mysql container.首先将 SQL 转储文件从本地目录复制到 mysql 容器。 use docker cp command使用 docker cp 命令

docker cp [SRC-Local path to sql file] [container-name or container-id]:[DEST-path to copy to]

docker cp ./data.sql mysql-container:/home

and then execute the mysql-container using (NOTE: in case you are using alpine version you need to replace bash with sh in the given below command.)然后使用 mysql-container 执行(注意:如果您使用的是 alpine 版本,则需要在下面给出的命令中用 sh 替换 bash。)

docker exec -it -u root mysql-container bash

and then you can simply import this SQL dump file.然后你可以简单地导入这个 SQL 转储文件。

mysql [DB_NAME] < [SQL dump file path]

mysql movie_db < /home/data.sql

SECOND WAY : SIMPLE第二种方式:简单

docker cp ./data.sql mysql-container:/docker-entrypoint-initdb.d/

As mentioned in the mysql Docker hub official page.如 mysql Docker hub 官方页面所述。

Whenever a container starts for the first time, a new database is created with the specified name in MYSQL_DATABASE variable - which you can pass by setting up the environment variable see here how to set environment variables每当容器第一次启动时,都会在 MYSQL_DATABASE 变量中创建一个具有指定名称的新数据库 - 您可以通过设置环境变量来传递,请参阅此处如何设置环境变量

By default container will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d folder.默认情况下,容器将执行扩展名为 .sh、.sql 和 .sql.gz 的文件,这些文件位于 /docker-entrypoint-initdb.d 文件夹中。 Files will be executed in alphabetical order.文件将按字母顺序执行。 this way your SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.这样,您的 SQL 文件将默认导入到 MYSQL_DATABASE 变量指定的数据库中。

for more details you can always visit the official page有关更多详细信息,您可以随时访问官方页面

do docker cp file.sql <CONTAINER NAME>:/file.sql first先做docker cp file.sql <CONTAINER NAME>:/file.sql

then docker exec -it <CONTAINER NAME> mysql -u user -p然后docker exec -it <CONTAINER NAME> mysql -u user -p

then inside mysql container execute source \\file.sql然后在 mysql 容器内执行source \\file.sql

Trying "docker exec ... < data.sql" in Window PowerShell responses with:在 Window PowerShell 响应中尝试"docker exec ... < data.sql"

The '<' operator is reserved for future use. '<' 运算符保留供将来使用。

But one can wrap it out with cmd /c to eliminate the issue:但是可以用cmd /c来解决这个问题:

cmd /c "docker exec -i mysql-container mysql -uuser -ppassword name_db < data.sql"

You can run a container setting a shared directory (-v volume), and then run bash in that container.您可以运行设置共享目录(-v 卷)的容器,然后在该容器中运行 bash。 After this, you can interactively use mysql-client to execute the .sql file, from inside the container.在此之后,您可以交互地使用 mysql-client 从容器内部执行 .sql 文件。 obs: /my-host-dir/shared-dir is the .sql location in the host system. obs:/my-host-dir/shared-dir 是主机系统中的 .sql 位置。

docker run --detach --name=test-mysql -p host-port:container-port  --env="MYSQL_ROOT_PASSWORD=my-root-pswd" -v /my-host-dir/shared-dir:/container-dir mysql:latest


docker exec -it test-mysql bash

Inside the container...容器内...

mysql -p < /container-dir/file.sql 

Custom parameters:自定义参数:

  • test-mysql (container name) test-mysql (容器名称)
  • host-port and container-port主机端口容器端口
  • my-root-pswd (mysql root password) my-root-pswd (mysql 根密码)
  • /my-host-dir/shared-dir and /container-dir (the host directory that will be mounted in the container, and the container location of the shared directory) /my-host-dir/shared-dir/container-dir (将挂载到容器中的主机目录,以及共享目录的容器位置)

This one work for me这个对我有用

$ docker exec -i NAME_CONTAINER_MYSQL mysql -u DB_USER -pPASSWORD DATABASE < /path/to/your/file.sql

First if do you want to know what is the NAME_CONTAINER_MYSQL, you should use this command below :首先,如果您想知道 NAME_CONTAINER_MYSQL 是什么,您应该使用以下命令:

$ docker ps

In the output column NAME you will see the NAME_CONTAINER_MYSQL that do you need to replace in the command above.在输出列 NAME 中,您将看到需要在上面的命令中替换的 NAME_CONTAINER_MYSQL。

you can copy the export file for eg dump.sql using docker cp into the container and then import the db.您可以使用 docker cp 将例如 dump.sql 的导出文件复制到容器中,然后导入数据库。 if you need full instructions, let me know and I will provide如果您需要完整的说明,请告诉我,我会提供

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

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