利用MyFlash實現(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 文件。
開啟 binlog
[mysqld] # binlog功能 log_bin=/var/lib/mysql/mysql-bin # binlog 文件格式 binlog_format=ROW binlog_row_image=FULL # binlog 文件保留時間7天(默認(rèn)0天) expire_logs_days=7 # binlog 單個文件的最大值大小(默認(rèn)1G) max_binlog_size=512m # 開啟 binlog 后需要創(chuàng)建 function 或 procedure 時要開啟 log_bin_trust_function_creators=1 # 服務(wù)id,以區(qū)分主庫和備庫 server-id=1
-- 顯示是否開啟 binlog show variables like 'log_bin%'; -- 顯示 binlog 文件格式 show variables like 'binlog_format%'; -- 顯示 binlog 文件保留時間 show variables like 'expire_logs_days%'; -- 顯示 binlog 單個文件的最大值大小 show variables like 'max_binlog_size%';
-- 顯示 binlog 事件 show binlog events; -- 顯示全部 binlog 文件列表 show binary logs; show master logs; -- 顯示最新 binlog 文件編號以及最后一個操作事件結(jié)束點(Position)值 show master status; -- 結(jié)束當(dāng)前 binlog 文件并生成新的 binlog 文件 flush logs; -- 重置 binlog 文件(清空全部 biglog 文件) reset master;
注: binlog 默認(rèn)存放在 /var/lib/mysql/ 數(shù)據(jù)庫文件目錄。
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 # 驗證是否安裝成功 pkg-config --modversion glib-2.0
- 安裝MyFlash
# 下載源碼 git clone https://github.com/Meituan-Dianping/MyFlash.git cd MyFlash # 動態(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
- 驗證是否安裝成功
cd ~/MyFlash/binary ./flashback --help
flashback 選項
選項 | 說明 |
---|---|
–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í)行后會在當(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ù)庫密碼
到此這篇關(guān)于利用MyFlash實現(xiàn)MySQL數(shù)據(jù)閃回的操作指南的文章就介紹到這了,更多相關(guān)MyFlash MySQL數(shù)據(jù)閃回內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析數(shù)據(jù)庫分頁的兩種方法對比(row_number()over()和top的對比)
本篇文章是對數(shù)據(jù)庫分頁的兩種方法對比(row_number()over()和top的對比)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07詳細(xì)聊一聊mysql的樹形結(jié)構(gòu)存儲以及查詢
由于mysql是關(guān)系型數(shù)據(jù)庫,因此對于類似組織架構(gòu),子任務(wù)等相關(guān)的樹形結(jié)構(gòu)的處理不是很友好,下面這篇文章主要給大家介紹了關(guān)于mysql樹形結(jié)構(gòu)存儲以及查詢的相關(guān)資料,需要的朋友可以參考下2022-04-04mysql數(shù)據(jù)庫查詢基礎(chǔ)命令詳解
這篇文章主要介紹了mysql數(shù)據(jù)庫查詢基礎(chǔ)命令,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11Django連接本地mysql數(shù)據(jù)庫(pycharm)的步驟
這篇文章主要介紹了Django連接本地mysql數(shù)據(jù)庫(pycharm)的步驟,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09mysql提示Can't?connect?to?MySQL?server?on?localhost
這篇文章主要介紹了Can't?connect?to?MySQL?server?on?localhost?(10061)解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03mysql數(shù)據(jù)庫備份及恢復(fù)命令 mysqldump,source的用法
mysql數(shù)據(jù)庫備份及恢復(fù)命令 mysqldump,source的用法,需要的朋友可以參考下。2011-02-02