MySQL之delete 忘加where条件误删除恢复

一、mysql环境介绍:

mysql数据库指定字符集位utf8,同时表的字符集也得为utf8,同时mysql要开启row模式的bin-log日志

/etc/my.cnf文件字符集参数设置:

[root@git-server ~]# grep character-set /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
[mysqld]
port = 3306
socket = /tmp/mysql.sock
character-set-server = utf8

同时要求表的字符集也是utf8的

MySQL [zixun3]> show create table  zixun3.zx_scores\G
*************************** 1. row ***************************
       Table: zx_scores
Create Table: CREATE TABLE `zx_scores` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `titles` char(15) NOT NULL,
  `icon` smallint(6) unsigned DEFAULT '0',
  `integral` int(10) NOT NULL DEFAULT '0',
  `isdefault` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `integral` (`integral`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

如果表的字符集不是uft8的话要修改表的字符集位uft8,否则在mysqlbinlog 解析出来的sql文件对于中文汉字的会出现乱码,导致最后恢复到线上的表中报错

修改表的字符集位utf8:
MySQL [zixun3]> alter table zx_scores convert to character set utf8;

二、事故处理:

2018-05-16开发在delete删除数据时忘记加条件,直接执行delete from zx_scores ,导致此表中的数据全部删除

MySQL [zixun3]> delete from zx_scores;
Query OK, 6 rows affected (0.00 sec)

MySQL [zixun3]> select * from zx_scores;
Empty set (0.00 sec)

以下是恢复误删数据的过程:

首先要确定delete误操作写进了那个binlog二进制日志文件里面,然后从这个二进制日志中恢复相关行操作。因为启用的是row行格式,所有的每条记录

的修改等等操作都会记录在二进制日志里面。

MySQL [zixun3]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000036
         Position: 7620
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

MySQL [zixun3]> 

如果没有其他人flush logs的话,误操作就记录在mysql_bin.000036这个二进制日志文件中。如果有他人flush logs。就往以上找。直到发现误操作的记录。
开始恢复,在线上的话,应该比较复杂,要先进行锁表,以免数据再次被污染。(锁表,查看正在写哪个二进制日志)

mysql> lock table zixun3.zx_scores read;
Query OK, 0 rows affected (0.00 sec)
MySQL [zixun3]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000036
         Position: 7620
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

MySQL [zixun3]> 

把binlog日志 中2018年05月16日的delete语句提取出来 供恢复数据

/usr/local/mysql/bin/mysqlbinlog  --no-defaults  --base64-output=decode-rows  -v -v -v /data/mysql/data/mysql-bin.000036 >/tmp/info.txt
cat /tmp/info.txt |awk -F '[/*]+' '{print $1}' >/tmp/info1.txt

经过上述2个命令处理,得到的最终的binlog日志文件的sql为如下

[```
root@git-server ~]# cat /tmp/info1.txt

DELIMITER
#at 4
#180516 23:15:16 server id 1 end_log_pos 120 CRC32 0x183f4334 Start: binlog v 4, server v 5.6.36-log created 180516 23:15:16 at startup
#Warning: this binlog is either in use or was not closed properly.
ROLLBACK
#at 120
#180516 23:15:39 server id 1 end_log_pos 194 CRC32 0x0a6c78ee Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1526483739
SET @@session.pseudo_thread_id=1
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1
SET @@session.sql_mode=1075838976
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1

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
BEGIN

#at 194
#180516 23:15:39 server id 1 end_log_pos 254 CRC32 0x10198556 Table_map: zixun3.zx_scores mapped to number 70
#at 254
#180516 23:15:39 server id 1 end_log_pos 517 CRC32 0x59f30b1c Delete_rows: table id 70 flags: STMT_END_F

DELETE FROM zixun3.zx_scores

WHERE

@1=15

@2='大将'

@3=12

@4=29000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=12

@2='上将'

@3=11

@4=24000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=11

@2='中将'

@3=10

@4=19000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=10

@2='少将'

@3=9

@4=14000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=9

@2='上校'

@3=8

@4=9000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=8

@2='中校'

@3=7

@4=6000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=7

@2='少校'

@3=6

@4=5000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=6

@2='上尉'

@3=5

@4=4000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=5

@2='中尉'

@3=4

@4=3000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=4

@2='少尉'

@3=3

@4=2000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=3

@2='班长'

@3=2

@4=1000

@5=1

DELETE FROM zixun3.zx_scores

WHERE

@1=2

@2='列兵'

@3=1

@4=0

@5=1

#at 517
#180516 23:15:39 server id 1 end_log_pos 592 CRC32 0x36661206 Query thread_id=1 exec_time=0 error_code=0
SET TIMESTAMP=1526483739
COMMIT

DELIMITER ;
#End of log file
ROLLBACK


三、已删除数据的提取脚本

[root@git-server ~]# cat /tmp/test.sh 
#!/bin/bash

#bl表列数(表字段) #语句yj
bl=5
yj=DELETE
zs1=`awk '/#180516/,/#180517/ {print $0}' /tmp/info1.txt|awk '"/$yj/",/# at/ {print $0}'|grep ^###|grep "@"|cut -d"=" -f2`
zs2=`echo $zs1|awk '{print NF}'`
zt=`echo "$zs2/$bl"|bc`
hs=0
##databa指数据库  ###tab指表 ###a1--a5指的是此表一共有5个字段
databa=zixun3
tab=zx_scores
ztt=$(($zt+0))
ii=0

a1=1
a2=2
a3=3
a4=4
a5=5
  while [[ $ii -lt $ztt ]];do
l1=`echo $zs1|awk '{print $'"$a1"'}'`
l2=`echo $zs1|awk '{print $'"$a2"'}'`
l3=`echo $zs1|awk '{print $'"$a3"'}'`
l4=`echo $zs1|awk '{print $'"$a4"'}'`
echo "use $databa;insert into $tab values($l1,$l2,$l3,$l4,$l5)" >>/tmp/hf

a1=$(($a1+$bl))
a2=$(($a2+$bl))
a3=$(($a3+$bl))
a4=$(($a4+$bl))
a5=$(($a5+$bl))
ii=$(($ii+1));
  done

四、执脚本提取数据成功

[root@git-server ~]# sh /tmp/test.sh 
[root@git-server ~]# cat /tmp/hf 
use zizun3;insert into zx_scores values(15,'大将',12,29000,1);
use zizun3;insert into zx_scores values(12,'上将',11,24000,1);
use zizun3;insert into zx_scores values(11,'中将',10,19000,1);
use zizun3;insert into zx_scores values(10,'少将',9,14000,1);
use zizun3;insert into zx_scores values(9,'上校',8,9000,1);
use zizun3;insert into zx_scores values(8,'中校',7,6000,1);
use zizun3;insert into zx_scores values(7,'少校',6,5000,1);
use zizun3;insert into zx_scores values(6,'上尉',5,4000,1);
use zizun3;insert into zx_scores values(5,'中尉',4,3000,1);
use zizun3;insert into zx_scores values(4,'少尉',3,2000,1);
use zizun3;insert into zx_scores values(3,'班长',2,1000,1);
use zizun3;insert into zx_scores values(2,'列兵',1,0,1);

五、恢复数据到数据库中

MySQL [zixun3]> insert into zx_scores values(12,'上将',11,24000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(11,'中将',10,19000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(10,'少将',9,14000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(9,'上校',8,9000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(8,'中校',7,6000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(7,'少校',6,5000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(6,'上尉',5,4000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(5,'中尉',4,3000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(4,'少尉',3,2000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(3,'班长',2,1000,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> insert into zx_scores values(2,'列兵',1,0,1);
Query OK, 1 row affected (0.00 sec)

MySQL [zixun3]> select * from zx_scores ;
+----+--------+------+----------+-----------+
| id | titles | icon | integral | isdefault |
+----+--------+------+----------+-----------+
|  2 | 列兵   |    1 |        0 |         1 |
|  3 | 班长   |    2 |     1000 |         1 |
|  4 | 少尉   |    3 |     2000 |         1 |
|  5 | 中尉   |    4 |     3000 |         1 |
|  6 | 上尉   |    5 |     4000 |         1 |
|  7 | 少校   |    6 |     5000 |         1 |
|  8 | 中校   |    7 |     6000 |         1 |
|  9 | 上校   |    8 |     9000 |         1 |
| 10 | 少将   |    9 |    14000 |         1 |
| 11 | 中将   |   10 |    19000 |         1 |
| 12 | 上将   |   11 |    24000 |         1 |
| 15 | 大将   |   12 |    29000 |         1 |
+----+--------+------+----------+-----------+
12 rows in set (0.00 sec)

到此处删除的数据已经恢复完成。
提示:经过多次测试,此脚本在恢复数据时,对数据库的表结构是有要求的 ,并不是误删了任何的表结构中的数据来采用此脚本都可以恢复的,所以此脚本具有很大的局限性。
自脚本只能是恢复简单的表字段的表中被误删的数据

猜你喜欢

转载自blog.51cto.com/wujianwei/2117249