mysql增量恢复的一个实例操作

通过防火墙禁止web等应用向主库写数据或者锁表,让主库暂时停止更新,然后进行恢复

模拟整个场景

1、登录数据库

1
2
3
4
[root@promote 3306]# mysql -uroot -S /data/3306/mysql.sock
 
创建一个库,并创建一个表,里面适当的加入一些初始数据
insert into nima(id,name) values(4,'hahh');

2、将我本机的时间设置为早上0点 

1
2
[root@promote ~]# date -s '2017/7/27'
20170727日 星期四 00:00:00 CST

3、完全备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@promote 3306]# mysqldump -uroot -S /data/3306/mysql.sock -F -B --master-data=2 test >/opt/test.sql
 
[root@promote 3306]# ll
总用量 36
drwxr-xr-x. 5 mysql mysql 4096 7月  20 15:15 data
-rw-r--r--. 1 root  root  1897 7月  18 20:11 my.cnf
-rwx------. 1 root  root  1299 7月  18 20:29 mysql
-rw-rw----. 1 mysql mysql 1047 7月  27 00:02 mysql-bin.000002
-------------------------------------------------------------------------------备份点  //最好提前看一下从哪里开始备份的,就是查看新的bin-log
-rw-rw----. 1 mysql mysql  107 7月  27 00:02 mysql-bin.000003    --------------新内容
-rw-rw----. 1 mysql mysql   56 7月  27 00:02 mysql-bin.index
-rw-rw----. 1 mysql mysql    6 7月  20 15:15 mysqld.pid
-rw-r-----. 1 mysql root  4184 7月  20 15:15 mysql_oldboy3306.err
srwxrwxrwx. 1 mysql mysql    0 7月  20 15:15 mysql.sock

4、在创建的表中加入新的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
没加入数据前
mysql> select * from nima;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 尼玛   |
| NULL | hahh   |
|    3 | hahh   |
|    4 | hahh   |
+------+--------+
4 rows in set (0.00 sec)
 
 
 
mysql> insert into nima(id,name) values(100,'hahh');
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into nima(id,name) values(101,'hahh');
 
mysql> select * from nima;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 尼玛   |
| NULL | hahh   |
|    3 | hahh   |
|    4 | hahh   |
100 | hahh   |
101 | hahh   |
+------+--------+
6 rows in set (0.00 sec)

5、删除创建的数据库

1
2
3
4
5
6
7
8
9
10
11
12
mysql> drop database test;
Query OK, 1 row affected (0.07 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

6、此时就出现了问题,test库没有了,然后去查看bin-log  

1
2
3
4
5
6
7
8
9
10
11
12
[root@promote 3306]# mysqlbinlog mysql-bin.000003
 
# at 511
#170727  0:08:56 server id 1  end_log_pos 592   Query   thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1501085336/*!*/;
drop database test   <==发现了是此条语句的问题
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

7、此时刷新一下bin-log

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@promote 3306]# mysqladmin -uroot -S /data/3306/mysql.sock flush-logs
[root@promote 3306]# ll
总用量 40
drwxr-xr-x. 4 mysql mysql 4096 7月  27 00:08 data
-rw-r--r--. 1 root  root  1897 7月  18 20:11 my.cnf
-rwx------. 1 root  root  1299 7月  18 20:29 mysql
-rw-rw----. 1 mysql mysql 1047 7月  27 00:02 mysql-bin.000002
-rw-rw----. 1 mysql mysql  635 7月  27 00:18 mysql-bin.000003
---------------------------------------------------------------------------需要恢复03的内容,然后04就是新内容
-rw-rw----. 1 mysql mysql  107 7月  27 00:18 mysql-bin.000004
-rw-rw----. 1 mysql mysql   84 7月  27 00:18 mysql-bin.index
-rw-rw----. 1 mysql mysql    6 7月  20 15:15 mysqld.pid
-rw-r-----. 1 mysql root  4184 7月  20 15:15 mysql_oldboy3306.err
srwxrwxrwx. 1 mysql mysql    0 7月  20 15:15 mysql.sock

8、开始恢复手续

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@promote 3306]# cp mysql-bin.000003 /tmp
[root@promote 3306]# cd /tmp
[root@promote tmp]# ll
总用量 4
-rw-r-----. 1 root root 635 7月  27 00:20 mysql-bin.000003
drwx------. 2 root root  24 6月  22 13:45 ssh-sq615X9ctBqx
drwx------. 3 root root  16 6月  22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-colord.service-FFHLPi
drwx------. 3 root root  16 6月  22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-cups.service-jm0Csy
drwx------. 3 root root  16 6月  22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-rtkit-daemon.service-90fMAb
drwx------. 3 root root  16 6月  22 13:44 systemd-private-d13e6e6b8a2d4dc6a5a816c2cfd8a0be-vmtoolsd.service-HfIX2Y
[root@promote tmp]# mysqlbinlog -d test mysql-bin.000003 >bin.sql
[root@promote tmp]# vim bin.sql //删除有问题的语句
 
[root@promote tmp]# mysql -uroot -S /data/3306/mysql.sock < /opt/test.sql   //恢复全备
[root@promote tmp]# mysql -uroot -S /data/3306/mysql.sock test <bin.sql     //恢复增量
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
 
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select * from nima;
+------+--------+
| id   | name   |
+------+--------+
|    1 | 尼玛   |
| NULL | hahh   |
|    3 | hahh   |
|    4 | hahh   |
100 | hahh   |
101 | hahh   |
+------+--------+
6 rows in set (0.00 sec)
 
mysql>
恢复结束

  

 

posted @   JvvYou  阅读(1080)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示