MySQL之delete 忘加where條件誤刪除恢複方法二

來源:互聯網
上載者:User

標籤: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條件誤刪除恢複方法二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.