如何将MySQL转储从主机还原到Docker容器 [英] How to restore MySQL dump from host to Docker container

查看:124
本文介绍了如何将MySQL转储从主机还原到Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这是重复的主题,但是我根本无法完成它:我想在运行时将数据库转储恢复到MySQL容器,而无需修改 docker-compose.yml 文件.

I'm sure this is a duplicated topic, but I simply cannot get it done: I like to restore my database dump to MySQL container in run time, without modifying the docker-compose.yml file.

Dockerfile

FROM php:5.4.45-apache
RUN apt-get update
RUN docker-php-ext-install mysql mysqli

docker-compose.yml

version: '2'
services:
  php_service:
    container_name: my_php
    # Use Dockerfile in this dir to build the image
    build: .
    # Stop containers always after exiting.
    restart: always
    ports:
      # 'localhost' does not works from the host, but the IP of docker itself
      # (192.168.99.100 for example - shown on the top of the console)
      - "80:80"
      - "443:443"
    environment:
      # Pass variables
      - API_TOKEN=xxxx
    volumes:
      # The current directory will be mounted to '/var/www/html'
      # WORKS ONLY IN USER'S DIR ON WINDOWS (~/Downloads for example)
      - .:/var/www/html
  # See https://hub.docker.com/_/mysql/ for additional information.
  # To open up console, run `docker exec -it my_mysql bash`.
  # To restore a dump `docker exec -i my_mysql /usr/bin/mysql -u root
  # --password=test_pass DATABASE < DUMP.sql` should work, but it never did.
  mysql_service:
    container_name: my_mysql
    # Use an existing image
    image: mysql:5.6
    restart: always
    ports:
      # Let it accessible for other apps (mysql on host, IDE, etc.)
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
      MYSQL_USER: 'test'
      MYSQL_PASS: 'pass'
    volumes:
      # Named volumes (my-datavolume) has to be listed in the "volumes"
      # section - I don't know how it works or what is it doing at all...
      # (-_-')
      - my-datavolume:/var/lib/mysql
volumes:
  my-datavolume:

复制步骤:

  • 在Windows 7主机上启动Docker Toolbox
  • docker-compose up
  • 打开一个新的Docker Toolbox终端
  • docker exec my_msql/usr/bin/mysql -u root --password = test_pass -e'CREATE DATABASE testdb;'
  • docker exec -i my_mysql/usr/bin/mysql -u root --password = test_pass testdb<dump_on_host.sql
  • docker exec -it my_mysql/usr/bin/mysql -u root --password = test_pass testdb
  • mysql>显示表格;

数据库为空.似乎它什么也没做,因为终端响应太快.我通过在主机上安装MySQL尝试了转储,可以将其还原.

The database is empty. It seems that it does nothing, because the terminal responds too quickly. I tried out the dump by installing MySQL on my host, it can be restored.

推荐答案

尝试以下命令对我来说很好.

Try below command works fine for me.

从docker主机上运行它.

Run it from docker host machine.

备份

docker exec容器/usr/bin/mysqldump -u root --password = root数据库backup.sql

docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql

还原
猫backup.sql |docker exec -i容器/usr/bin/mysql -u root--password =根数据库

Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE

如有任何问题,请通知我.

Please let me know in case any issue.

这篇关于如何将MySQL转储从主机还原到Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆