不小心把数据库删了,要赶紧跑路吗???

 

一不小心把公司生产环境数据库删了,而数据库备份只停留到昨天,要丢失一天的生产环境数据?需要赶紧收拾东西跑路吗?

壮士留步,你可以通过数据库历史备份+Binarylog恢复完整数据

mysql会把所有表结构变更以及表数据修改的操作记录到一个二进制日志文件中,也就是我们今天要说的Binarylog(二进制日志),简称binlog。binlog日志是以事件的形式记录对mysql的操作,还记录各个操作消耗的时间

接下来我们通过一个例子演示如何通过binlog恢复被删除的数据

mysql> create database shifou;
Query OK, 1 row affected (0.00 sec)

mysql> use shifou;
Database changed

mysql> create table orders(
    ->        id int auto_increment not null primary key, 
    ->        goods_id int,
    ->        count int 
    -> );
Query OK, 0 rows affected (0.01 sec)


往orders表中插入数据

mysql> insert into orders  values (1, 10, 3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into orders  values (2, 11, 8);
Query OK, 1 row affected (0.00 sec)


手一抖,把数据库给删了

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


这个时候我们看到binlog文件里已经有内容了

mysql> show master logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      1234 | No        |
+---------------+-----------+-----------+
1 row in set (0.00 sec)


我们来看一下binlog 里边的具体内容,可以看到我们刚才对数据库的所有操作都以event的形式记录到了binlog中

mysql> show binlog events in 'binlog.000001';
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                              |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| binlog.000001 |    4 | Format_desc    |         1 |         125 | Server ver: 8.0.21, Binlog ver: 4                                                                                                                                 |
| binlog.000001 |  125 | Previous_gtids |         1 |         156 |                                                                                                                                                                   |
| binlog.000001 |  156 | Anonymous_Gtid |         1 |         233 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                              |
| binlog.000001 |  233 | Query          |         1 |         347 | create database shifou /* xid=35 */                                                                                                                               |
| binlog.000001 |  347 | Anonymous_Gtid |         1 |         426 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                              |
| binlog.000001 |  426 | Query          |         1 |         652 | use `shifou`; create table orders(
       id int auto_increment not null primary key, 
       goods_id int,
       count int 
) /* xid=40 */                      |
| binlog.000001 |  652 | Anonymous_Gtid |         1 |         731 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                              |
| binlog.000001 |  731 | Query          |         1 |         808 | BEGIN                                                                                                                                                             |
| binlog.000001 |  808 | Table_map      |         1 |         864 | table_id: 121 (shifou.orders)                                                                                                                                     |
| binlog.000001 |  864 | Write_rows     |         1 |         912 | table_id: 121 flags: STMT_END_F                                                                                                                                   |
| binlog.000001 |  912 | Xid            |         1 |         943 | COMMIT /* xid=47 */                                                                                                                                               |
| binlog.000001 |  943 | Anonymous_Gtid |         1 |        1022 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                              |
| binlog.000001 | 1022 | Query          |         1 |        1099 | BEGIN                                                                                                                                                             |
| binlog.000001 | 1099 | Table_map      |         1 |        1155 | table_id: 121 (shifou.orders)                                                                                                                                     |
| binlog.000001 | 1155 | Write_rows     |         1 |        1203 | table_id: 121 flags: STMT_END_F                                                                                                                                   |
| binlog.000001 | 1203 | Xid            |         1 |        1234 | COMMIT /* xid=49 */                                                                                                                                               |
| binlog.000001 | 1234 | Anonymous_Gtid |         1 |        1311 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                              |
| binlog.000001 | 1311 | Query          |         1 |        1421 | drop database shifou /* xid=51 */                                                                                                                                 |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
18 rows in set (0.00 sec)


binlog 日志中的最后一个操作event就是我们执行删库的操作,所以我们在恢复数据的时候,要略过这个event。


mysqlbinlog 命令可以通过指定事件位置号来恢复具体的事件操作
接下来我们用mysqlbinlog命令来恢被删除的数据库,从binlog文件中可以看出,刚开始的事件位置编号是4,删库前的事件位置编号是1234,所以我们恢复事件4-1234之间的数据操作:

$ mysqlbinlog --start-position=4 --stop-position=1234 /usr/local/var/mysql/binlog.000001 | mysql -uroot -p


执行成功之后我们再看下数据:

mysql> select * from orders;
+------------------+------------------------+---------------------+
|        id        |        goods_id        |        count        |
+------------------+------------------------+---------------------+
|                1 |                     10 |                   3 |
|                2 |                     11 |                   8 |
+------------------+------------------------+---------------------+
2 rows in set (0.00 sec)


good!被删除的数据库又回来了。好了,不用着急跑路,继续安心的写代码吧

下篇文章中我们会详细介绍下binlog的更多用法和配置,以及binlog中event的实现原理

只有干货,没有鸡汤。点关注不迷路

想要交流其他开发技术,可关注我微信公众号 全栈师否:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值