標籤:delete忘加where條件誤刪除恢複
和昨天介紹的MySQL之delete 忘加where條件誤刪除恢複的mysql的環境條件是一樣的:
mysql資料庫指定字元集位utf8,同時表的字元集也得為utf8,同時mysql要開啟row模式的bin-log日誌
建立一張測試表測試:
create table MyClass( id int(4) not null primary key auto_increment, name char(20) not null, sex varchar(10) not null default ‘0‘, degree varchar(10) not null );
插入資料:
insert into myclass (id,name,sex,degree) values (21,‘小王‘,‘男‘,‘學士‘);insert into myclass (id,name,sex,degree) values (22,‘小花‘,‘女‘,‘學士‘);insert into myclass (id,name,sex,degree) values (23,‘小李‘,‘女‘,‘碩士‘);insert into myclass (id,name,sex,degree) values (24,‘王雪‘,‘女‘,‘碩士‘);insert into myclass (id,name,sex,degree) values (25,‘小強‘,‘男‘,‘博士‘);MySQL [zixun3]> select * from myclass;+----+--------+-----+--------+| id | name | sex | degree |+----+--------+-----+--------+| 20 | 張三 | 男 | 學士 || 21 | 小王 | 男 | 學士 || 22 | 小花 | 女 | 學士 || 23 | 小李 | 女 | 碩士 || 24 | 王雪 | 女 | 碩士 || 25 | 小強 | 男 | 博士 |+----+--------+-----+--------+6 rows in set (0.00 sec)
查看binlog位置點:
MySQL [zixun3]> show master status\G*************************** 1. row *************************** File: mysql-bin.000037 Position: 1803 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
從二級制binlog記錄檔中匯出DELETE誤操作的sql語句
/usr/local/mysql/bin/mysqlbinlog --no-defaults --base64-output=decode-rows -v -v /data/mysql/data/mysql-bin.000037|sed -n ‘/### DELETE FROM `zixun3`.`myclass`/,/COMMIT/p‘ >/tmp/delete.txt
sql語句內容如下:
[[email protected] ~]# cat /tmp/delete.txt ### DELETE FROM `zixun3`.`myclass`### WHERE### @1=20 /* INT meta=0 nullable=0 is_null=0 */### @2=‘張三‘ /* STRING(60) meta=65084 nullable=0 is_null=0 */### @3=‘男‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### @4=‘學士‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### DELETE FROM `zixun3`.`myclass`### WHERE### @1=21 /* INT meta=0 nullable=0 is_null=0 */### @2=‘小王‘ /* STRING(60) meta=65084 nullable=0 is_null=0 */### @3=‘男‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### @4=‘學士‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### DELETE FROM `zixun3`.`myclass`### WHERE### @1=22 /* INT meta=0 nullable=0 is_null=0 */### @2=‘小花‘ /* STRING(60) meta=65084 nullable=0 is_null=0 */### @3=‘女‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### @4=‘學士‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### DELETE FROM `zixun3`.`myclass`### WHERE### @1=23 /* INT meta=0 nullable=0 is_null=0 */### @2=‘小李‘ /* STRING(60) meta=65084 nullable=0 is_null=0 */### @3=‘女‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### @4=‘碩士‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### DELETE FROM `zixun3`.`myclass`### WHERE### @1=24 /* INT meta=0 nullable=0 is_null=0 */### @2=‘王雪‘ /* STRING(60) meta=65084 nullable=0 is_null=0 */### @3=‘女‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### @4=‘碩士‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### DELETE FROM `zixun3`.`myclass`### WHERE### @1=25 /* INT meta=0 nullable=0 is_null=0 */### @2=‘小強‘ /* STRING(60) meta=65084 nullable=0 is_null=0 */### @3=‘男‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */### @4=‘博士‘ /* VARSTRING(30) meta=30 nullable=0 is_null=0 */# at 428#180517 18:30:08 server id 1 end_log_pos 459 CRC32 0x29e84ce6 Xid = 8COMMIT/*!*/;
然後通過sed命令替換字串進行拼接的方式,把原來的delete語句轉換成insert 語句
cat /tmp/delete.txt | sed -n ‘/###/p‘ | sed ‘s/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;‘ | sed -r ‘s/(@5.*),/\1;/g‘ | sed ‘s/@[1-5]=//g‘ > /tmp/test.txt[[email protected] ~]# cat /tmp/test.txt INSERT INTO `zixun3`.`myclass`SELECT 20 , ‘張三‘ , ‘男‘ , ‘學士‘ ;INSERT INTO `zixun3`.`myclass`SELECT 21 , ‘小王‘ , ‘男‘ , ‘學士‘ ;INSERT INTO `zixun3`.`myclass`SELECT 22 , ‘小花‘ , ‘女‘ , ‘學士‘ ;INSERT INTO `zixun3`.`myclass`SELECT 23 , ‘小李‘ , ‘女‘ , ‘碩士‘ ;INSERT INTO `zixun3`.`myclass`SELECT 24 , ‘王雪‘ , ‘女‘ , ‘碩士‘ ;INSERT INTO `zixun3`.`myclass`SELECT 25 , ‘小強‘ , ‘男‘ , ‘博士‘ ;
登入mysql匯入資料
use zixun3;直接匯入 source /tmp/test.txtMySQL [zixun3]> select * from myclass;+----+--------+-----+--------+| id | name | sex | degree |+----+--------+-----+--------+| 20 | 張三 | 男 | 學士 || 21 | 小王 | 男 | 學士 || 22 | 小花 | 女 | 學士 || 23 | 小李 | 女 | 碩士 || 24 | 王雪 | 女 | 碩士 || 25 | 小強 | 男 | 博士 |+----+--------+-----+--------+6 rows in set (0.00 sec)
當然此種sed命令拼接的方式也是一種思路,但是這種方式的恢複,也只適合表結構欄位都是基本的簡單的欄位的表進行恢複資料,並不適合複雜的表結構的資料的恢複
MySQL之delete 忘加where條件誤刪除恢複方法二