利用MyFlash實現(xiàn)MySQL數(shù)據(jù)閃回的操作指南
更新時間:2024年06月21日 09:08:58 作者:逢生博客
MySQL數(shù)據(jù)閃回是一種高級功能,它允許你在數(shù)據(jù)庫中恢復到某個特定的時間點,通常是事務開始或保存點的狀態(tài),以便處理數(shù)據(jù)錯誤或回滾意外更改,本文給大家介紹了如何利用MyFlash實現(xiàn)MySQL數(shù)據(jù)閃回,需要的朋友可以參考下
Github
MyFlash 限制
僅支持 5.6 與 5.7
binlog 格式必須為 row,且 binlog_row_image=full
只能回滾DML(增、刪、改)
MySQL 準備
注: 本章 MySQL 是采用 Docker 部署,容器內是 不帶mysqlbinlog 的,可以從已安裝 MySQL 的Linux服務器上拷貝 mysqlbinlog 文件。
開啟 binlog
[mysqld] # binlog功能 log_bin=/var/lib/mysql/mysql-bin # binlog 文件格式 binlog_format=ROW binlog_row_image=FULL # binlog 文件保留時間7天(默認0天) expire_logs_days=7 # binlog 單個文件的最大值大?。J1G) max_binlog_size=512m # 開啟 binlog 后需要創(chuàng)建 function 或 procedure 時要開啟 log_bin_trust_function_creators=1 # 服務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 文件編號以及最后一個操作事件結束點(Position)值 show master status; -- 結束當前 binlog 文件并生成新的 binlog 文件 flush logs; -- 重置 binlog 文件(清空全部 biglog 文件) reset master;
注: binlog 默認存放在 /var/lib/mysql/ 數(shù)據(jù)庫文件目錄。
mysqlbinlog
- 查看 Linux 環(huán)境下 mysqlbinlog 目錄
# /usr/bin/mysqlbinlog whereis mysqlbinlog
- 將 mysqlbinlog 文件復制到容器內的 /usr/bin 目錄
docker cp mysqlbinlog mysql:/usr/bin # 進入容器內 docker exec -it mysql /bin/bash # 在容器內為 mysqlbinlog 添加權限 chmod +x /usr/bin/mysqlbinlog
- 使用 mysqlbinlog 生成 sql 文件
# 進入容器內執(zhí)行 mysqlbinlog --no-defaults --base64-output=DECODE-ROWS -v /var/lib/mysql/mysql-bin.000001 >/tmp/binlog-000001.sql
# 進入容器內執(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(,) |
生成回滾文件
# 進入MyFlash的bin目錄 cd ~/MyFlash/binary # 生成回滾文件,執(zhí)行后會在當前目錄下生成 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ù)庫密碼
到此這篇關于利用MyFlash實現(xiàn)MySQL數(shù)據(jù)閃回的操作指南的文章就介紹到這了,更多相關MyFlash MySQL數(shù)據(jù)閃回內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Mysql動態(tài)更新數(shù)據(jù)庫腳本的示例講解
今天小編就為大家分享一篇關于Mysql動態(tài)更新數(shù)據(jù)庫腳本的示例講解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

