我确信这是一个重复的主题,但我就是做不到:我喜欢在运行时将数据库转储恢复到MySQL容器,而不修改 * docker-compose. yml * 文件。
- 停靠文件**
FROM php:5.4.45-apache
RUN apt-get update
RUN docker-php-ext-install mysql mysqli
- 停靠-编写. 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工具箱
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> SHOW TABLES;
数据库是空的。看起来它什么也没做,因为终端响应太快了。我在我的主机上安装MySQL尝试了转储,它可以恢复。
6条答案
按热度按时间fjaof16o1#
尝试下面的命令对我来说很好。
从Docker主机运行。
备份
还原
如有任何问题,请通知我。
e4eetjau2#
dockerhub上的文档对我很有效:https://hub.docker.com/_/mysql
waxmsbnn3#
您正在使用卷,这意味着在恢复转储后,数据将持续存在。
您还将在端口3306中公开数据库,因此解决方案可能是通过客户端连接到数据库,您可以使用图形客户端(如MySql Workbench)连接到mysql并从那里恢复数据库。
或者如果您已经在命令行中安装了mysql
tuwxkamq4#
一个DOCKER DB到另一个DOCKER DB
cat低层门户数据库. sql|docker exec-i容器ID mysql-u根目录--密码=根目录portal-db
vq8itlhq5#
根据https://hub.docker.com/_/mysql上的最新说明,您可以执行以下操作:
这样做的好处是不用将密码放入命令历史记录中。
x4shl7ld6#
尝试以下命令