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

相关推荐
垂钓的小鱼121 分钟前
用关联图谱做量化选股:让基本面惊喜沿着网络扩散
服务器·php·apache
Lonely 净土7 小时前
Linux 运维文件写入操作
linux·运维·服务器·文件管理
城管不管7 小时前
重生——第十一次面试之挖财一面2026.8.19已OC
java·服务器·jvm·数据库·spring·面试·职场和发展
飞英思特无线充电7 小时前
严苛场景巡检机器人的无线补能设计:点位、BMS、认证与运维边界
运维·机器人
Jimmy_jimi8 小时前
利用nginx代理完成url重定位
运维·nginx
liuyicenysabel8 小时前
多服务上线日记三:
运维·服务器·笔记·学习
吠品9 小时前
Wine 在 Linux 上运行 Windows 软件完整指南
java·linux·服务器
一只鹿鹿鹿9 小时前
大数据中心安全系统解决方案(Word文件)
大数据·运维·物联网·安全·政务
深念Y12 小时前
从 Windows 迁移到 Linux 开发环境的记录
linux·windows·链接·bun·ram·imdisk
AuTumn-L12 小时前
使用 SSH 密钥认证实现安全免密登录
服务器·安全·ssh