MySQL 利用mysqlbinlog二進制文件還原指定數據庫


推薦這篇 mysqlbinlog詳解 - https://www.cnblogs.com/Presley-lpc/p/9619571.html

1.

我的初始環境

系統:CentOS 7.6.1810(Py3.7.8)

MySQL:5.7.31

 

2.

檢查MySQL是否開啟binlog日志

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)

log_bin為ON即為開啟,

若未開啟?

修改配置文件,在 [mysqld]下添加如下內容

log-bin=mysql-bin
server-id = 1
binlog_format=mixed

#log-bin  開啟 Binlog 並寫明存放日志的位置;默認使用的設置是“log-bin=mysql-bin”,這樣日志是存放在默認的位置上的,一般是放在data目錄中。

#server-id  指定一個集群內的 MySQL 服務器 ID,如果做數據庫集群那么必須全局唯一,一般來說不推薦 指定 server_id 等於 1。

#binlog_format  三種Bin-log日志模式 -- 自動模式

 

 3.

模擬數據丟失 

create database test;
use test;
create table t1(
id int primary key,
name varchar(32)
);
 
INSERT INTO t1 VALUE(1,'val1');
INSERT INTO t1 VALUE(2,'val2');

create database test2;
use test2;
create table t1(
id int primary key,
name varchar(32)
);
 
INSERT INTO t1 VALUE(1,'val1');
INSERT INTO t1 VALUE(2,'val2');

drop database test;
drop database test2;

 

4.

查看日志並恢復數據

查看當前日志文件

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     4285 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.06 sec)

結果:說明 mysql-bin.000001 是當前備份的文件名

也可以直接進入/www/server/data,找到類似mysql-bin.000001的文件,選擇后綴最大的文件,因為這個是最新的

如果找不到,可以全局搜索一下,使用 find / -name mysql-bin.000001; 去查看一下路徑

由於binlog是二進制的,所以需要先轉換成文本文件,一般可以采用Mysql自帶的mysqlbinlog轉換成文本。

/www/server/mysql/bin/mysqlbinlog --no-defaults --base64-output='decode-rows' -v mysql-bin.000001 > ./binlog_2020_12_07;

部分參數說明:

  1. --no-defaults 為了防止報錯:mysqlbinlog: unknown variable 'default_character_set=utf8mb4'
  2. --base64-output='decode-rows' 和-v一起使用, 進行base64解碼
  3. -d databaseName:可以使用-d來指定數據庫
  4. --start-datetime="2020-12-07 14:27:36" :指定開始時間,注意格式不要寫錯
  5. --stop-datetime="" :指定結束時間
  6. --start-position="" : 指定起始點
  7. --stop-position="" :指定結束點

 

之后導出文件 ‘binlog_2020_12_07’,我是直接在寶塔面板下載的,用vscode打開文本文件

會看到binlog的基本塊如下:

# at 1488
#201207 14:27:26 server id 1  end_log_pos 1569 CRC32 0xcb9c1cbf     Query    thread_id=178    exec_time=0    error_code=0
SET TIMESTAMP=1607322446/*!*/;
BEGIN

基本塊解釋:

# at 1488
指明的當前位置相對文件開始的偏移位置,這個在mysqlbinlog命令中可以作為--start-position的參數

#201207 14:27:26 server id 1  end_log_pos 1569 CRC32 0xcb9c1cbf     Query    thread_id=178    exec_time=0    error_code=0
201207 14:27:26指明時間為20年12月7號14:27:26,serverid也就是你在配置文件中的配置的,end_log_pos 1569,這個塊在1569結束,也就是說結束點是1569。thread_id執行的線程id,exec_time執行時間,error_code錯誤碼

SET TIMESTAMP=1607322446/*!*/;
BEGIN
具體執行語句

 

截取部分日志:

 1 /*!*/;
 2 # at 1852
 3 #201207 14:27:30 server id 1  end_log_pos 1958 CRC32 0xf8660231     Query    thread_id=178    exec_time=0    error_code=0
 4 SET TIMESTAMP=1607322450/*!*/;
 5 INSERT INTO t1 VALUE(2,'val2')
 6 /*!*/;
 7 # at 1958
 8 #201207 14:27:30 server id 1  end_log_pos 1989 CRC32 0x3a49fcc1     Xid = 607
 9 COMMIT/*!*/;
10 # at 1989
11 #201207 14:27:36 server id 1  end_log_pos 2054 CRC32 0xa4d22816     Anonymous_GTID    last_committed=8    sequence_number=9    rbr_only=no
12 SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
13 # at 2054
14 #201207 14:27:36 server id 1  end_log_pos 2146 CRC32 0x11bdfbca     Query    thread_id=178    exec_time=0    error_code=0
15 SET TIMESTAMP=1607322456/*!*/;
16 drop database test
17 /*!*/;
18 # at 2146
19 #201207 14:27:42 server id 1  end_log_pos 2211 CRC32 0x9a60cd1d     Anonymous_GTID    last_committed=9    sequence_number=10    rbr_only=no
20 SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
21 # at 2211
22 #201207 14:27:42 server id 1  end_log_pos 2306 CRC32 0xd29068e1     Query    thread_id=178    exec_time=0    error_code=0
23 SET TIMESTAMP=1607322462/*!*/;
24 drop database test2
25 /*!*/;
26 SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
27 DELIMITER ;
28 # End of log file
29 /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
30 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

這里我們用搜索去找 drop 等關鍵字,來找到刪除操作的日志記錄位置

可以看到以上內容,在第16行和第24行,分別刪除了數據庫test和test2,之后進行恢復數據庫`test`操作,有兩種方法:

這里我使用的是第一種

 

1) 利用 drop database test 前的 “時間” 進行恢復,請注意時間格式

會產生一個警告,不用管,輸入數據庫root密碼進行驗證敲回車

1 [root@iZm5eajbhqhhehk85j4d95Z data]# /www/server/mysql/bin/mysqlbinlog -d test --stop-datetime="2020-12-07 14:27:36" mysql-bin.000001 | mysql -uroot -p;
2 Enter password: WARNING: The option --database has been used. It may filter parts of transactions, but will include the GTIDs in any case. If you want to exclude or include transactions, you should use the options --exclude-gtids or --include-gtids, respectively, instead.
3 
4 [root@iZm5eajbhqhhehk85j4d95Z data]# 

 

2) 利用 --stop-position="" 進行恢復

1 [root@iZm5eajbhqhhehk85j4d95Z data]# /www/server/mysql/bin/mysqlbinlog -d test --stop-position="2054" mysql-bin.000001 | mysql -uroot -p;
2 WARNING: The option --database has been used. It may filter parts of transactions, but will include the GTIDs in any case. If you want to exclude or include transactions, you should use the options --exclude-gtids or --include-gtids, respectively, instead.
3 Enter password: 
4 [root@iZm5eajbhqhhehk85j4d95Z data]#

再次進入數據庫,可以看到數據庫`test`以及數據就已經恢復了

 

5.

重置日志文件

使用 mysqlbinlog 恢復之后,日志文件會重復記錄前面的操作,此時可以選擇重置日志文件

mysql> reset master

再次查看二級制文件時,發現已經被重置了

 1 mysql> show binary logs;
 2 +------------------+-----------+
 3 | Log_name         | File_size |
 4 +------------------+-----------+
 5 | mysql-bin.000001 |      8079 |
 6 +------------------+-----------+
 7 1 row in set (0.00 sec)
 8 
 9 mysql> reset master;
10 Query OK, 0 rows affected (0.02 sec)
11 
12 mysql> show binary logs;
13 +------------------+-----------+
14 | Log_name         | File_size |
15 +------------------+-----------+
16 | mysql-bin.000001 |       154 |
17 +------------------+-----------+
18 1 row in set (0.00 sec)

 


免責聲明!

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



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