简体   繁体   English

如何从 Docker 容器中恢复 MySQL 数据

[英]How to recover MySQL data from Docker container

I was running mariadb instance on docker windows toolkit.我在 docker windows 工具包上运行 mariadb 实例。 I did a env vaiable change on the mariaDB container using kitematic.我使用 kitematic 对 mariaDB 容器进行了环境变量更改。 Now it has recreated an instance loosing all my database.现在它重新创建了一个丢失我所有数据库的实例。 Is there a way to recover from this?有没有办法从中恢复?

Checked if threre are dangling volumes, and there are few检查是否有 threre 悬空卷,并且很少

docker volume ls -f dangling=true docker 音量 ls -f dangling=true

Got the data recovered using the dangling volumes.使用悬空卷恢复数据。 Approach is as following.方法如下。

First get the list of dangling volumes.首先获取悬空卷的列表。

$ docker volume ls -f dangling=true
DRIVER              VOLUME NAME
local               6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19
local               47bb077ef6f6df9f56bd30c35eeb45f35e62213d2c50db6f078bfdeeee6698ec

Then mounted it on to a Ubuntu container (so that you can go inside the directory and check what is there, as there is no other way to do this when you are using Docker Tool Box on windows)然后将其安装到 Ubuntu 容器上(这样您就可以在目录中使用 go 并检查其中的内容,因为在 Windows 上使用 ZC5FD214CDD0D2B3B4272E73B022 工具箱时没有其他方法可以做到这一点)

$ docker run --name tempContainer1-UBUNTU -v 6f79b6329d98379495a284287e32a7a57605483dd7bf7fa19924fb2a98fb1d19:/var/lib/backup -t -i ubuntu /bin/bash

Then you will be inside the bash of newly created contianer.然后您将在新创建的 contianer 的 bash 内部。 Go to newly mounted directory and check content Go 到新挂载的目录并查看内容

$cd /var/lib/backup
$ls
$aria_log.00000001  aria_log_control  ib_buffer_pool  ib_logfile0  ib_logfile1  ibdata1  ibtmp1  multi-master.info  mysql  performance_schema
-- once you are sure directory data is what you require, make a zip file of the folder 
$apt-get update
$apt-get install zip
$cd ..
$zip -r backup.zip backup

On another terminal from host copy the content of container backup.zip to host在主机的另一个终端上,将容器备份的内容.zip 复制到主机

$docker cp tempContainer1-UBUNTU:/var/lib/backup.zip .

Then create a docker compose file like following and mount the backup folder as data directory.然后创建一个 docker 组合文件,如下所示,并将备份文件夹挂载为数据目录。 Run this on linux host as this mounting will not work as expected for mysql on windows.在 linux 主机上运行此命令,因为对于 windows 上的 mysql,此安装将无法正常工作。

version: "3.2"
services:
  mysql:
    image: mariadb:10.4.12
    restart: always
    ports:
      - "3306:3306"      
    command: mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON
    volumes:
            - ./backup_data_folder:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: somepassword
      TZ: Asia/Singapore
    networks:
      - frontend
    container_name: maria
networks:
  frontend:

Start开始

$docker-compose up

Once it is up, from another terminal go inside newly created container一旦启动,从新创建的容器内的另一个终端 go

$docker exec -t -i maria /bin/bash
-- Take dump of all the DBS 
$mysqldump -u root -p --all-databases > alldb.sql

Copy content of the dump to host from another terminal from host将转储的内容从主机的另一个终端复制到主机

$docker cp maria:/alldb.sql .

Now this sql file is a full dump, restore it as usual on your mysql DB or contianer.现在这个 sql 文件是一个完整的转储文件,像往常一样在 mysql DB 或 contianer 上恢复它。

mysql -u root -p < alldb.sql 

Recently I had to face the same problem for a lost wordpress container and I've followed the instructions from Don.最近,由于丢失的 wordpress 容器,我不得不面对同样的问题,我已经按照 Don 的说明进行操作。 However, as there were lots of dangling volumes, I had to optimize the process.然而,由于有很多悬空卷,我不得不优化这个过程。 I've managed a way to do it simpler, in the same terminal, resulting in the following steps:我已经设法在同一个终端中更简单地完成它,从而产生以下步骤:

docker volume ls -f dangling=true

DRIVER              VOLUME NAME
local               43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e
local               4fde3ea412e54a1b8b42fce6dae5de5135b9fd12933b020b43bd482cd5fd2225
local               52074ccfd62fb83b8b40cff5f8024215b34f79ad09b630e067ec732f811f798c
...

Then, for each container execute the following instruction, replacing 43277666c8bc3d... with each VOLUME NAME found.然后,对于每个容器执行以下指令,将43277666c8bc3d...替换为找到的每个 VOLUME NAME。 This instruction will remove previously maria-restore containers if they exist, create a new one and attach to it:该指令将删除以前maria-restore容器(如果存在),创建一个新容器并附加到它:

docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore; docker run --name maria-restore -v 43277666c8bc3da0b585d90952c2303226c92c6c6a561007c0c7ee00b6da817e:/var/lib/mysql -d mariadb:10.4.12 mysqld --innodb-flush-method=littlesync --innodb-use-native-aio=ON --log_bin=ON && docker exec -it maria-restore bash

If it wasn't a mysql volume, it will fail and exit immediately.如果它不是 mysql 卷,它将失败并立即退出。 If it is a mysql volume, you'll be inside the mariadb container.如果它是 mysql 卷,您将位于 mariadb 容器内。 The database will be already started.数据库将已经启动。 You can then connect to the database to see if it is the right one and backup it:然后,您可以连接到数据库以查看它是否正确并备份它:

root@8b35c8e2c474:/# mysql -uadmin -p
root@8b35c8e2c474:/# mysqldump -uadmin -p --all-databases > alldb.sql
root@8b35c8e2c474:/# exit

Copy the backed up database:复制备份的数据库:

docker cp mysql-restore:/alldb.sql .

Finally you'll have to clean up the maria-restore container:最后,您必须清理maria-restore容器:

docker container ls -a -q --filter "name=maria-restore" && docker container rm -f maria-restore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM