MySQL 誤操作后數據恢復(update,delete忘加where條件)【轉】


在數據庫日常維護中,開發人員是最讓人頭痛的,很多時候都會由於SQL語句 寫的有問題導致服務器出問題,導致資源耗盡。最危險的操作就是在做DML操作的時候忘加where條件,導致全表更新,這是作為運維或者DBA的我們改如 何處理呢?下面我分別針對update和delete操作忘加where條件導致全表更新的處理方法。

一. update 忘加where條件誤操作恢復數據(binglog格式必須是ROW)
1.創建測試用的數據表
create table t1 (
id int unsigned not null auto_increment,
name char(20) not null,
sex enum('f','m') not null default 'm',
address varchar(30) not null,
primary key(id)
);
2.插入測試數據
insert into t1 (name,sex,address)values('daiiy','m','guangzhou');
insert into t1 (name,sex,address)values('tom','f','shanghai'); 
insert into t1 (name,sex,address)values('liany','m','beijing');
insert into t1 (name,sex,address)values('lilu','m','zhuhai');
3.現在需要將id等於2的用戶的地址改為zhuhai,update時沒有添加where條件
mysql>  select * from t1;
+----+-------+-----+-----------+
| id | name  | sex | address   |
+----+-------+-----+-----------+
|  1 | daiiy | m   | guangzhou |
|  2 | tom   | f   | shanghai  |
|  3 | liany | m   | beijing   |
|  4 | lilu  | m   | zhuhai    |
+----+-------+-----+-----------+
4 rows in set (0.00 sec)
mysql> update t1 set address='zhuhai';
mysql>  select * from t1;
+----+-------+-----+---------+
| id | name  | sex | address |
+----+-------+-----+---------+
|  1 | daiiy | m   | zhuhai  |
|  2 | tom   | f   | zhuhai  |
|  3 | liany | m   | zhuhai  |
|  4 | lilu  | m   | zhuhai  |
+----+-------+-----+---------+
4 rows in set (0.00 sec)
4.開始恢復,在線上的話,應該比較復雜,要先進行鎖表,以免數據再次被污染。(鎖表,查看正在寫哪個二進制日志)
mysql > lock tables t1 read ;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |     2309 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5.分析二進制日志,並且在其中找到相關記錄,在更新時是address='zhuhai',我們可以在日志中過濾出來。
[root@node1 binlog]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000005 | grep -B 15 -i --color 'zhuhai'
COMMIT/*!*/;
# at 1790
#160302 14:43:37 server id 1  end_log_pos 1862 CRC32 0xe5d422f5     Query    thread_id=44    exec_time=0    error_code=0
SET TIMESTAMP=1456901017/*!*/;
BEGIN
/*!*/;
# at 1862
#160302 14:43:37 server id 1  end_log_pos 1916 CRC32 0x39917b1c     Table_map: `arun`.`t1` mapped to number 156
# at 1916
#160302 14:43:37 server id 1  end_log_pos 1969 CRC32 0x31a19175     Write_rows: table id 156 flags: STMT_END_F
### INSERT INTO `arun`.`t1`
### SET
###   @1=4 /* INT meta=0 nullable=0 is_null=0 */
###   @2='lilu' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
--
/*!*/;
# at 2072
#160302 14:50:00 server id 1  end_log_pos 2126 CRC32 0xd2434753     Table_map: `arun`.`t1` mapped to number 156
# at 2126
#160302 14:50:00 server id 1  end_log_pos 2278 CRC32 0xfb36ef03     Update_rows: table id 156 flags: STMT_END_F
### UPDATE `arun`.`t1`
### WHERE
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='guangzhou' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### UPDATE `arun`.`t1`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=1 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='shanghai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### SET
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=1 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### UPDATE `arun`.`t1`
### WHERE
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='beijing' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### SET
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
 
可以看見里面記錄了每一行的變化,這也是binglog格式要一定是row才行的原因。其中@1,@2,@3,@4,分別對應表中 id,name,sex,address字段。相信大家看到這里有點明白了吧,對,沒錯,你猜到了,我們將相關記錄轉換為sql語句,重新導入數據庫。
6.處理分析處理的二進制日志
[root@node1 binlog]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin.000005 | sed -n '/# at 2126/,/COMMIT/p' > t1.txt 
[root@node1 binlog]# cat t1.txt
# at 2126
#160302 14:50:00 server id 1  end_log_pos 2278 CRC32 0xfb36ef03     Update_rows: table id 156 flags: STMT_END_F
### UPDATE `arun`.`t1`
### WHERE
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='guangzhou' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### SET
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### UPDATE `arun`.`t1`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=1 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='shanghai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### SET
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=1 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### UPDATE `arun`.`t1`
### WHERE
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='beijing' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### SET
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
# at 2278
#160302 14:50:00 server id 1  end_log_pos 2309 CRC32 0x58165775     Xid = 1466
COMMIT/*!*/;
這里sed有點復雜,需要童鞋們好好自己研究研究,這里我就不多說了。
[root@node1 binlog]# sed '/WHERE/{:a;N;/SET/!ba;s/\([^\n]*\)\n\(.*\)\n\(.*\)/\3\n\2\n\1/}' t1.txt |sed -r '/WHERE/{:a;N;/@4/!ba;s/###   @2.*//g}' | sed 's/### //g;s/\/\*.*/,/g' | sed '/WHERE/{:a;N;/@1/!ba;s/,/;/g};s/#.*//g;s/COMMIT,//g' | sed '/^$/d' > recover.sql
[root@node1 binlog]# cat recover.sql
UPDATE `arun`.`t1`
SET
  @1=1 ,
  @2='daiiy' ,
  @3=2 ,
  @4='guangzhou' ,
WHERE
  @1=1 ;
UPDATE `arun`.`t1`
SET
  @1=2 ,
  @2='tom' ,
  @3=1 ,
  @4='shanghai' ,
WHERE
  @1=2 ;
UPDATE `arun`.`t1`
SET
  @1=3 ,
  @2='liany' ,
  @3=2 ,
  @4='beijing' ,
WHERE
  @1=3 ;
將文件中的@1,@2,@3,@4替換為t1表中id,name,sex,address字段,並刪除最后字段的","號
 
[root@node1 binlog]# sed -i 's/@1/id/g;s/@2/name/g;s/@3/sex/g;s/@4/address/g' recover.sql
[root@node1 binlog]# sed -i -r 's/(address=.*),/\1/g' recover.sql
[root@node1 binlog]# cat recover.sql
UPDATE `arun`.`t1`
SET
  id=1 ,
  name='daiiy' ,
  sex=2 ,
  address='guangzhou'
WHERE
  id=1 ;
UPDATE `arun`.`t1`
SET
  id=2 ,
  name='tom' ,
  sex=1 ,
  address='shanghai'
WHERE
  id=2 ;
UPDATE `arun`.`t1`
SET
  id=3 ,
  name='liany' ,
  sex=2 ,
  address='beijing'
WHERE
  id=3 ;
7.到這里日志就處理好了,現在導入即可(導入數據后,解鎖表);
mysql> UNLOCK TABLES;
mysql> source /data/log/mysql/binlog/recover.sql;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0
可以看見數據已經完全恢復,這種方法的優點是快速,方便。
二. delete 忘加where條件誤刪除恢復(binglog格式必須是ROW)
其實這和update忘加條件差不多,不過這處理更簡單,這里就用上面那張表做測試吧
1.模擬誤刪除數據
mysql> select * from t1;
+----+-------+-----+-----------+
| id | name  | sex | address   |
+----+-------+-----+-----------+
|  1 | daiiy | m   | guangzhou |
|  2 | tom   | f   | shanghai  |
|  3 | liany | m   | beijing   |
|  4 | lilu  | m   | zhuhai    |
+----+-------+-----+-----------+
4 rows in set (0.00 sec)
 
mysql> delete from t1;
Query OK, 4 rows affected (0.02 sec)
 
mysql> select * from t1;
Empty set (0.00 sec)
2.在binglog中去查找相關記錄
 
[root@node1 binlog]# mysqlbinlog --no-defaults -v -v --base64-output=decode-rows mysql-bin.000005 |sed -n '/### DELETE FROM `arun`.`t1`/,/COMMIT/p'
### DELETE FROM `arun`.`t1`
### WHERE
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */
###   @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='guangzhou' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### DELETE FROM `arun`.`t1`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=1 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='shanghai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### DELETE FROM `arun`.`t1`
### WHERE
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
###   @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='beijing' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
### DELETE FROM `arun`.`t1`
### WHERE
###   @1=4 /* INT meta=0 nullable=0 is_null=0 */
###   @2='lilu' /* STRING(60) meta=65084 nullable=0 is_null=0 */
###   @3=2 /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */
###   @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */
# at 3244
#160302 16:33:31 server id 1  end_log_pos 3275 CRC32 0xed9c2a39     Xid = 1484
COMMIT/*!*/;
[root@node1 binlog]# mysqlbinlog --no-defaults -v -v --base64-output=decode-rows mysql-bin.000005 |sed -n '/### DELETE FROM `arun`.`t1`/,/COMMIT/p'  > delete.txt
3.將記錄轉換為SQL語句
[root@node1 binlog]# cat delete.txt | sed -n '/###/p' | sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;' | sed -r 's/(@4.*),/\1;/g' | sed 's/@[1-9]=//g' > t1.sql
[root@node1 binlog]# cat t1.sql
INSERT INTO `arun`.`t1`
SELECT
  1 ,
  'daiiy' ,
  2 ,
  'guangzhou' ;
INSERT INTO `arun`.`t1`
SELECT
  2 ,
  'tom' ,
  1 ,
  'shanghai' ;
INSERT INTO `arun`.`t1`
SELECT
  3 ,
  'liany' ,
  2 ,
  'beijing' ;
INSERT INTO `arun`.`t1`
SELECT
  4 ,
  'lilu' ,
  2 ,
  'zhuhai' ;
4.導入數據,驗證數據完整性
 
mysql> source /data/log/mysql/binlog/t1.sql;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
mysql> select * from t1;
+----+-------+-----+-----------+
| id | name  | sex | address   |
+----+-------+-----+-----------+
|  1 | daiiy | m   | guangzhou |
|  2 | tom   | f   | shanghai  |
|  3 | liany | m   | beijing   |
|  4 | lilu  | m   | zhuhai    |
+----+-------+-----+-----------+
4 rows in set (0.00 sec)
 

到這里數據就完整回來了。將binglog格式設置為row有利有弊,好處是記錄了每一行的實際變化,在主從復制時也不容易出問題。但是 由於記錄每行的變化,會占用大量磁盤,主從復制時帶寬占用會有所消耗。到底是使用row還是mixed,需要在實際工作中自己去衡量,但從整體上來 說,binglog的格式設置為row,都是不二的選擇。

總結:

所以在數據庫操作的過程中我們需要格外小心,當然開發那邊我們需要做好權限的控制,不過有一個參數可以解決我們的問題,讓我們不用擔心類似的問題發生:

在[mysql]段落開啟這個參數:

safe-updates
這樣當我們在做DML操作時忘記加where條件時,mysqld服務器是不會執行操作的:
還是剛才的表,執行delete操作==>
mysqldelete from t1;
ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
 
轉自
MySQL 誤操作后數據恢復(update,delete忘加where條件) - arun_yh - 博客園 http://www.cnblogs.com/itcomputer/articles/5234893.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM