Binlog2sql+CentOS7 離線安裝


Binlog2sql+CentOS7 離線安裝

1. 環境

CentOS Linux release 7.7.1908 (Core)

Mysql 8.0.20

Mysql 5.7.29

數據庫已開啟Binlog

2. 下載

下載Python-3.8.2:https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz (3.8.3 Make失敗,3.8.2安裝正常)

下載Binlog2sql: https://codeload.github.com/danfengcao/binlog2sql/zip/master

下載PyMySQL-0.9.3 https://codeload.github.com/PyMySQL/PyMySQL/tar.gz/v0.9.3

下載mysql-replication-0.21

https://files.pythonhosted.org/packages/e3/54/8c496e300d610299bf168e2068dc10a64b66b299cbe596a27aac5d5b3e7b/mysql-replication-0.21.tar.gz

相關主頁

Binlog2sql開源主頁 https://github.com/danfengcao/binlog2sql

PyMysql開源歷史版本 https://github.com/PyMySQL/PyMySQL/releases

mysql-replication 歷史版本 https://pypi.org/project/mysql-replication/#history

3 安裝

3.1 Pip 安裝

yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc libffi-devel

tar -xvf Python-3.8.2.tar.xz && cd Python-3.8.2

./configure --prefix=/usr --with-ensurepip --with-system-ffi

make && make install

[root@mysql1 Python-3.8.2]# python --version
Python 2.7.5
[root@mysql1 Python-3.8.2]# python3 --version
Python 3.8.2
[root@mysql1 Python-3.8.2]# pip3 --version
pip 19.2.3 from /usr/lib/python3.8/site-packages/pip (python 3.8)

3.2 PyMySQL/mysql-replication 安裝

pip3 install PyMySQL-0.9.3.tar.gz

pip3 install mysql-replication-0.21.tar.gz

[root@mysql1 ~]# pip3 list
Package           Version
----------------- -------
mysql-replication 0.21   
pip               19.2.3 
PyMySQL           0.9.3  
setuptools        41.2.0 

3.3 解壓縮 Binlog2sql

unzip binlog2sql-master.zip

mv binlog2sql-master/binlog2sql ./

rm -rf binlog2sql-master

binlog2sql 解壓即可,不需要安裝,且只有binlog2sql 文件夾是我們真正需要的。

4. 案例:誤刪除數據恢復

案例准備:建庫,建表,並插入數據

故障模擬:誤刪除數據

恢復目標:找回數據

4.1 案例准備

create database cym;

use cym;

create table t1(id int,name varchar(10),addtime datetime default now());

insert into t1 values(1,'趙',now()),(2,'錢',now()),(3,'孫','2022-01-12 12:12:12'),(4,'李','2000-12-12 1:00:00');

select * from t1;

mysql> select * from t1;
+------+------+---------------------+
| id   | name | addtime             |
+------+------+---------------------+
|    1 | 趙   | 2020-05-01 14:01:04 |
|    2 | 錢   | 2020-05-01 14:01:04 |
|    3 | 孫   | 2022-01-12 12:12:12 |
|    4 | 李   | 2000-12-12 01:00:00 |
+------+------+---------------------+
4 rows in set (0.00 sec)

4.2 故障模擬

select now();

delete from cym.t1;

select * from cym.t1;

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-05-01 14:02:43 |
+---------------------+
1 row in set (0.00 sec)
mysql> delete from cym.t1;
Query OK, 4 rows affected (0.00 sec)
mysql> select * from cym.t1;
Empty set (0.00 sec)
-- 確定數據丟失

4.3 故障恢復

我們可以使用root 操作,也可以設置專門的最小權限binlog挖掘用戶

create user binlog2sql identified by 'binlog2sql';

GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON . TO binlog2sql;

4.3.1 檢索需要用到的binlog文件

mysql -ubinlog2sql -pbinlog2sql -e 'show master status'

[root@mysql1 ~]# mysql -ubinlog2sql -pbinlog2sql -e 'show master status'
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000013 |     3156 |              |                  | fa9a20b5-831c-11ea-b919-080027a0316a:1-17 |
+------------------+----------+--------------+------------------+-------------------------------------------+

如果刪除的時間比較久,我們需要根據大概的時間范圍,結合binlog最后更新時間,確定可能用到的binlog 文件

獲取binlog 位置:

mysql -ubinlog2sql -pbinlog2sql -e 'select @@log_bin_basename'

[root@mysql1 ~]# mysql -ubinlog2sql -pbinlog2sql -e 'select @@log_bin_basename'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------------------------+
| @@log_bin_basename          |
+-----------------------------+
| /mysqldata/binlog/mysql-bin |
+-----------------------------+
[root@mysql1 ~]# ll  /mysqldata/binlog/mysql-bin*
-rw-r-----. 1 mysql mysql  217 Apr 28 19:24 /mysqldata/binlog/mysql-bin.000009
-rw-r-----. 1 mysql mysql  241 Apr 28 21:01 /mysqldata/binlog/mysql-bin.000010
-rw-r-----. 1 mysql mysql  194 May  1 12:25 /mysqldata/binlog/mysql-bin.000011
-rw-r-----. 1 mysql mysql  217 May  1 13:26 /mysqldata/binlog/mysql-bin.000012
-rw-r-----. 1 mysql mysql 3156 May  1 14:05 /mysqldata/binlog/mysql-bin.000013
-rw-r-----. 1 mysql mysql  455 May  1 13:26 /mysqldata/binlog/mysql-bin.index
--以上時間為binlog 最后變更時間

4.3.2 獲取誤刪除操作的undo語句

已知:

誤操作時間范圍:2020-05-01 14:02:43 之后

mysql-bin.000012 包含的13:26之前的操作

所以我們要挖掘的binlog文件為:mysql-bin.000013

獲取Undo語句

python binlog2sql/binlog2sql.py -ubinlog2sql -pbinlog2sql -dcym -t t1 -B --start-file='mysql-bin.000013' --start-datetime='2020-05-01 14:02:43' --only-dml

[root@mysql1 ~]# python binlog2sql/binlog2sql.py -ubinlog2sql -pbinlog2sql -dcym -t t1 --start-file='mysql-bin.000013' --start-datetime='2020-05-01 14:02:43' --only-dml
DELETE FROM `cym`.`t1` WHERE `addtime`='2020-05-01 14:01:04' AND `id`=1 AND `name`='趙' LIMIT 1; #start 2427 end 2638 time 2020-05-01 14:02:43
DELETE FROM `cym`.`t1` WHERE `addtime`='2020-05-01 14:01:04' AND `id`=2 AND `name`='錢' LIMIT 1; #start 2427 end 2638 time 2020-05-01 14:02:43
DELETE FROM `cym`.`t1` WHERE `addtime`='2022-01-12 12:12:12' AND `id`=3 AND `name`='孫' LIMIT 1; #start 2427 end 2638 time 2020-05-01 14:02:43
DELETE FROM `cym`.`t1` WHERE `addtime`='2000-12-12 01:00:00' AND `id`=4 AND `name`='李' LIMIT 1; #start 2427 end 2638 time 2020-05-01 14:02:43

我們需要把undo 語句保存為sql文件

python binlog2sql/binlog2sql.py -ubinlog2sql -pbinlog2sql -dcym -t t1 -B --start-file='mysql-bin.000013' --start-datetime='2020-05-01 14:02:43' --only-dml|cut -d '#' -f1 >t1.sql

[root@mysql1 ~]# python binlog2sql/binlog2sql.py -ubinlog2sql -pbinlog2sql -dcym -t t1 -B --start-file='mysql-bin.000013' --start-datetime='2020-05-01 14:02:43' --only-dml|cut -d '#' -f1 >t1.sql
[root@mysql1 ~]# cat t1.sql 
INSERT INTO `cym`.`t1`(`addtime`, `id`, `name`) VALUES ('2000-12-12 01:00:00', 4, '李'); 
INSERT INTO `cym`.`t1`(`addtime`, `id`, `name`) VALUES ('2022-01-12 12:12:12', 3, '孫'); 
INSERT INTO `cym`.`t1`(`addtime`, `id`, `name`) VALUES ('2020-05-01 14:01:04', 2, '錢'); 
INSERT INTO `cym`.`t1`(`addtime`, `id`, `name`) VALUES ('2020-05-01 14:01:04', 1, '趙');

4.3.3 執行恢復操作

可以使用業務用戶,也可以使用超級用戶恢復數據。

mysql -uroot -proot < t1.sql

mysql -uroot -proot -e 'select * from cym.t1'

[root@mysql1 ~]# mysql -uroot -proot -e 'select * from cym.t1'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+------+---------------------+
| id   | name | addtime             |
+------+------+---------------------+
|    4 | 李   | 2000-12-12 01:00:00 |
|    3 | 孫   | 2022-01-12 12:12:12 |
|    2 | 錢   | 2020-05-01 14:01:04 |
|    1 | 趙   | 2020-05-01 14:01:04 |
+------+------+---------------------+

完成恢復

5. Binlog2sql 可用參數

python binlog2sql/binlog2sql.py

參考:https://github.com/danfengcao/binlog2sql#選項

可選參數
--stop-never
-K, --no-primary-key
-B, --flashback
--back-interval
連接參數
-h -u -p -P
位置過濾參數
--start-file
--stop-file
--start-position
--stop-position
--start-datetime
 --stop-datetime
 
 對象過濾參數
 -d DATABASES2 DATABASES2
 -t TABLE1 TABLE2
 
 類型過濾參數
 --only-dml
 --sql-type  INSERT UPDATE DELETE


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM