mydumper备份数据库以及还原

mysqldumper官网地址:https://launchpad.net/mydumper

一、查看Linux版本

bash 复制代码
[root@client ~]# uname -a
Linux client 5.14.0-362.8.1.el9_3.x86_64 
#1 SMP PREEMPT_DYNAMIC Tue Oct 3 11:12:36 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux

二、下载相应版本的mysqldumper

https://github.com/mydumper/mydumper/releases/download/v0.21.3-1/mydumper-0.21.3-1.el9.x86_64.rpm

bash 复制代码
[root@client ~]# wget https://github.com/mydumper/mydumper/releases/download/v0.21.3-1/mydumper-0.21.3-1.el9.x86_64.rpm

-rw-r--r--. 1 root root  3429450 Mar 28 19:34 mydumper-0.21.3-1.el9.x86_64.rpm

三、安装rpm包

bash 复制代码
[root@client ~]# dnf install mydumper-0.21.3-1.el9.x86_64.rpm -y
bash 复制代码
[root@client ~]# rpm -qal | grep mydumper
/etc/mydumper.cnf
/usr/bin/mydumper
/usr/share/man/man1/mydumper.1.gz

四、数据准备

sql 复制代码
-- 创建库
mysql> create database school;
Query OK, 1 row affected (0.01 sec)
 
-- 选择库
mysql> use school;
Database changed
 
-- 创建表
mysql> CREATE TABLE `Teacher` (
    `id` int(11) NOT NULL COMMENT '编号',
    `name` varchar(15) NOT NULL COMMENT '姓名',
    PRIMARY KEY (`id`)
);
 
mysql> CREATE TABLE `Student` (
    `no` int(10) NOT NULL COMMENT '学号',
    `name` varchar(16) NOT NULL COMMENT '姓名',
    `sex` char(2) NOT NULL COMMENT '性别',
    `age` tinyint(2) NOT NULL DEFAULT '0' COMMENT '学生年龄',
    `dept` varchar(16) DEFAULT 'NULL' COMMENT '学生所在系别',
    PRIMARY KEY (`no`)
);
Query OK, 0 rows affected, 2 warnings (0.09 sec)
 
-- 插入数据
mysql> INSERT INTO `Teacher` VAlUES (1, '李老师'), (2, '梁老师'), (3, '张老师'), (4, '王老师');
 
mysql> INSERT INTO `Student` VALUES (1, '陆亚', '男', 24, '计算机网络'),
(2, 'tom', '男', 26, '英语'),
(3, '张阳', '男', 21, '物流管理'),
(4, 'alex', '女', 22, '电子商务');
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0

五、备份数据

1、备份全部数据库

复制代码
[root@client backup]# mydumper -u root -p 'MySQL@123' -r 100000 -o /backup/all/

** (mydumper:621559): WARNING **: 17:17:29.019: Using --trx-tables options, binlog coordinates will not be accurate if you are writing to non transactional tables.

2、备份全部数据库,包含触发器、事件、存储过程及函数

复制代码
[root@client backup]# mydumper -u root -p 'MySQL@123' -G -R -E -r 100000 -o /backup/all2/

** (mydumper:632300): WARNING **: 17:18:56.596: Using --trx-tables options, binlog coordinates will not be accurate if you are writing to non transactional tables.

3、备份指定数据库

复制代码
[root@client backup]# mydumper -u root -p 'MySQL@123' -G -R -E -r 100000 -B school -o /backup/school/

** (mydumper:638909): WARNING **: 17:19:50.840: Using --trx-tables options, binlog coordinates will not be accurate if you are writing to non transactional tables.

六、myloader 还原数据

1、完全还原

sql 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| school_tables      |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database school;
mysql> drop database school_tables

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |     |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
sql 复制代码
[root@client backup]# myloader -u root -p 'MySQL@123' -o -d /backup/all3

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| school_tables      |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

2、还原指定数据库

sql 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| school_tables      |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database school;
Query OK, 2 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school_tables      |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
sql 复制代码
[root@client backup]# myloader -u root -p 'MySQL@123' -o -d /backup/school

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| school_tables      |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

七、总结

深入掌握了mysqldumper的使用方法,以及有时需要结合binlog来实现增量备份(此文章没有做)。

相关推荐
RisunJan7 小时前
Linux命令-skill(发送信号给进程)
linux·运维·服务器
潘正翔7 小时前
k8s基础_kubeadm搭建k8s集群
linux·运维·docker·云原生·容器·kubernetes
changjiahong7 小时前
lvs总结
服务器·数据库·lvs
Echo flower7 小时前
Docker 容器中 Puppeteer 僵尸进程排查与修复
运维·docker·容器·puppeteer
骊城英雄7 小时前
彩笔运维勇闯机器学习--逻辑回归
运维·机器学习·逻辑回归
孪生质数-8 小时前
AI 应用实践篇——让大模型真正开始工作
linux·运维·服务器·人工智能·深度学习·语言模型·llama
Splashtop高性能远程控制软件8 小时前
连锁零售多门店远程运维指南:从故障响应到批量运维的四项能力
运维·零售·远程控制·splashtop
2301_777998348 小时前
Linux线程同步与互斥(三):信号量与环形队列实现生产者消费者模型
linux·c语言·c++
瞬间&永恒~8 小时前
【MySQL】 InnoDB 锁等待排查与并发压测实验
运维·数据库·mysql
zcmodeltech8 小时前
智能矿井沙盘模型多系统协同控制系统设计:基于STM32与Modbus RTU的感知-传输-控制一体化方案
服务器·数据库·分布式·stm32·单片机·嵌入式硬件