MySQL备份还原方法1---mysqldump

4.mysqldump

该方法无需下载工具,redhat9系统自带

mysqldump 是 MySQL 运维中最基础、最常用的备份工具,适合中小型数据库的定期备份、迁移和开发测试。对于超大库(几百 GB 以上),性能会受影响,通常会搭配 mydumperxtrabackup 等工具使用。

4.1数据准备

这里之前有数据,就是用的school库和里面的Student表,里面数据不重要,随便写

bash 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| school             |#用到这个
| sys                |
+--------------------+
mysql> use school;

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| Student          |
| Teacher          |
+------------------+

4.2完全备份

bash 复制代码
#创建备份用到的目录
[root@bogon ~]# mkdir /mysql_backup/dump -p

#完全备份school库
[root@bogon ~]# mysqldump --opt -B school > /mysql_backup/dump/school.sql
[root@bogon ~]# ls /mysql_backup/dump/
school.sql

4.3增量备份

1.增加记录

2.模拟数据破坏

3.刷新日志

4.增量备份(看binlog文件里面)

​ 1基于时间 --start-datetime,--stop-datetime

​ 2基于位置 --start-position,--stop-position

bash 复制代码
mysql> INSERT INTO Student VALUES(5,'xumubin','男',29,'中文专业'),
    -> (6,'wangzhao','男',21,'导弹专业');
    
mysql> drop database school;

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      4487 | No        |
| binlog.000002 |       744 | No        |
| binlog.000003 |       158 | No        |
+---------------+-----------+-----------+

备份binlog文件

一般都是选择刷新日志后的 倒数第二个!!!

bash 复制代码
#手动备份 binlog 文件
mysql> system cp /var/lib/mysql/binlog.000002 /mysql_backup/dump/
[root@bogon ~]# cd /mysql_backup/dump/
[root@bogon dump]# ls
binlog.000002  school.sql

找时间点/位置

先定位到插入数据的大概位置

找到begin和commit,选择begin前和commit后的时间和位置点

bash 复制代码
260401 10:41:16[root@bogon dump]# mysqlbinlog binlog.000002  --base64-output=DECODE-ROWS -vv
......
# at 237
#260401 10:41:16 server id 1  end_log_pos 314 CRC32 0x27fdc597 	Query	thread_id=8	exec_time=0	error_code=0
SET TIMESTAMP=1775011276/*!*/;
SET @@session.pseudo_thread_id=8/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113696/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
# at 314
#260401 10:41:16 server id 1  end_log_pos 384 CRC32 0x2caeff05 	Table_map: `school`.`Student` mapped to number 89
# has_generated_invisible_primary_key=0
# at 384
#260401 10:41:16 server id 1  end_log_pos 482 CRC32 0xad982ff4 	Write_rows: table id 89 flags: STMT_END_F
### INSERT INTO `school`.`Student`
### SET
###   @1=5 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xumubin' /* VARSTRING(64) meta=64 nullable=0 is_null=0 */
###   @3='男' /* STRING(8) meta=65032 nullable=0 is_null=0 */
###   @4=29 /* TINYINT meta=0 nullable=0 is_null=0 */
###   @5='中文专业' /* VARSTRING(64) meta=64 nullable=1 is_null=0 */
### INSERT INTO `school`.`Student`
### SET
###   @1=6 /* INT meta=0 nullable=0 is_null=0 */
###   @2='wangzhao' /* VARSTRING(64) meta=64 nullable=0 is_null=0 */
###   @3='男' /* STRING(8) meta=65032 nullable=0 is_null=0 */
###   @4=21 /* TINYINT meta=0 nullable=0 is_null=0 */
###   @5='导弹专业' /* VARSTRING(64) meta=64 nullable=1 is_null=0 */
# at 482
#260401 10:41:16 server id 1  end_log_pos 513 CRC32 0x772bc368 	Xid = 58
COMMIT/*!*/;
# at 513
#260401 10:41:50
......

begin: 260401 10:41:16 2026-04-01 10:41:16 at 237

commit: 260401 10:41:50 2026-04-01 10:41:50 at 513

4.4完全还原

bash 复制代码
#模拟数据库破坏
mysql> drop database school;

#完全还原
[root@bogon dump]# mysql < school.sql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> select * from school.Student;
+----+--------+-----+-----+-----------------+
| no | name   | sex | age | dept            |
+----+--------+-----+-----+-----------------+
|  1 | 陆亚   | 男  |  24 | 计算机网络      |
|  2 | tom    | 男  |  26 | 英语            |
|  3 | 张阳   | 男  |  21 | 物流管理        |
|  4 | alex   | 女  |  22 | 电子商务        |
+----+--------+-----+-----+-----------------+
4 rows in set (0.00 sec)

可以看到没有后面插入的记录

4.5增量还原

4.5.1 基于时间

bash 复制代码
[root@bogon dump]# mysqlbinlog binlog.000002 --start-datetime="2026-04-01 10:41:16" --stop-datetime=" 2026-04-01 10:41:50" > time1.sql
[root@bogon dump]# ls
binlog.000002  school.sql  time1.sql

mysql> select * from school.Student;
+----+----------+-----+-----+-----------------+
| no | name     | sex | age | dept            |
+----+----------+-----+-----+-----------------+
|  1 | 陆亚     | 男  |  24 | 计算机网络      |
|  2 | tom      | 男  |  26 | 英语            |
|  3 | 张阳     | 男  |  21 | 物流管理        |
|  4 | alex     | 女  |  22 | 电子商务        |
|  5 | xumubin  | 男  |  29 | 中文专业        |
|  6 | wangzhao | 男  |  21 | 导弹专业        |
+----+----------+-----+-----+-----------------+
6 rows in set (0.00 sec)

有后面插入的数据,增量还原成功

4.5.2基于位置

bash 复制代码
#删除增量的部分
mysql> delete from school.Student where no=5 or no=6;
mysql> select * from school.Student;
+----+--------+-----+-----+-----------------+
| no | name   | sex | age | dept            |
+----+--------+-----+-----+-----------------+
|  1 | 陆亚   | 男  |  24 | 计算机网络      |
|  2 | tom    | 男  |  26 | 英语            |
|  3 | 张阳   | 男  |  21 | 物流管理        |
|  4 | alex   | 女  |  22 | 电子商务        |
+----+--------+-----+-----+-----------------+

#增量还原
[root@bogon dump]# mysqlbinlog binlog.000002 --start-position=237 --stop-position=513 > pos1.sql
[root@bogon dump]# ls
binlog.000002  pos1.sql  school.sql  time1.sql

mysql> source /mysql_backup/dump/pos1.sql;

mysql> select * from school.Student;
+----+----------+-----+-----+-----------------+
| no | name     | sex | age | dept            |
+----+----------+-----+-----+-----------------+
|  1 | 陆亚     | 男  |  24 | 计算机网络      |
|  2 | tom      | 男  |  26 | 英语            |
|  3 | 张阳     | 男  |  21 | 物流管理        |
|  4 | alex     | 女  |  22 | 电子商务        |
|  5 | xumubin  | 男  |  29 | 中文专业        |
|  6 | wangzhao | 男  |  21 | 导弹专业        |
+----+----------+-----+-----+-----------------+
相关推荐
麦聪聊数据2 小时前
企业数据流通与敏捷API交付实战(二):微服务取数与冗余CRUD
数据库·sql·低代码·微服务·restful
zfoo-framework2 小时前
minikube+docker desktop搭建k8s环境部署SpringBoot应用(仅仅是玩玩,端口映射很麻烦)
运维·docker·容器
果果燕2 小时前
ARM嵌入式学习(四)--- C语言应用:led、beep、key
linux·运维·算法
不愿透露姓名的大鹏2 小时前
SQL Server数据库的LDF文件过大的清理方式
数据库·sqlserver
Wyawsl2 小时前
MySQL高可用集群
数据库·mysql
尽兴-2 小时前
MySQL 与 Elasticsearch 数据一致性保障的四大主流方案
数据库·mysql·elasticsearch
以太浮标2 小时前
华为eNSP模拟器 - 设备及技术栈场景全维度解析
运维·网络·网络协议·网络安全·华为·负载均衡·信息与通信
天行健,君子而铎2 小时前
政务行业高准确率、可控、符合规范的数据库审计与监测实践方案
网络·数据库·政务
落羽的落羽2 小时前
【Linux系统】入门线程:线程介绍与线程控制
linux·服务器·c++·人工智能·stm32·单片机·机器学习