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来实现增量备份(此文章没有做)。

相关推荐
新*39 分钟前
ES 7.17 三节点 Docker 集群的自动备份:从 NFS 被拦住到用 ACL 根治权限
运维·elasticsearch·docker·slm·快照备份
Decay__1 小时前
PostgreSQL DBA 日常运维手册 —— 会话、锁阻塞、慢查询、膨胀与每日巡检
运维·数据库·sql·postgresql
落羽的落羽1 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
木子欢儿1 小时前
Ubuntu 26.04 LTS 安装Python 3.12
linux·运维·服务器·python·ubuntu
时间的拾荒人3 小时前
Linux 库的制作与使用(下):链接、加载与动态链接原理
linux·运维·动态库·静态库
FlightYe3 小时前
音视频修炼之容器与协议(六):HTTP 协议
android·linux·网络·网络协议·算法·http·音视频
如夏倾城3 小时前
网络安全态势感知全解析:概念、架构、厂商与旁路/串联接入选型
大数据·运维·网络·web安全·架构·知识图谱
分布式存储与RustFS3 小时前
RAG 索引每次都扫本地目录:文档放进 RustFS 桶后,只重建变化的文件
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
qq_452396233 小时前
第十七篇:《RocketMQ vs Kafka vs Pulsar:存储、延迟、生态与运维成本的完整对比矩阵》
运维·kafka·rocketmq
Decay__5 小时前
PostgreSQL 安装部署实战 —— PGDG yum 源安装、initdb 初始化与 postgresql.conf 参数调优
运维·数据库·sql·postgresql