如何从主机恢复MySQL转储到Docker容器

问题描述:

我确定这是一个重复的主题,但我完全无法完成它:我喜欢在运行时将我的数据库转储恢复到MySQL容器,而无需修改泊坞窗-compose.yml文件。如何从主机恢复MySQL转储到Docker容器

Dockerfile

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

泊坞窗,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-compose up
  • 打开一个新的泊坞工具箱终端
  • 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> SHOW TABLES;

数据库是空的。它似乎什么都不做,因为终端反应太快。我通过在主机上安装MySQL来试用转储,它可以被恢复。

+0

你得到像'泊坞窗的exec -i my_mysql WC Thilo

+0

感谢您的建议 - 它表示输入为空('0 0 0')。 – bimlas

+0

假设文件真的存在,也许管道不起作用?试试'cat dump_on_host.sql |码头执行官...' – Thilo

尝试下面的命令适合我。

从码头主机运行它。

备份

搬运工EXEC CONTAINER的/ usr /斌/ mysqldump的-u根--password =根 DATABASE> backup.sql

还原
猫backup.sql |搬运工EXEC -i集装箱的/ usr/bin中/ MySQL的ü根 --password =根数据库

请让我知道的情况下的任何问题。

+0

请参阅开始消息的注释。 – bimlas

您正在使用卷,这意味着在您还原转储后,数据将持续存在。

您也在3306端口公开数据库,因此解决方案可能是通过客户端连接到数据库。您可以使用图形客户端连接到mysql,例如MySql Workbench,并从那里恢复数据库。

或者,如果你已经在你的命令行mysql安装

$ mysql --host=127.0.0.1 --port=3306 -u test -p testdb < dump_on_host.sql