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

编程入门 行业动态 更新时间:2024-10-25 22:32:50
本文介绍了如何将 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 hub.docker/_/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
  • 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数据库 >备份.sql

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

恢复猫备份.sql |docker exec -i CONTAINER/usr/bin/mysql -u root--password=root 数据库

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 容器

本文发布于:2023-10-20 03:32:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   容器   主机   MySQL   Docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!