環境:阿里雲 RDS數據庫 ,開通了binlog
一個坑 (中途接手的項目)
表結構中加上 ON UPDATE CURRENT_TIMESTAMP
`CREATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '時間'
增加一個字段後同意更新發現CREATE_TIME的時間也被更新
解決問題:
修改字段
alter table t_xxxx modify column `CREATE_TIME` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '時間'
根據時間查找對應的binlog數據,並下載到本地
mysqlbinlog --no-defaults -vv --base64-output=decode-rows 日誌文件名 -r 保存文件名
D:\Downloads>mysqlbinlog --no-defaults -vv --base64-output=decode-rows mysql-bin.000239 -r my.sql
mysqlbinlog : 安裝mysql的bin目錄下就有,需要注意版本號
https://help.aliyun.com/knowledge_detail/41709.html
更新工具提取原來的值
需要注意的是時間類型TIMESTAMP
update t_xxxxx set CREATE_TIME=FROM_UNIXTIME(1513993596) where ID=1;
最後執行sql就恢復數據了,這裏只修改了CREATE_TIME
UNIX_TIMESTAMP 和 FROM_UNIXTIME 一般是用於unix的時間戳。
例子:
SELECT UNIX_TIMESTAMP("2016-07-11")
-- 1468166400
SELECT UNIX_TIMESTAMP("2016-07-17 23:59:59")
-- 1468771199
FROM_UNIXTIME:表示把UNIX_TIMESTAMP還原成標準的時間格式
SELECT FROM_UNIXTIME(1468166400),FROM_UNIXTIME(1468771199)
出現的錯誤
D:\Downloads>mysqlbinlog -vv --base64-output=decode-rows mysql-bin.000239 -r my.sql
mysqlbinlog: [ERROR] unknown option '--no-beep'
解決方式:
D:\Downloads>mysqlbinlog --no-defaults -vv --base64-output=decode-rows mysql-bin.000239 -r my.sql