MySqlは、誤って削除されたテーブルまたはデータを復元します(不完全なリカバリ)

7558 ワード

一:実験目的
mysqldumpフルスペアおよびバイナリ・ログを使用して、誤って削除されたテーブルまたはデータをリカバリします(不完全なリカバリ)
二:実験手順
2.1:テストデータの準備
use test;
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t              |
| t3             |
| t4             |
| t5             |
+----------------+
4 rows in set (0.00 sec)

mysql> create table t6(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t6 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t6 values(2);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> delete
    -> from t6
    -> where id=1;
Query OK, 1 row affected (0.00 sec)

mysql> insert into t6 values(3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t6 values(4);
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select *
    -> from t6;
+------+
| id   |
+------+
|    2 |
|    3 |
|    4 |
+------+
3 rows in set (0.00 sec)
mysql> drop table t6;
Query OK, 0 rows affected (0.01 sec)

mysql> select *
    -> from t6;
ERROR 1146 (42S02): Table 'test.t6' doesn't exist

2.2:リカバリ
--ここでは、t 6テーブルの値が2,3,4と削除された値が1のデータを復元したいと思います.
2.2.1:最新のフル・スペア・リカバリ
[root@target_pc ~]# mysql -u root -p test< /backup/databasefile/database_test_201410081042.bak
Enter password: 

--         :
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> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t              |
| t3             |
| t4             |
| t5             |
+----------------+
4 rows in set (0.00 sec)
  ,      t6。


 
2.2.2:バイナリ・ログによるリカバリ
mysql> show binary logs;
+----------------+-----------+
| Log_name       | File_size |
+----------------+-----------+
| log-bin.000006 |       120 |
| log-bin.000007 |      9609 |
| log-bin.000008 |       120 |
| log-bin.000009 |      4904 |
+----------------+-----------+
4 rows in set (0.00 sec)
すべてはlog-binです.000007までに行うので、ここではバイナリログファイルlog-binを復元する必要がある.000007,log-bin.000008,log-bin.000009
--表t 6の情報はlog-bin.000009里
2.2.2.1:まずlog-binを回復する.000007,log-bin.000008
[root@target_pc ~]# mysqlbinlog /var/lib/mysql/log-bin.000007 | mysql -u root -p
Enter password:  
[root@target_pc ~]# mysqlbinlog /var/lib/mysql/log-bin.000008 | mysql -u root -p
Enter password: 
2.2.2.2:log-binを回復する.000009
2.2.2.1:log-binを表示する.000009具体的な内容
mysql>show binlog events in 'log-bin.000009';
--ここには、t 6テーブルについてのみ表示されます.
+----------------+------+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------+
| Log_name       | Pos  | Event_type  | Server_id | End_log_pos | Info                                                                                               |
+----------------+------+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------+
 
| log-bin.000009 | 4599 | Query       |         1 |        4696 | use `test`; create table t6(id int)                                                                |
| log-bin.000009 | 4696 | Query       |         1 |        4775 | BEGIN                                                                                              |
| log-bin.000009 | 4775 | Query       |         1 |        4873 | use `test`; insert into t6 values(1)                                                               |
| log-bin.000009 | 4873 | Xid         |         1 |        4904 | COMMIT /* xid=202 */                                                                               |
| log-bin.000009 | 4904 | Query       |         1 |        4983 | BEGIN                                                                                              |
| log-bin.000009 | 4983 | Query       |         1 |        5081 | use `test`; insert into t6 values(2)                                                               |
| log-bin.000009 | 5081 | Xid         |         1 |        5112 | COMMIT /* xid=210 */                                                                               |
| log-bin.000009 | 5112 | Query       |         1 |        5191 | BEGIN                                                                                              |
| log-bin.000009 | 5191 | Query       |         1 |        5290 | use `test`; delete
from t6
where id=1                                                              |
| log-bin.000009 | 5290 | Xid         |         1 |        5321 | COMMIT /* xid=212 */                                                                               |
| log-bin.000009 | 5321 | Query       |         1 |        5400 | BEGIN                                                                                              |
| log-bin.000009 | 5400 | Query       |         1 |        5498 | use `test`; insert into t6 values(3)                                                               |
| log-bin.000009 | 5498 | Xid         |         1 |        5529 | COMMIT /* xid=213 */                                                                               |
| log-bin.000009 | 5529 | Query       |         1 |        5608 | BEGIN                                                                                              |
| log-bin.000009 | 5608 | Query       |         1 |        5706 | use `test`; insert into t6 values(4)                                                               |
| log-bin.000009 | 5706 | Xid         |         1 |        5737 | COMMIT /* xid=214 */                                                                               |  
| log-bin.000009 | 5737 | Query       |         1 |        5852 | use `test`; DROP TABLE `t6` /* generated by server */                                              |
+----------------+------+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------+
64 rows in set (0.00 sec)
 
2.2.2.2:t 6テーブルを復元してデータを削除する前のデータ
[root@target_pc ~]# mysqlbinlog /var/lib/mysql/log-bin.000009  --stop-position='5112' | mysql -u root -p
Enter password: 
このとき,t 6には1,2個のデータがある
mysql> select *
    -> from t6;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)
2.2.2.2.3:削除文をスキップし、残りのいくつかの挿入文を回復する
[root@target_pc ~]# mysqlbinlog /var/lib/mysql/log-bin.000009 --start-position='5400' --stop-position='5737' | mysql -u root -p
Enter password: 
この場合:
mysql> select *
    -> from t6;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+
4 rows in set (0.00 sec)
2.2.2.4:残ったのはdrop table文で、回復しない
drop table後に別のデータベースDML操作があれば、リカバリを続行すればよい.
--この記事では、以下を参照してください.http://wenku.baidu.com/link?url=-oWg11CKbEy0jvRec1IxLg4eiR2jRkZRY7cKxkakCSOkGEZqbX5nmG7om4IMZCi-CAE48jah4s_mZNf433roacNu-a61EMxQlFqL5wsfl67