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

相关推荐
谢平康9 分钟前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发
IP老炮不瞎唠16 分钟前
Python 价格监控如何实现?思路与实用方法分享
运维·服务器·网络
GIS数据转换器19 分钟前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
睡不醒男孩0308231 小时前
CLup 6.x 版本中针对StarRocks 存算一体集群的完整操作手册
java·服务器·网络·clup
Tokai_Teio_11 小时前
第四届黄河流域 misc
运维·服务器
hj2862512 小时前
Linux 网络服务综合笔记(概念 + 命令 + 实操案例)2
linux·运维·网络
what_20182 小时前
Linux 磁盘 (查看、划分、inode)
linux·运维·服务器
ManageEngine卓豪2 小时前
从性能故障到安全风险,现代企业数字化转型下的网络丢包运维管控指南
运维·网络安全·网络故障·网络丢包
2739920292 小时前
GDB调试(Linux)
linux
凡人叶枫2 小时前
Effective C++ 条款23:宁以 non-member、non-friend 替换 member 函数
linux·开发语言·c++·嵌入式开发