MySQL資料庫恢復到指定時間點時,我們必須透過MySQL全備+MySQL增量備份(可選)+MySQL的二進位制日誌(binlog)進行重放來恢復到指定時間點,實際的生產環境中,可能一段時間內生成了多個二進位制日誌檔案(binlog), MySQL本身不會儲存二進位制日誌檔案(binlog)的開始時間和結束時間,如果要還原到某個時間點,我們需要知道還原後重放哪些二進位制日誌檔案。那麼就必須獲取二進位制日誌(binlog)的開始時間和結束時間。那麼我們如何獲取MySQL二進位制日誌檔案(binlog)的開始時間和結束時間呢?下面簡單總結幾個方法,以供參考。
1:透過xtrabackup_info檔案獲取
Xtrabckup還原全備或增量備份時,會生成一個xtrabackup_info檔案,如下所示:
$ more xtrabackup_info
uuid = 3bd8a0f7-ea2f-11ed-9896-00505697b437
name =
tool_name = xtrabackup
tool_command = --defaults-file=/data/conf/my.cnf --login-path=**** --backup --target-dir=/db_backup/mysql_backup/db_backup/backup_cycle_2023_04_30/full_backup_2023_05_04_11_53_25
tool_version = 8.0.31-24
ibbackup_version = 8.0.31-24
server_version = 8.0.31
start_time = 2023-05-04 11:53:26
end_time = 2023-05-04 11:53:30
lock_time = 0
binlog_pos = filename 'mysql_binlog.000042', position '289', GTID of the last change 'd01ecb4f-c944-11ed-9896-00505697b437:1-111:100
0108-1953894,d01edb91-c944-11ed-9896-00505697b437:1-5,f8ef839e-c942-11ed-9bd2-00505697b437:1-13'
innodb_from_lsn = 0
innodb_to_lsn = 824007891
partial = N
incremental = N
format = file
compressed = N
encrypted = N
如上所示,你可以看到一行關於binlog_pos的資訊:binlog檔名為mysql_binlog.000042,起始位置為289,我們應用MySQL二進位制日誌(binlog)可以從這個二進位制日誌開始:
mysqlbinlog --start-position="289" ./mysql_binlog.000042 > restore.sql
這個方法有侷限性,只能被動獲取我們需要重放二進位制日誌的檔名和開始位置。這裡僅供參考。
2:透過mysqlbinlog解析獲取
如下所示,我們想知道mysql_binlog.000042什麼時候開始生成,什麼時候開始結束的,其實MySQL二進位制日誌裡面包含有這樣的資訊,如下所示:
$ mysqlbinlog mysql_binlog.000042 | head -10
# The proper term is pseudo_replica_mode, but we use this compatibility alias
# to make the statement usable on server versions 8.0.24 and older.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#230504 11:53:30 server id 1 end_log_pos 126 Start: binlog v 4, server v 8.0.31 created 230504 11:53:30
BINLOG '
OixTZA8BAAAAegAAAH4AAAAAAAQAOC4wLjMxAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEwANAAgAAAAABAAEAAAAYgAEGggAAAAICAgCAAAACgoKKioAEjQA
我們可以透過獲取關鍵字Start: binlog獲取這一行的資訊,從而獲取MySQL二進位制日誌(binlog)的開始時間,如下所示:
$ mysqlbinlog mysql_binlog.000042 |grep "Start: binlog"
#230504 11:53:30 server id 1 end_log_pos 126 Start: binlog v 4, server v 8.0.31 created 230504 11:53:30
$ mysqlbinlog mysql_binlog.000042 |grep "Start: binlog" | awk -F "server id" '{print $1}'
#230504 11:53:30
如上所示,生成mysql_binlog.000042的時間點為230504 11:53:30,這裡年份使用了縮寫模式,即2023縮寫為23,230504代表的是2023-05-04.
如果要獲取二進位制日誌(binlog)的結束時間,這個還要看二進位制日誌是正常迴圈結束還是例項關閉結束,它們會對應不同的資訊
STOP_EVENT
A STOP_EVENT has not payload or post-header
ROTATE_EVENT
The rotate event is added to the binlog as last event to tell the reader what binlog to request next.
如下所示:
$ mysqlbinlog mysql_binlog.000042 | tail -10
# at 533
#230504 12:13:35 server id 3 end_log_pos 560 Xid = 50948183
COMMIT/*!*/;
# at 560
#230504 12:14:03 server id 1 end_log_pos 606 Rotate to mysql_binlog.000043 pos: 4
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
$ mysqlbinlog mysql_binlog.000042 |grep Rotate
#230504 12:14:03 server id 1 end_log_pos 606 Rotate to mysql_binlog.000043 pos: 4
$ mysqlbinlog mysql_binlog.000042 |grep Rotate | awk -F "server id" '{print $1}'
#230504 12:14:03
如果MySQL關閉後,對應的MySQL二進位制日誌的格式如下所示:
$ mysqlbinlog mysql_binlog.000045 | tail -10
# at 126
#230504 17:02:23 server id 10 end_log_pos 157 CRC32 0x2d378ba5 Previous-GTIDs
# [empty]
# at 157
#230504 17:08:11 server id 10 end_log_pos 180 CRC32 0xb6b08f2c Stop
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
$ mysqlbinlog mysql_binlog.000045 | grep Stop | awk -F "server id" '{print $1}'
方法3:stat命令檢視建立時間
在Linux平臺,我們可以透過stat命令檢視檔案的建立時間,如下所示
$ stat mysql_binlog.000042
File: mysql_binlog.000042
Size: 606 Blocks: 8 IO Block: 4096 regular file
Device: fd08h/64776d Inode: 100663444 Links: 1
Access: (0640/-rw-r-----) Uid: ( 801/ mysql) Gid: ( 800/ mysql)
Access: 2023-05-04 13:36:54.872910222 +0800
Modify: 2023-05-04 12:14:18.712310369 +0800
Change: 2023-05-04 12:14:18.712310369 +0800
Birth: 2023-05-04 11:53:30.640989646 +0800
如上所示,我們可以透過Birth欄位資訊,知道mysql_binlog.000042是2023-05-04 11:53:30建立的。但是我們沒法獲取二進位制日誌的結束時間,不過由於MySQL的二進位制日誌檔案是有數字序列的,所以我們可以透過下一個二進位制日誌檔案(binlog)的開始時間來判斷上一個二進位制日誌檔案(binlog)的結束時間。
$ stat mysql_binlog.000043
File: mysql_binlog.000043
Size: 335 Blocks: 8 IO Block: 4096 regular file
Device: fd08h/64776d Inode: 100663430 Links: 1
Access: (0640/-rw-r-----) Uid: ( 801/ mysql) Gid: ( 800/ mysql)
Access: 2023-05-04 13:36:58.524978710 +0800
Modify: 2023-05-04 16:55:07.161545830 +0800
Change: 2023-05-04 16:55:07.161545830 +0800
Birth: 2023-05-04 12:14:18.713310387 +0800