Bootstrap

MySQL备份案例: mysqldump+binlog实现完全+增量备份

一、素材准备

1. 创建一个干净环境的虚拟机 -- 192.168.46.120 openEuler 2

2. linux 在线安装MySQL

        a. 查看本地MySQL安装列表,选择安装版本

[root@openEuler ~]# yum list | grep -i "^mysql"
mysql.src                                               8.0.40-1.oe2203sp4                                   update-source
mysql.x86_64                                            8.0.40-1.oe2203sp4                                   update
mysql-common.x86_64                                     8.0.40-1.oe2203sp4                                   update
mysql-config.x86_64                                     8.0.40-1.oe2203sp4                                   update
mysql-connector-java.noarch                             1:8.0.30-1.oe2203sp4                                 everything
mysql-connector-java.src                                1:8.0.30-1.oe2203sp4                                 source
mysql-debuginfo.x86_64                                  8.0.40-1.oe2203sp4                                   update
mysql-debugsource.x86_64                                8.0.40-1.oe2203sp4                                   update
mysql-devel.x86_64                                      8.0.40-1.oe2203sp4                                   update
mysql-errmsg.x86_64                                     8.0.40-1.oe2203sp4                                   update
mysql-help.x86_64                                       8.0.40-1.oe2203sp4                                   update
mysql-libs.x86_64                                       8.0.40-1.oe2203sp4                                   update
mysql-selinux.noarch                                    1.0.0-2.oe2203sp4                                    everything
mysql-selinux.src                                       1.0.0-2.oe2203sp4                                    source
mysql-server.x86_64                                     8.0.40-1.oe2203sp4                                   update
mysql-test.x86_64                                       8.0.40-1.oe2203sp4                                   update
mysql5.src                                              5.7.44-1.oe2203sp4                                   source
mysql5.x86_64                                           5.7.44-1.oe2203sp4                                   everything
mysql5-common.x86_64                                    5.7.44-1.oe2203sp4                                   everything
mysql5-debuginfo.x86_64                                 5.7.44-1.oe2203sp4                                   debuginfo
mysql5-debugsource.x86_64                               5.7.44-1.oe2203sp4                                   debuginfo
mysql5-devel.x86_64                                     5.7.44-1.oe2203sp4                                   everything
mysql5-embedded.x86_64                                  5.7.44-1.oe2203sp4                                   everything
mysql5-embedded-devel.x86_64                            5.7.44-1.oe2203sp4                                   everything
mysql5-errmsg.x86_64                                    5.7.44-1.oe2203sp4                                   everything
mysql5-libs.x86_64                                      5.7.44-1.oe2203sp4                                   everything
mysql5-server.x86_64                                    5.7.44-1.oe2203sp4                                   everything
mysql5-test.x86_64                                      5.7.44-1.oe2203sp4                                   everything

         选择 mysql-server.x86_64   进行安装。

        b. 进行安装

[root@openEuler ~]# dnf install -y mysql-server.x86_64

         安装成功!!!

        c. 启动该服务,登录即可

[root@openEuler ~]# systemctl start mysqld.service
[root@openEuler ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.40 Source distribution

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 3. 在MySQL里进行创建数据库、表及数据的操作 

mysql> create database school;
Query OK, 1 row affected (0.01 sec)

mysql> use school
Database changed
mysql> CREATE TABLE `Student` (
    ->   `Sno` int(10) NOT NULL COMMENT '学号',  `Sname` varchar(16) NOT NULL COMMENT '姓名',
    ->   `Ssex` char(2) NOT NULL COMMENT '性别',  `Sage` tinyint(2) NOT NULL DEFAULT '0' COMMENT '学生年龄',
    ->   `Sdept` varchar(16) DEFAULT 'NULL' COMMENT '学生所在系别',  PRIMARY KEY (`Sno`)
    -> ) ;
Query OK, 0 rows affected, 2 warnings (0.09 sec)

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. 创建文件夹mysqlbak,用来存放实验的备份。

[root@openEuler ~]# mkdir mysqlbak
[root@openEuler ~]# ls
anaconda-ks.cfg  mysqlbak
[root@openEuler ~]# cd mysqlbak/
[root@openEuler mysqlbak]# ^C
[root@openEuler mysqlbak]#

2. 完全备份 --- 对school里的数据进行完全备份。

[root@openEuler mysqlbak]# mysqldump --opt -B school > school.sql
[root@openEuler mysqlbak]# ls
school.sql

3. 插入数据

mysql> INSERT INTO Student values(0005,'xumubin','男',29,'中文专业'),(0006,'wangzhao',' 男',21,'导弹专业');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

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

mysql>

4. 查看binlog是否开启

# 方法一
[root@openEuler ~]# ll /var/lib/mysql
-rw-r----- 1 mysql mysql     1591 Jan 16 13:43  binlog.000001 --- 表示已经开启
# 方法二
mysql> show variables like "%log%";
| log_bin                                        | ON                                       |
| log_bin_basename                               | /var/lib/mysql/binlog                    |

5. 模拟数据损坏

mysql> drop database school;
Query OK, 1 row affected (0.02 sec)

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

mysql>

6. 刷新日志并保存 

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

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      1822 | No        |
| binlog.000002 |       157 | No        |
+---------------+-----------+-----------+
2 rows in set (0.00 sec)

mysql>
# 拷贝,即日志保存,防止日志再次受到破坏。-- 完全备份
# 方法一
[root@openEuler mysqlbak]# cp /var/lib/mysql/binlog.000001 .
[root@openEuler mysqlbak]# ls
binlog.000001  school.sql
[root@openEuler mysqlbak]#



# 方法二
mysql> system cp /var/lib/mysql/binlog.000001 /mysqlbak

7. 查看二进制日志

使用mysqlbinlog进行查看
	#注意5.7版本,insert语句已经加密,默认看不到,查看时加上选项 --base64-output=DECODE-ROWS -vv

[root@openEuler mysqlbak]# mysqlbinlog binlog.000001 --base64-output=DECODE-ROWS -vv

8. 还原数据

        a. 基于时间点增量恢复

                

[root@openEuler mysqlbak]# mysqlbinlog binlog.000001 --start-datetime="2025-01-16 11:57:32" --stop-datetime="2025-01-16 12:00:08" > time1.sql
[root@openEuler mysqlbak]# ls
binlog.000001  school.sql  time1.sql
[root@openEuler mysqlbak]#


#还原完全备份
# 方法一
[root@openEuler mysqlbak]# mysql < school.sql
# 方法二
mysql> source /mysqlbak/school.sql
mysql> select * from school.Student;
+-----+--------+------+------+-----------------+
| Sno | Sname  | Ssex | Sage | Sdept           |
+-----+--------+------+------+-----------------+
|   1 | 陆亚   | 男   |   24 | 计算机网络      |
|   2 | tom    | 男   |   26 | 英语            |
|   3 | 张阳   | 男   |   21 | 物流管理        |
|   4 | alex   | 女   |   22 | 电子商务        |
+-----+--------+------+------+-----------------+
4 rows in set (0.01 sec)

还原增量备份
mysql> source /mysqlbak/time1.sql
mysql> select * from school.Student;
+-----+----------+------+------+-----------------+
| Sno | Sname    | Ssex | Sage | Sdept           |
+-----+----------+------+------+-----------------+
|   1 | 陆亚     | 男   |   24 | 计算机网络      |
|   2 | tom      | 男   |   26 | 英语            |
|   3 | 张阳     | 男   |   21 | 物流管理        |
|   4 | alex     | 女   |   22 | 电子商务        |
|   5 | xumubin  | 男   |   29 | 中文专业        |
|   6 | wangzhao | 男   |   21 | 导弹专业        |
+-----+----------+------+------+-----------------+
6 rows in set (0.00 sec)

        b. 基于位置点恢复

[root@openEuler mysqlbak]# mysqlbinlog binlog.000001 --start-position=1236 --stop-position=1591 -r pos1.sql
[root@openEuler mysqlbak]# ls
binlog.000001  pos1.sql  school.sql  time1.sql
[root@openEuler mysqlbak]#
[root@openEuler mysqlbak]# mysql < school.sql
[root@openEuler mysqlbak]# mysql < pos1.sql

实验到此结束!!!

;