2017-10-05 95 views
0

我確定這是一個重複的主題,但我完全無法完成它:我喜歡在運行時將我的數據庫轉儲恢復到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

回答

1

嘗試下面的命令適合我。

從碼頭主機運行它。

備份

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

還原
貓backup.sql |搬運工EXEC -i集裝箱的/ usr/bin中/ MySQL的ü根 --password =根數據庫

請讓我知道的情況下的任何問題。

+0

請參閱開始消息的註釋。 – bimlas

0

您正在使用卷,這意味着在您還原轉儲後,數據將持續存在。

您也在3306端口公開數據庫,因此解決方案可能是通過客戶端連接到數據庫。您可以使用圖形客戶端連接到mysql,例如MySql Workbench,並從那裏恢復數據庫。

或者,如果你已經在你的命令行mysql安裝

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