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`))
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`))
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-1771728/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql 誤刪除表內資料,透過binlog日誌恢復MySql
- MySQL 通過 binlog 恢復資料MySql
- 通過flashback database恢復被刪除的表空間Database
- 通過binlog恢復mysql資料庫MySql資料庫
- 通過binlog恢復mysql備份之前的資料MySql
- MySQL資料庫表誤刪除恢復(一)MySql資料庫
- MySQL使用mysqldump+binlog完整恢復被刪除的資料庫(轉)MySql資料庫
- 通過MySQL relaylog + SQL_Thread 增量恢復binlogMySqlthread
- MySQL 透過 binlog 恢復資料MySql
- 【Mysql】如何透過binlog恢復資料MySql
- mysql binlog 刪除master- binlogMySqlAST
- DB2 恢復誤刪除的表DB2
- MySQL 如何重建/恢復刪除的 sys SchemaMySql
- 儲存過程誤刪除的恢復儲存過程
- mysql binlog日誌刪除MySql
- 【北亞資料恢復】誤刪除oracle表和誤刪除oracle表資料的資料恢復方法資料恢復Oracle
- flashback database 恢復誤刪除的表空間。Database
- 如何通過簡單的3步恢復Windows 7同時刪除UbuntuWindowsUbuntu
- 通過檔案控制程式碼恢復刪除的資料檔案
- ORACLE 10g 中恢復已刪除的表Oracle 10g
- 恢復刪除的檔案
- 刪除檔案的恢復
- 14、MySQL Case-線上表誤刪除恢復MySql
- MySQL 正確刪除 binlog 日誌MySql
- 通過拼碎片成功恢復伺服器被刪除資料案例分享伺服器
- mysql 利用binlog增量備份、恢復MySql
- MySQL超大表刪除資料過程MySql
- Git恢復刪除的檔案Git
- 恢復 Git 被刪除的分支Git
- Git恢復被刪除的分支Git
- 10g裡的閃回表命令-- 表的刪除和恢復
- 05、MySQL Case-MySQL binlog誤清除恢復MySql
- 行動硬碟刪除的檔案能恢復嗎,怎樣恢復刪除的檔案硬碟
- 基於mysql-binlog的恢復實驗MySql
- MySQL二進位制日誌刪除與恢復MySql
- mysql通過percona xtrabackup全備和mysql binlog實現基於時間點的資料庫恢復MySql資料庫
- OS 刪除oracle資料檔案恢復過程Oracle
- MySQL binlog超過binlog_expire_logs_seconds閾值沒有刪除案例MySql