mysql根据bin log恢复_MySQL通过Binlog恢复删除的表

查看log-bin是否开启:

mysql> show variables like '%log%bin%';

+---------------------------------+-------+

| Variable_name                   | Value |

+---------------------------------+-------+

| log_bin                         | ON    |

| log_bin_trust_function_creators | OFF   |

| sql_log_bin                     | ON    |

+---------------------------------+-------+

3 rows in set (0.00 sec)

用sakila数据库测试:

mysql> use sakila;

Database changed

查看表内容:

mysql> select * from yoon;

+----+------+

| id | name |

+----+------+

|  1 | yoon |

|  7 | aaa  |

+----+------+

2 rows in set (0.00 sec)

查看日志信息:

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000025 |      932 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

mysql> drop table yoon;

Query OK, 0 rows affected (0.00 sec)

刷新日志:

mysql> flush logs;

Query OK, 0 rows affected (0.01 sec)

mysql> select * from yoon;

ERROR 1146 (42S02): Table 'sakila.yoon' doesn't exist

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000026 |      107 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

[root@yoon data]# ls

ibdata1  ib_logfile0  ib_logfile2  mysql-bin.000025  mysql-bin.index     rocover.sql  test

ibdata2  ib_logfile1  mysql        mysql-bin.000026  performance_schema  sakila

[root@yoon data]# mysqlbinlog mysql-bin.000025 | grep --ignore-case DROP -A3 -B4

/*!\C utf8 *//*!*/;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;

SET @@session.lc_time_names=0/*!*/;

SET @@session.collation_database=DEFAULT/*!*/;

DROP TABLE `yoon` /* generated by server */

/*!*/;

# at 215

#150814  3:34:55 server id 360360  end_log_pos 379      Query   thread_id=1     exec_time=3215  error_code=0

--

COMMIT/*!*/;

# at 932

#150814  4:42:00 server id 360360  end_log_pos 1040     Query   thread_id=1     exec_time=0     error_code=0

SET TIMESTAMP=1439541720/*!*/;

DROP TABLE `yoon` /* generated by server */

/*!*/;

# at 1040

#150814  4:42:10 server id 360360  end_log_pos 1083     Rotate to mysql-bin.000026  pos: 4

mysql> select from_unixtime('1439541720');

+-----------------------------+

| from_unixtime('1439541720') |

+-----------------------------+

| 2015-08-14 04:42:00         |

+-----------------------------+

1 row in set (0.00 sec)

###如果从上次备份刷新binlog,到发现表被删掉的过程中产生了多个binlog,则要按照binlog产生的顺序,那么恢复的次序应该是按照binglog的产生的序号,从小到大依次恢复###

[root@yoon data]# mysqlbinlog -d sakila --stop-datetime='2015-08-14 04:42:00' mysql-bin.000025 > recover_sakila.sql

[root@yoon data]# mysql -uroot -p < recover_sakila.sql

Enter password:

mysql> select * from yoon;

+----+------+

| id | name |

+----+------+

|  1 | yoon |

|  7 | aaa  |

+----+------+

2 rows in set (0.00 sec)

过滤方法:(因为测试中只有一个表,而生产环境中就会有多张表)

[root@yoon data]# more recover_sakila.sql | grep --ignore-case -E 'insert|update|select|delete' -A2 -B2 | grep yoon

如果表名包含yoon_log,yoon_order,只想导出yoon表的话,+个-w

[root@yoon data]# more recover_sakila.sql | grep --ignore-case -E 'insert' -A2 -B2 | grep -w yoon> yoon.sql

insert into yoon(name) values ('yoon')

insert into yoon(name) values ('aaa')

[root@yoon data]# more recover_sakila.sql | grep --ignore-case -E 'create' -A2 -B2 | grep yoon

create table yoon (id int(11) unsigned NOT NULL AUTO_INCREMENT,name varchar(20),PRIMARY KEY (`id`))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值