MySQL通过Binlog恢复删除的表
2016-01-29 10:29
561 查看
查看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
;
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`
;
# 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`
;
# 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`))
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
;
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`
;
# 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`
;
# 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`))
相关文章推荐
- MySQL 日志 undo | redo
- MySQL主从切换
- MySQL主从故障修复
- MySQL不同库名相同表结构实现主从…
- mysqlsla 分析mysql慢查询日志
- 安装mysql 5.5.14 报错
- MySQL ibdata1撑爆占满磁盘空间
- MySQL查询阻塞语句
- MySQL 主键冲突,无法插入数据
- MySQL 通过mysql_config_edit…
- MySQL 库大小、表大小、索引…
- MySQL创建复合索引
- MySQL SHOW 语句大全
- Kill 所有MySQL进程
- MySQL 简洁连接数据库方式
- MySQL复制
- MySQL Server-id的作用
- mysql无法创建数据库
- MYSQL 配置slave故障
- mysql 配置主从