利用MyFlash實(shí)現(xiàn)MySQL數(shù)據(jù)閃回的操作指南
Github
MyFlash 限制
僅支持 5.6 與 5.7
binlog 格式必須為 row,且 binlog_row_image=full
只能回滾DML(增、刪、改)
MySQL 準(zhǔn)備
注: 本章 MySQL 是采用 Docker 部署,容器內(nèi)是 不帶mysqlbinlog 的,可以從已安裝 MySQL 的Linux服務(wù)器上拷貝 mysqlbinlog 文件。
開(kāi)啟 binlog
[mysqld] # binlog功能 log_bin=/var/lib/mysql/mysql-bin # binlog 文件格式 binlog_format=ROW binlog_row_image=FULL # binlog 文件保留時(shí)間7天(默認(rèn)0天) expire_logs_days=7 # binlog 單個(gè)文件的最大值大?。J(rèn)1G) max_binlog_size=512m # 開(kāi)啟 binlog 后需要?jiǎng)?chuàng)建 function 或 procedure 時(shí)要開(kāi)啟 log_bin_trust_function_creators=1 # 服務(wù)id,以區(qū)分主庫(kù)和備庫(kù) server-id=1
-- 顯示是否開(kāi)啟 binlog show variables like 'log_bin%'; -- 顯示 binlog 文件格式 show variables like 'binlog_format%'; -- 顯示 binlog 文件保留時(shí)間 show variables like 'expire_logs_days%'; -- 顯示 binlog 單個(gè)文件的最大值大小 show variables like 'max_binlog_size%';
-- 顯示 binlog 事件 show binlog events; -- 顯示全部 binlog 文件列表 show binary logs; show master logs; -- 顯示最新 binlog 文件編號(hào)以及最后一個(gè)操作事件結(jié)束點(diǎn)(Position)值 show master status; -- 結(jié)束當(dāng)前 binlog 文件并生成新的 binlog 文件 flush logs; -- 重置 binlog 文件(清空全部 biglog 文件) reset master;
注: binlog 默認(rèn)存放在 /var/lib/mysql/ 數(shù)據(jù)庫(kù)文件目錄。
mysqlbinlog
- 查看 Linux 環(huán)境下 mysqlbinlog 目錄
# /usr/bin/mysqlbinlog whereis mysqlbinlog
- 將 mysqlbinlog 文件復(fù)制到容器內(nèi)的 /usr/bin 目錄
docker cp mysqlbinlog mysql:/usr/bin # 進(jìn)入容器內(nèi) docker exec -it mysql /bin/bash # 在容器內(nèi)為 mysqlbinlog 添加權(quán)限 chmod +x /usr/bin/mysqlbinlog
- 使用 mysqlbinlog 生成 sql 文件
# 進(jìn)入容器內(nèi)執(zhí)行 mysqlbinlog --no-defaults --base64-output=DECODE-ROWS -v /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
# 進(jìn)入容器內(nèi)執(zhí)行 mysqlbinlog --no-defaults \ --database=db_name \ --start-datetime='2024-06-20 00:00:00' \ --stop-datetime='2024-06-20 17:00:00' \ /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
安裝 MyFlash
- 安裝依賴
# 安裝 gcc glib-2.0 yum install -y gcc libgnomeui-devel # 驗(yàn)證是否安裝成功 pkg-config --modversion glib-2.0
- 安裝MyFlash
# 下載源碼 git clone https://github.com/Meituan-Dianping/MyFlash.git cd MyFlash # 動(dòng)態(tài)編譯鏈接安裝 gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback
- 配置環(huán)境變量
vim /etc/profile
# 在文件末尾添加 alias flashback=~/MyFlash/binary/flashback
source /etc/profile
- 驗(yàn)證是否安裝成功
cd ~/MyFlash/binary ./flashback --help
flashback 選項(xiàng)
| 選項(xiàng) | 說(shuō)明 |
|---|---|
| –databaseNames | databaseName to apply. if multiple, seperate by comma(,) |
| –tableNames | tableName to apply. if multiple, seperate by comma(,) |
| –tableNames-file | tableName to apply. if multiple, seperate by comma(,) |
| –start-position | start position |
| –stop-position | stop position |
| –start-datetime | start time (format %Y-%m-%d %H:%M:%S) |
| –stop-datetime | stop time (format %Y-%m-%d %H:%M:%S) |
| –sqlTypes | sql type to filter . support INSERT, UPDATE ,DELETE. if multiple, seperate by comma(,) |
| –maxSplitSize | max file size after split, the uint is M |
| –binlogFileNames | binlog files to process. if multiple, seperate by comma(,) |
| –outBinlogFileNameBase | output binlog file name base |
| –logLevel | log level, available option is debug,warning,error |
| –include-gtids | gtids to process. if multiple, seperate by comma(,) |
| –include-gtids-file | gtids to process. if multiple, seperate by comma(,) |
| –exclude-gtids | gtids to skip. if multiple, seperate by comma(,) |
| –exclude-gtids-file | gtids to skip. if multiple, seperate by comma(,) |
生成回滾文件
# 進(jìn)入MyFlash的bin目錄 cd ~/MyFlash/binary # 生成回滾文件,執(zhí)行后會(huì)在當(dāng)前目錄下生成 binlog_output_base.flashback 文件 ./flashback --sqlTypes='INSERT' \ --binlogFileNames=/var/lib/mysql/mysql-bin.000001 \ --databaseNames=db_name \ --tableNames=table_name \ --start-datetime='2024-06-20 18:23:00'
執(zhí)行回滾操作
# 在binlog_output_base.flashback所在目錄下執(zhí)行以下命令 mysqlbinlog binlog_output_base.flashback | mysql -h 127.0.0.1 -uroot -p # 或 mysqlbinlog binlog_output_base.flashback | mysql -uroot -p # 輸入數(shù)據(jù)庫(kù)密碼
到此這篇關(guān)于利用MyFlash實(shí)現(xiàn)MySQL數(shù)據(jù)閃回的操作指南的文章就介紹到這了,更多相關(guān)MyFlash MySQL數(shù)據(jù)閃回內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL5.6.22 綠色版 安裝詳細(xì)教程(圖解)
本文通過(guò)圖文并茂的形式給大家介紹了MySQL5.6.22 綠色版 安裝詳細(xì)教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-11-11
聊聊QT添加MySQL驅(qū)動(dòng)依賴的問(wèn)題
這篇文章主要介紹了QT添加MySQL驅(qū)動(dòng)依賴的問(wèn)題,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
mysql jdbc連接步驟及常見(jiàn)參數(shù)
這篇文章主要介紹了mysql jdbc連接步驟及常見(jiàn)參數(shù),需要的朋友可以參考下2015-09-09
Mysql動(dòng)態(tài)更新數(shù)據(jù)庫(kù)腳本的示例講解
今天小編就為大家分享一篇關(guān)于Mysql動(dòng)態(tài)更新數(shù)據(jù)庫(kù)腳本的示例講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
MYSQL復(fù)雜查詢練習(xí)題以及答案大全(難度適中)
在我們學(xué)習(xí)mysql數(shù)據(jù)庫(kù)時(shí)需要一些題目進(jìn)行練習(xí),下面這篇文章主要給大家介紹了關(guān)于MYSQL復(fù)雜查詢練習(xí)題以及答案的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),這些練習(xí)題難度適中,需要的朋友可以參考下2022-08-08

