MySQL【Update误操作】回滚(转)
前言:
继上一篇MySQL【Delete误操作】回滚之后,现在介绍下Update回滚,操作数据库时候难免会因为“大意”而误操作,需要快速恢复的话通过备份来恢复是不太可能的,因为需要还原和binlog差来恢复,等不了,很费时。这里说明因为Update
操作的恢复方法:主要还是通过binlog来进行恢复,前提是binlog_format必须是Row格式,否则只能通过备份来恢复数据了。和上一篇的条件一样。
方法:
条件:开启Binlog,Format为Row。
步骤:
1.通过MySQL自带工具mysqlbinlog 指定导出操作的记录:
表结构和记录数:

root@localhost : test 10:06:16>select count(*) from me_info;
+----------+
| count(*) |
+----------+
| 84183 |
+----------+
1 row in set (0.00 sec) root@localhost : test 10:12:14>select id,realName,contactAddress from me_info limit 3;
+---------+--------------------------+--------------------+
| id | realName | contactAddress |
+---------+--------------------------+--------------------+
| 2123269 | 数据库管理员 | 浙江杭州滨江 |
| 2123270 | 中级数据库管理员 | 浙江杭州西湖 |
| 2123271 | 高级数据库管理员 | 浙江杭州余杭 |
+---------+--------------------------+--------------------+ root@localhost : test 10:12:18>desc me_info;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | MUL | 0 | |
| birthTime | date | YES | | NULL | |
| enName | varchar(255) | YES | | NULL | |
| gender | tinyint(2) | YES | | 0 | |
| identity | varchar(255) | YES | | | |
| identitylType | tinyint(2) | YES | | 0 | |
| interest | varchar(255) | YES | | | |
| jobYear | int(11) | YES | | 0 | |
| livePlace | mediumint(6) | YES | | 0 | |
| location | mediumint(6) | YES | | 0 | |
| married | tinyint(2) | YES | | 0 | |
| mdCerti | tinyint(2) | YES | | 0 | |
| mdCertiNum | varchar(255) | YES | | | |
| photo | varchar(255) | YES | | | |
| posit | mediumint(6) | YES | | NULL | |
| graduateMajor | int(11) | YES | | 0 | |
| realName | varchar(255) | YES | | NULL | |
| userPublic | tinyint(2) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| contactCell | varchar(255) | YES | | NULL | |
| contactPhone | varchar(255) | YES | | NULL | |
| contactZip | varchar(6) | YES | | NULL | |
| contactWebsite | varchar(255) | YES | | NULL | |
| contactLocation | mediumint(6) | YES | | 0 | |
| contactAddress | varchar(255) | YES | | NULL | |
| userName | varchar(50) | YES | | NULL | |
| education | int(11) | YES | | 0 | |
| workName | varchar(255) | YES | | NULL | |
| workCategory | varchar(255) | YES | | NULL | |
| nowSalary | int(11) | YES | | 0 | |
| grade | int(11) | YES | | 0 | |
| userId | int(11) | YES | | 0 | |
| jobApplyStatus | tinyint(4) | YES | | NULL | |
| source | tinyint(4) | YES | | NULL | |
| englishLevel | tinyint(4) | YES | | 0 | |
| modifyTime | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+

更新表:

root@localhost : test 10:15:09>update me_info set realName='周吴郑王',contactAddress='浙江vv杭州vv北京';
Query OK, 84183 rows affected (1.56 sec)
Rows matched: 84183 Changed: 84183 Warnings: 0 root@localhost : test 11:11:08>select id,realName,contactAddress from me_info limit 3;
+---------+--------------+------------------------+
| id | realName | contactAddress |
+---------+--------------+------------------------+
| 2123269 | 周吴郑王 | 浙江vv杭州vv北京 |
| 2123270 | 周吴郑王 | 浙江vv杭州vv北京 |
| 2123271 | 周吴郑王 | 浙江vv杭州vv北京 |
+---------+--------------+------------------------+
3 rows in set (0.00 sec)

最后通过mysqlbinlog 取出:
root@zhoujy:/var/log/mysql# mysqlbinlog --no-defaults --start-datetime='2012-12-26 22:15:05' --stop-datetime='2012-12-26 22:17:00' -vv mysql-bin.000001 > /home/zhoujy/restore/me_info.txt
原始数据:

### UPDATE test.me_info
### WHERE
### @1=2123269 /* INT meta=0 nullable=0 is_null=0 */
### @2='1990:11:12' /* DATE meta=0 nullable=1 is_null=0 */
### @3=NULL /* DATE meta=765 nullable=1 is_null=1 */
### @4=2 /* TINYINT meta=0 nullable=1 is_null=0 */
### @5='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @6=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @7='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @8=-1 (4294967295) /* INT meta=0 nullable=1 is_null=0 */
### @9=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @10=340800 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @11=1 /* TINYINT meta=0 nullable=1 is_null=0 */
### @12=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @13='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @14='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @15=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @16=32071 /* INT meta=0 nullable=1 is_null=0 */
### @17='数据库管理员' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @18=NULL /* VARSTRING(765) meta=0 nullable=1 is_null=1 */
### @19='123456@qq.comx0a' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @20='123456' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @21='0571-123456' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @22=NULL /* VARSTRING(765) meta=18 nullable=1 is_null=1 */
### @23='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @24=340100 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @25='浙江杭州滨江' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @26=NULL /* VARSTRING(765) meta=150 nullable=1 is_null=1 */
### @27=1 /* INT meta=0 nullable=1 is_null=0 */
### @28=NULL /* INT meta=765 nullable=1 is_null=1 */
### @29=NULL /* INT meta=765 nullable=1 is_null=1 */
### @30=0 /* INT meta=0 nullable=1 is_null=0 */
### @31=0 /* INT meta=0 nullable=1 is_null=0 */
### @32=1700671 /* INT meta=0 nullable=1 is_null=0 */
### @33=NULL /* INT meta=0 nullable=1 is_null=1 */
### @34=3 /* TINYINT meta=0 nullable=1 is_null=0 */
### @35=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @36=NULL /* TINYINT meta=0 nullable=1 is_null=1 */
### SET
### @1=2123269 /* INT meta=0 nullable=0 is_null=0 */
### @2='1990:11:12' /* DATE meta=0 nullable=1 is_null=0 */
### @3=NULL /* DATE meta=765 nullable=1 is_null=1 */
### @4=2 /* TINYINT meta=0 nullable=1 is_null=0 */
### @5='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @6=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @7='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @8=-1 (4294967295) /* INT meta=0 nullable=1 is_null=0 */
### @9=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @10=340800 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @11=1 /* TINYINT meta=0 nullable=1 is_null=0 */
### @12=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @13='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @14='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @15=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @16=32071 /* INT meta=0 nullable=1 is_null=0 */
### @17='周吴郑王' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @18=NULL /* VARSTRING(765) meta=0 nullable=1 is_null=1 */
### @19='123456@qq.comx0a' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @20='123456' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @21='0571-123456' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @22=NULL /* VARSTRING(765) meta=18 nullable=1 is_null=1 */
### @23='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @24=340100 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @25='浙江vv杭州vv北京' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @26=NULL /* VARSTRING(765) meta=150 nullable=1 is_null=1 */
### @27=1 /* INT meta=0 nullable=1 is_null=0 */
### @28=NULL /* INT meta=765 nullable=1 is_null=1 */
### @29=NULL /* INT meta=765 nullable=1 is_null=1 */
### @30=0 /* INT meta=0 nullable=1 is_null=0 */
### @31=0 /* INT meta=0 nullable=1 is_null=0 */
### @32=1700671 /* INT meta=0 nullable=1 is_null=0 */
### @33=NULL /* INT meta=0 nullable=1 is_null=1 */
### @34=3 /* TINYINT meta=0 nullable=1 is_null=0 */
### @35=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @36=NULL /* TINYINT meta=0 nullable=1 is_null=1 */

Row格式的binlog记录的格式如上面所示,需要做的工作就是把Update的操作的WHERE好SET对调,上面的都是有一定规律的,并且需要注意的是:
除了MySQL【Delete误操作】回滚事项外,还有
①:需要把@表示的“虚列”换成“实列”。
②:更新NULL值的时候,WHERE 后面的字段有NULL的,不能用“=”号,需要用“is”。
清楚里之后,可以用脚本来还原刚才update的值:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: restore_update.py
# Purpose: 通过Binlog恢复Update误操作数据
# Author: zhoujy
# Created: 2012-12-26
# update: 2012-12-26
# Copyright: (c) Mablevi 2012
# Licence: zjy
# Usage: python restore_update.py binlog.txt tablename
#-------------------------------------------------------------------------------
import MySQLdb
import sys
reload(sys)
sys.setdefaultencoding("utf-8") def get_column(conn,tbname): #从库中取字段"实名",代替@字段名。
query = "select group_concat(COLUMN_NAME) from information_schema.COLUMNS where TABLE_NAME='%s'" %tbname
cursor = conn.cursor()
cursor.execute(query)
items = cursor.fetchone()
return items
def read_binlog(file,tbname,column):
f = open(file)
columns = column.split(',')
num = '@'+str(len(columns)) #取字段数
while True:
lines = f.readline()
if lines.strip()[0:3] == '###':
lines=lines.split(' ',3)
if lines[1].strip() == 'WHERE': #SET和WHERE对调。
lines[1] = "SET"
sep1 = ','
sep2 = '' #如果是SET 后面的字段,则用这些定义的分隔符。
sep3 = ' = '
lines[-1] = lines[-1].strip()
elif lines[1].strip() == 'SET': #SET和WHERE对调。
lines[1] = "WHERE"
lines[-1] = lines[-1].strip()
sep1 = '\nand'
sep2 = ';\n' #WHERE 后面的字段,则用这些定义的分隔符。
sep3 = ' is '
else:
lines[-1] = lines[-1].strip()
if ''.join(lines).find('@') <> -1 and lines[3].split('=',1)[0] <> num:
c = int(lines[3].split('=',1)[0].split('@')[1])
col = columns[c-1] #取实名字段。
lines[3] = lines[3].split('=',1)[-1].strip()
if lines[3].strip('\'').strip().find('\'') <> -1:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')
lines[3] = col + " = " + '\'' + lines[3] + '\'' + sep1
elif lines[3].find('INT meta') <> -1 and lines[3].find('NULL') == -1:
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = col + " = " + lines[3].split()[0] + sep1
elif lines[3].find('NULL') <> -1: #和Delete脚本一样,不一样的是分隔符。
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = col + sep3 + lines[3] + sep1
else:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')
lines[3] = col + " = " + '\'' + lines[3].strip('\''' ') + '\'' + sep1
if ''.join(lines).find('@') <> -1 and lines[3].split('=',1)[0] == num:
c = int(lines[3].split('=',1)[0].split('@')[1])
col = columns[c-1]
lines[3] = lines[3].split('=',1)[-1].strip()
if lines[3].strip('\'').strip().find('\'') <> -1:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')
lines[3] = col + " = " + '\'' + lines[3] + '\'' + sep2
elif lines[3].find('INT meta') <> -1 and lines[3].find('NULL') == -1:
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = col + " = " + lines[3].split()[0] + sep2
elif lines[3].find('NULL') <> -1: #和Delete脚本一样,不一样的是分隔符。
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = col + sep3 + lines[3] + sep2
else:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'')
lines[3] = col + " = " + '\'' + lines[3].strip('\''' ') + '\'' + sep2
print ' '.join(lines[1:])
if lines == '':
break
if __name__=='__main__':
conn = MySQLdb.connect(host='localhost',user='root',passwd='123456',charset='utf8',db='test')
col = get_column(conn,sys.argv[2])
for column in col:
read_binlog(sys.argv[1],sys.argv[2],column)

执行脚本:格式:python 脚本名 binlog文本 表名
zhoujy@zhoujy:~/restore$ python restore_update.py me_info.txt me_info > me_info.sql
效果:

PDATE test.me_info
SET
id = 2123269,
birthTime = '1990:11:12',
enName = NULL,
gender = 2,
identity = '',
identitylType = 0,
interest = '',
jobYear = -1,
livePlace = 0,
location = 340800,
married = 1,
mdCerti = 0,
mdCertiNum = '',
photo = '',
posit = 0,
graduateMajor = 32071,
realName = '数据库管理员',
userPublic = NULL,
email = '123456@qq.comx0a',
contactCell = '123456',
contactPhone = '0571-123456',
contactZip = NULL,
contactWebsite = '',
contactLocation = 340100,
contactAddress = '浙江杭州滨江',
userName = NULL,
education = 1,
workName = NULL,
workCategory = NULL,
nowSalary = 0,
grade = 0,
userId = 1700671,
jobApplyStatus = NULL,
source = 3,
englishLevel = 0,
modifyTime = NULL
WHERE
id = 2123269
and
birthTime = '1990:11:12'
and
enName is NULL
and
gender = 2
and
identity = ''
and
identitylType = 0
and
interest = ''
and
jobYear = -1
and
livePlace = 0
and
location = 340800
and
married = 1
and
mdCerti = 0
and
mdCertiNum = ''
and
photo = ''
and
posit = 0
and
graduateMajor = 32071
and
realName = '周吴郑王'
and
userPublic is NULL
and
email = '123456@qq.comx0a'
and
and
contactCell = '123456'
and
contactPhone = '0571-123456'
and
contactZip is NULL
and
contactWebsite = ''
and
contactLocation = 340100
and
contactAddress = '浙江vv杭州vv北京'
and
userName is NULL
and
education = 1
and
workName is NULL
and
workCategory is NULL
and
nowSalary = 0
and
grade = 0
and
userId = 1700671
and
jobApplyStatus is NULL
and
source = 3
and
englishLevel = 0
and
modifyTime is NULL;

还原:
zhoujy@zhoujy:~/restore$ mysql test < me_info.sql
结果,表结果和记录数:

root@localhost : test 11:11:14>select count(*) from me_info;
+----------+
| count(*) |
+----------+
| 84183 |
+----------+
1 row in set (0.00 sec) root@localhost : test 11:12:36>select id,realName,contactAddress from me_info limit 3;
+---------+--------------------------+--------------------+
| id | realName | contactAddress |
+---------+--------------------------+--------------------+
| 2123269 | 数据库管理员 | 浙江杭州滨江 |
| 2123270 | 中级数据库管理员 | 浙江杭州西湖 |
| 2123271 | 高级数据库管理员 | 浙江杭州余杭 |
+---------+--------------------------+--------------------+
3 rows in set (0.00 sec)

总结:
【更新于 20160504】
开启Row模式的回滚操作,特别要注意表字符集的问题,保证表的字符集一致,否则出现乱码问题,如:

表结构:
CREATE TABLE `tmp_1` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`address` varchar(20) CHARACTER SET gbk DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 操作:
insert into tmp_1 values(1,'UTF8字符串', 'GKB字符串');
insert into tmp_1 values(2,'我们a', '你们a'); binlog记录: '/*!*/;
### INSERT INTO `dba_test`.`tmp_1`
### SET
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
### @2='UTF8字符串' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
### @3='GKB?ַ???' /* VARSTRING(40) meta=40 nullable=1 is_null=0 */ #出现乱码,复制是没有问题的,要是反转回滚就会出现乱码!

更多的信息见:
MySQL【Update误操作】回滚(转)的更多相关文章
- 数据库周刊28│开发者最喜爱的数据库是什么?阿里云脱口秀聊程序员转型;MySQL update误操作;PG流复制踩坑;PG异机归档;MySQL架构选型;Oracle技能表;Oracle文件损坏处理……
热门资讯 1.Stackoverflow 2020年度报告出炉!开发者最喜爱的数据库是什么?[摘要]2020年2月,近6.5万名开发者参与了 Stackoverflow 的 2020 年度调查,这份报 ...
- 第二百八十六节,MySQL数据库-MySQL事务操作(回滚)
MySQL数据库-MySQL事务操作(回滚) 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 举例:有这样一张表 从表里可以看出张 ...
- 记一次生产mysql数据误操作恢复过程
提示:建议每次对数据库进行修改时都做下备份 注意:以下Mysql开启的是row格式的binlog日志,确定到误操作具体时间可能有些麻烦,默认的格式就能很快找出来.这里开启row的原因是还有一种更快的方 ...
- MySQL存储过程中实现回滚
用存储过程处理复杂的业务时,可能涉及到对多张表格的操作,在任一个步骤出了问题,就需要对前面的操作回滚.举例实现: DROP PROCEDURE IF EXISTS pro_test; CREATE P ...
- 【MySQL】使用mysqlbinlog回滚
参考:http://wubx.net/?s=mysqlbinlog mysql官方的mysqlbinlog没有回滚的功能,淘宝大牛对官方代码进行了修改使之能够将binlog中的DML操作变成互逆的语句 ...
- MySQL InnoDB加锁超时回滚机制(转)
add by zhj: 看来我对MySQL的理解还有待深入,水还是挺深的啊,MySQL给记录加锁时,可以通过innodb_lock_wait_timeout参数设置超时时间, 如果加锁等待超过这个时间 ...
- mysql事务提交和回滚机制
应用场景: 银行取钱,从ATM机取钱,分为以下几个步骤 1 登陆ATM机,输入密码: 2 连接数据库,验证密码: 3 验证成功,获得用户信息,比如存款余额等: 4 用 ...
- 对mysql事务提交、回滚的错误理解
一.起因 begin或者START TRANSACTION开始一个事务 rollback事务回滚 commit 事务确认 人们对事务的解释如下:事务由作为一个单独单元的一个或多个SQL语句组成,如果其 ...
- mysql分类和事务回滚
主要内容: ***数据定义语言DDL重点 ***数据操纵语言DML重点 数据查询语言DQL重点 ---事务控制语言TCL ---数据库控制语言DCL ---主键(primary key) ---数据冗 ...
随机推荐
- 【转发】构建高可伸缩性的WEB交互式系统(下)
原文转自:http://kb.cnblogs.com/page/504518/ 本文是<构建高可伸缩性的WEB交互式系统>系列文章的第三篇,以网易的NEJ框架为例,对模块的可伸缩性进行分析 ...
- OLAP的一些知识——接下去的项目需要的背景
1.维是人们观察主题的特定角度,每一个维分别用一个表来描述,称为“维表”(Dimension Table),它是对维的详细描述. 2.事实表示所关注的主题,亦由表来描述,称为“事实表”(Fact Ta ...
- [SYSU]每周一赛
2014年每周一赛第一场 A.Cutting Sausages B.Rectangular Fields //待做 ...
- 《JavaScript Ninja》之挥舞函数
挥舞函数 匿名函数为什么如此重要 通常使用匿名函数的情况是,创建一个供以后使用的函数.例如,将匿名函数保存在一个变量里,将其作为一个对象的方法,或者是将匿名函数作为一个回调.-->在这些情况下, ...
- R 学习1
首先安装吧 http://cran.rstudio.com/bin/windows/base/R-3.2.1-win.exe 里面既有32位又有64. R有很多包,如果有的包本地没有,来这里搜 htt ...
- 冒泡排序(C++版)
/** Bubble Sort * * Key * * position: where swap * * iter: sub-position in each trip * */ template & ...
- dede首页调用栏目内容_{dede:field.content/}首页调用
如何将已经做成单页的栏目内容调用到首页来. 常用的需要调到首页来的单页内容,比如公司简介.联系我们等内容,我们在首页可能都要进行展现.通过常规的方式,包括查阅dede官方论坛资料,都找不到比较合适的答 ...
- 比较compareTo与equals及==的区别
1.compareTo: 附上:源码: public int compareTo(String anotherString) { int len1 = value.length; ...
- 全国信息学奥林匹克联赛(NOIP2014)复赛 模拟题Day2 长乐一中
题目名称 改造二叉树 数字对 交换 英文名称 binary pair swap 输入文件名 binary.in pair.in swap.in 输出文件名 binary.out pair.out sw ...
- POJ 3422Kaka's Matrix Travels(最小费用最大流)
Kaka's Matrix Travels Time Limit: 1000MS M ...