MySQL 误操作后数据恢复(update,delete忘加where条件)
在数据库日常维护中,开发人员是最让人头痛的,很多时候都会由于SQL语句写的有问题导致服务器出问题,导致资源耗尽。最危险的操作就是在做DML操作的时候忘加where条件,导致全表更新,这是作为运维或者DBA的我们改如何处理呢?下面我分别针对update和delete操作忘加where条件导致全表更新的处理方法。
一. update 忘加where条件误操作恢复数据(binglog格式必须是ROW)
1.创建测试用的数据表
mysql> 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) -> ); Query OK, 0 rows affected (0.31 sec) mysql>
2.插入测试数据
mysql> insert into t1 (name,sex,address)values('daiiy','m','guangzhou'); Query OK, row affected (0.01 sec) mysql> insert into t1 (name,sex,address)values('tom','f','shanghai'); Query OK, row affected (0.00 sec) mysql> insert into t1 (name,sex,address)values('liany','m','beijing'); Query OK, row affected (0.00 sec) mysql> insert into t1 (name,sex,address)values('lilu','m','zhuhai'); Query OK, row affected (0.05 sec) mysql>
3.现在需要将id等于2的用户的地址改为zhuhai,update时没有添加where条件
mysql> select * from t1; +----+-------+-----+-----------+ | id | name | sex | address | +----+-------+-----+-----------+ | daiiy | m | guangzhou | | tom | f | shanghai | | liany | m | beijing | | lilu | m | zhuhai | +----+-------+-----+-----------+ rows in set (0.01 sec) mysql> update t1 set address='zhuhai'; Query OK, rows affected (0.09 sec) Rows matched: Changed: Warnings: mysql> select * from t1; +----+-------+-----+---------+ | id | name | sex | address | +----+-------+-----+---------+ | daiiy | m | zhuhai | | tom | f | zhuhai | | liany | m | zhuhai | | lilu | m | zhuhai | +----+-------+-----+---------+ rows in set (0.00 sec) mysql>
4.开始恢复,在线上的话,应该比较复杂,要先进行锁表,以免数据再次被污染。(锁表,查看正在写哪个二进制日志)
mysql> lock tables t1 read ; Query OK, rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | | | +------------------+----------+--------------+------------------+ row in set (0.00 sec) mysql>
5.分析二进制日志,并且在其中找到相关记录,在更新时是address='zhuhai',我们可以在日志中过滤出来。
[root@localhost mysql]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin. | 'zhuhai'
# at # at # :: server id end_log_pos Table_map: `db01`.`t1` mapped # :: server id end_log_pos Update_rows: flags: STMT_END_F ### UPDATE db01.t1 ### WHERE ### /* INT meta=0 nullable=0 is_null=0 */ ### @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @4='guangzhou' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### SET ### /* INT meta=0 nullable=0 is_null=0 */ ### @2='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### UPDATE db01.t1 ### WHERE ### /* INT meta=0 nullable=0 is_null=0 */ ### @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @4='shanghai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### SET ### /* INT meta=0 nullable=0 is_null=0 */ ### @2='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @4='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### UPDATE db01.t1 ### WHERE ### /* INT meta=0 nullable=0 is_null=0 */ ### @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @4='beijing' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### SET ### /* INT meta=0 nullable=0 is_null=0 */ ### @2='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### /* 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@localhost mysql]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql-bin. | sed -n '/# at 1679/,/COMMIT/p' > t1.txt 注意: 如果是gtid的方式需要加 skip-gtids=true 参数 [root@localhost mysql]# cat t1.txt # at # :: server end_log_pos Table_map: `db01`.`t1` mapped to number # :: server end_log_pos Update_rows: table flags: STMT_END_F ### UPDATE db01.t1 ### WHERE ### @= /* INT meta=0 nullable=0 is_null=0 */ ### @='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### @= /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @='guangzhou' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### SET ### @= /* INT meta=0 nullable=0 is_null=0 */ ### @='daiiy' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### @= /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### UPDATE db01.t1 ### WHERE ### @= /* INT meta=0 nullable=0 is_null=0 */ ### @='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### @= /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @='shanghai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### SET ### @= /* INT meta=0 nullable=0 is_null=0 */ ### @='tom' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### @= /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### UPDATE db01.t1 ### WHERE ### @= /* INT meta=0 nullable=0 is_null=0 */ ### @='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### @= /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @='beijing' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ ### SET ### @= /* INT meta=0 nullable=0 is_null=0 */ ### @='liany' /* STRING(60) meta=65084 nullable=0 is_null=0 */ ### @= /* ENUM(1 byte) meta=63233 nullable=0 is_null=0 */ ### @='zhuhai' /* VARSTRING(90) meta=90 nullable=0 is_null=0 */ # at # :: server end_log_pos Xid = COMMIT/*!*/; [root@localhost mysql]#
这里sed有点复杂,需要童鞋们好好自己研究研究,这里我就不多说了。
[root@localhost mysql]# 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@localhost mysql]# cat recover.sql UPDATE db01.t1 SET @= , @='daiiy' , @= , @='guangzhou' , WHERE @= ; UPDATE db01.t1 SET @= , @='tom' , @= , @='shanghai' , WHERE @= ; UPDATE db01.t1 SET @= , @='liany' , @= , @='beijing' , WHERE @= ; [root@localhost mysql]#
将文件中的@1,@2,@3,@4替换为t1表中id,name,sex,address字段,并删除最后字段的","号
[root@localhost mysql]# sed -i 's/@1/id/g;s/@2/name/g;s/@3/sex/g;s/@4/address/g' recover.sql [root@localhost mysql]# sed -i -r 's/(address=.*),/\1/g' recover.sql [root@localhost mysql]# cat recover.sql UPDATE db01.t1 SET , name='daiiy' , sex= , address='guangzhou' WHERE ; UPDATE db01.t1 SET , name='tom' , sex= , address='shanghai' WHERE ; UPDATE db01.t1 SET , name='liany' , sex= , address='beijing' WHERE ; [root@localhost mysql]#
7.到这里日志就处理好了,现在导入即可(导入数据后,解锁表);
mysql> source recover.sql; Query OK, row affected (0.12 sec) Rows matched: Changed: Warnings: Query OK, row affected (0.00 sec) Rows matched: Changed: Warnings: Query OK, row affected (0.01 sec) Rows matched: Changed: Warnings: mysql> select * from t1; +----+-------+-----+-----------+ | id | name | sex | address | +----+-------+-----+-----------+ | daiiy | m | guangzhou | | tom | f | shanghai | | liany | m | beijing | | lilu | m | zhuhai | +----+-------+-----+-----------+ rows in set (0.00 sec) mysql>
可以看见数据已经完全恢复,这种方法的优点是快速,方便。
转载自 http://www.cnblogs.com/gomysql/p/3582058.html