[英]How to restore MySQL dump from host to Docker container
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.我确定这是一个重复的主题,但我根本无法完成它:我喜欢在运行时将我的数据库转储恢复到 MySQL 容器,而不修改docker-compose.yml文件。
Dockerfile Dockerfile
FROM php:5.4.45-apache
RUN apt-get update
RUN docker-php-ext-install mysql mysqli
docker-compose.yml 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:
Steps to reproduce:重现步骤:
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;
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.我通过在我的主机上安装 MySQL 来尝试转储,它可以恢复。
Try below command works fine for me.试试下面的命令对我来说很好。
Run it from docker host machine.从 docker 主机运行它。
Backup备份
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql docker exec 容器 /usr/bin/mysqldump -u root --password=root 数据库> backup.sql
Restore恢复
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
Please let me know in case any issue.如果有任何问题,请告诉我。
The documentation on dockerhub worked for me: https://hub.docker.com/_/mysql dockerhub 上的文档对我有用: https ://hub.docker.com/_/mysql
Backup备份
docker exec some-mysql sh -c 'exec mysqldump --all-databases -u<user> -p<password> <database>' > /some/path/on/your/host/all-databases.sql
Restore恢复
docker exec -i some-mysql sh -c 'exec mysql -u<user> -p<password> <database>' < /some/path/on/your/host/all-databases.sql
You are using a volume, that means that after you restore the dump, data will persist.您正在使用卷,这意味着在您恢复转储后,数据将保留。
You are also exposing the database in port 3306, so a solution might be to connect to the database through a client.您还在端口 3306 中公开数据库,因此解决方案可能是通过客户端连接到数据库。 You can connect to mysql with a graphical client like MySql Workbench and restore the database from there.您可以使用MySql Workbench之类的图形客户端连接到 mysql 并从那里恢复数据库。
Or if you have installed mysql in your command line或者,如果您在命令行中安装了 mysql
$ mysql --host=127.0.0.1 --port=3306 -u test -p testdb < dump_on_host.sql
ONE DOCKER DB TO ANOTHER DOCKER DB一个 Docker 数据库到另一个 Docker 数据库
Backup备份
docker exec containerid mysqldump -u root --password=root portal-db > lower-portal-db.sql
Restore恢复
cat lower-portal-db.sql | cat lower-portal-db.sql | docker exec -i containerid mysql -u root --password=root portal-db docker exec -i containerid mysql -u root --password=root portal-db
Try the following command试试下面的命令
sudo docker exec -i CONTAINER_ID sh -c 'exec mysql -u root -p[YourPassword] database_name' < /your/database/directory/database_name.sql; sudo docker exec -i CONTAINER_ID sh -c 'exec mysql -u root -p[YourPassword] database_name' < /your/database/directory/database_name.sql;
Per the latest instructions at https://hub.docker.com/_/mysql you can do this:根据https://hub.docker.com/_/mysql上的最新说明,您可以执行以下操作:
docker-compose exec -T mysql_service sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" testdb' < dump_on_host.sql
This has the nice benefit of not putting the password into your command history.这样做的好处是不会将密码放入您的命令历史记录中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.