歡迎你們前往雲+社區,獲取更多騰訊海量技術實踐乾貨哦~php
做者:騰訊雲數據庫內核團隊 html
原文標題:【騰訊雲CDB】深刻解析MySQL binlogmysql
binlog是Mysql sever層維護的一種二進制日誌,與innodb引擎中的redo/undo log是徹底不一樣的日誌;其主要是用來記錄對mysql數據更新或潛在發生更新的SQL語句,並以"事務"的形式保存在磁盤中;sql
做用主要有:數據庫
binlog信息查詢binlog開啓後,能夠在配置文件中查看其位置信息,也能夠在myslq命令行中查看:
show variables like '%log_bin%';
+---------------------------------+-------------------------------------+
| Variable_name | Value |
+---------------------------------+-------------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/3306/mysql-bin |
| log_bin_index | /var/lib/mysql/3306/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------------------------------------+複製代碼
binlog文件開啓binlog後,會在數據目錄(默認)生產host-bin.n(具體binlog信息)文件及host-bin.index索引文件(記錄binlog文件列表)。當binlog日誌寫滿(binlog大小max_binlog_size,默認1G),或者數據庫重啓纔會生產新文件,可是也可經過手工進行切換讓其從新生成新的文件(flush logs);另外,若是正使用大的事務,因爲一個事務不能橫跨兩個文件,所以也可能在binlog文件未滿的狀況下刷新文件
mysql> show binary logs; //查看binlog文件列表,
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 177 |
| mysql-bin.000002 | 177 |
| mysql-bin.000003 | 10343266 |
| mysql-bin.000004 | 10485660 |
| mysql-bin.000005 | 53177 |
| mysql-bin.000006 | 2177 |
| mysql-bin.000007 | 1383 |
+------------------+-----------+複製代碼
查看binlog的狀態:show master status可查看當前二進制日誌文件的狀態信息,顯示正在寫入的二進制文件,及當前position
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+複製代碼
默認狀況下binlog日誌是二進制格式,沒法直接查看。可以使用兩種方式進行查看:bash
a. mysqlbinlog: /usr/bin/mysqlbinlog mysql-bin.000007
- mysqlbinlog是mysql官方提供的一個binlog查看工具,
- 也可以使用–read-from-remote-server從遠程服務器讀取二進制日誌,
- 還可以使用--start-position --stop-position、--start-time= --stop-time精確解析binlog日誌
截取位置1190-1352 binlog以下:
***************************************************************************************
# at 1190 //事件的起點
#171223 21:56:26 server id 123 end_log_pos 1190 CRC32 0xf75c94a7 Intvar
SET INSERT_ID=2/*!*/;
#171223 21:56:26 server id 123 end_log_pos 1352 CRC32 0xefa42fea Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1514123786/*!*/; //開始事務的時間起點 (每一個at即爲一個event)
insert into tb_person set name="name__2", address="beijing", sex="man", other="nothing" //sql語句
/*!*/;
# at 1352
#171223 21:56:26 server id 123 end_log_pos 1383 CRC32 0x72c565d3 Xid = 5 //執行時間,及位置戳,Xid:事件指示提交的XA事務
***************************************************************************************
b.直命令行解析
SHOW BINLOG EVENTS
[IN 'log_name'] //要查詢的binlog文件名
[FROM pos]
[LIMIT [offset,] row_count]
1190-135以下:mysql> show binlog events in 'mysql-bin.000007' from 1190 limit 2\G
*************************** 13. row ***************************
Log_name: mysql-bin.000007
Pos: 1190
Event_type: Query //事件類型
Server_id: 123
End_log_pos: 1352 //結束pose點,下個事件的起點
Info: use `test`; insert into tb_person set name="name__2", address="beijing", sex="man", other="nothing"
*************************** 14. row ***************************
Log_name: mysql-bin.000007
Pos: 1352
Event_type: Xid
Server_id: 123
End_log_pos: 1383
Info: COMMIT /* xid=51 */複製代碼
Mysql binlog日誌有ROW,Statement,MiXED三種格式;可經過my.cnf配置文件及 ==set global binlog_format='ROW/STATEMENT/MIXED'== 進行修改,命令行 ==show variables like 'binlog_format'== 命令查看binglog格式;。服務器
複製是mysql最重要的功能之一,mysql集羣的高可用、負載均衡和讀寫分離都是基於複製來實現的;從5.6開始複製有兩種實現方式,基於binlog和基於GTID(全局事務標示符);本文接下來將介紹基於binlog的一主一從複製;其複製的基本過程以下:負載均衡
a.Master將數據改變記錄到二進制日誌(binary log)中
b.Slave上面的IO進程鏈接上Master,並請求從指定日誌文件的指定位置(或者從最開始的日誌)以後的日誌內容
c.Master接收到來自Slave的IO進程的請求後,負責複製的IO進程會根據請求信息讀取日誌指定位置以後的日誌信息,返回給Slave的IO進程。
返回信息中除了日誌所包含的信息以外,還包括本次返回的信息已經到Master端的bin-log文件的名稱以及bin-log的位置
d.Slave的IO進程接收到信息後,將接收到的日誌內容依次添加到Slave端的relay-log文件的最末端,並將讀取到的Master端的 bin-log的
文件名和位置記錄到master-info文件中,以便在下一次讀取的時候可以清楚的告訴Master從某個bin-log的哪一個位置開始日後的日誌內容
e.Slave的Sql進程檢測到relay-log中新增長了內容後,會立刻解析relay-log的內容成爲在Master端真實執行時候的那些可執行的內容,並在自身執行複製代碼
接下來使用實例演示基於binlog的主從複製:函數
a.配置master
主要包括設置複製帳號,並授予REPLICATION SLAVE權限,具體信息會存儲在於master.info文件中,及開啓binlog;
mysql> CREATE USER 'test'@'%' IDENTIFIED BY '123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'test'@'%';
mysql> show variables like "log_bin";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
查看master當前binlogmysql狀態:mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
建表插入數據:
CREATE TABLE `tb_person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(36) NOT NULL,
`address` varchar(36) NOT NULL DEFAULT '',
`sex` varchar(12) NOT NULL DEFAULT 'Man' ,
`other` varchar(256) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
insert into tb_person set name="name1", address="beijing", sex="man", other="nothing";
insert into tb_person set name="name2", address="beijing", sex="man", other="nothing";
insert into tb_person set name="name3", address="beijing", sex="man", other="nothing";
insert into tb_person set name="name4", address="beijing", sex="man", other="nothing";
b.配置slave
Slave的配置相似master,需額外設置relay_log參數,slave沒有必要開啓二進制日誌,若是slave爲其它slave的master,須設置bin_log
c.鏈接master
mysql> CHANGE MASTER TO
MASTER_HOST='10.108.111.14',
MASTER_USER='test',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=120;
d.show slave status;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: ---------------------------- slave io狀態,表示還未啓動
Master_Host: 10.108.111.14
Master_User: test
Master_Port: 20126
Connect_Retry: 60 ------------------------- master宕機或鏈接丟失從服務器線程從新嘗試鏈接主服務器以前睡眠時間
Master_Log_File: mysql-bin.000003 ------------ 當前讀取master binlog文件
Read_Master_Log_Pos: 120 ------------------------- slave讀取master binlog文件位置
Relay_Log_File: relay-bin.000001 ------------ 回放binlog
Relay_Log_Pos: 4 -------------------------- 回放relay log位置
Relay_Master_Log_File: mysql-bin.000003 ------------ 回放log對應maser binlog文件
Slave_IO_Running: No
Slave_SQL_Running: No
Exec_Master_Log_Pos: 0 --------------------------- 相對於master從庫的sql線程執行到的位置
Seconds_Behind_Master: NULL
Slave_IO_State, Slave_IO_Running, 和Slave_SQL_Running爲NO說明slave尚未開始複製過程。
e.啓動複製
start slave
f.再次觀察slave狀態
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event -- 等待master新的event
Master_Host: 10.108.111.14
Master_User: test
Master_Port: 20126
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 3469 ---------------------------- 3469 等於Exec_Master_Log_Pos,已完成回放
Relay_Log_File: relay-bin.000002 ||
Relay_Log_Pos: 1423 ||
Relay_Master_Log_File: mysql-bin.000003 ||
Slave_IO_Running: Yes ||
Slave_SQL_Running: Yes ||
Exec_Master_Log_Pos: 3469 -----------------------------3469 等於slave讀取master binlog位置,已完成回放
Seconds_Behind_Master: 0
可看到slave的I/O和SQL線程都已經開始運行,並且Seconds_Behind_Master=0。Relay_Log_Pos增長,意味着一些事件被獲取並執行了。
最後看下如何正確判斷SLAVE的延遲狀況,斷定slave是否追上master的binlog:
一、首先看 Relay_Master_Log_File 和 Maser_Log_File 是否有差別;
二、若是Relay_Master_Log_File 和 Master_Log_File 是同樣的話,再來看Exec_Master_Log_Pos 和 Read_Master_Log_Pos 的差別,對比SQL線程比IO線程慢了多少個binlog事件;
三、若是Relay_Master_Log_File 和 Master_Log_File 不同,那說明延遲可能較大,須要從MASTER上取得binlog status,判斷當前的binlog和MASTER上的差距;
四、若是以上都不能發現問題,可以使用pt_heartbeat工具來監控主備複製的延遲。
g.查詢slave數據,主從一致
mysql> select * from tb_person;
+----+-------+---------+-----+---------+
| id | name | address | sex | other |
+----+-------+---------+-----+---------+
| 5 | name4 | beijing | man | nothing |
| 6 | name2 | beijing | man | nothing |
| 7 | name1 | beijing | man | nothing |
| 8 | name3 | beijing | man | nothing |
+----+-------+---------+-----+---------+
關於mysql複製的內容還有不少,好比不一樣的同步方式、複製格式狀況下有什麼區別,有什麼特色,應該在什麼狀況下使用....這裏再也不一一介紹。複製代碼
恢復是binlog的兩大主要做用之一,接下來經過實例演示如何利用binlog恢復數據:
a.首先,看下當前binlog位置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 | 1847 | | | |
+------------------+----------+--------------+------------------+-------------------+
b.向表tb_person中插入兩條記錄:
insert into tb_person set name="person_1", address="beijing", sex="man", other="test-1";
insert into tb_person set name="person_2", address="beijing", sex="man", other="test-2";
c.記錄當前binlog位置:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 | 2585 | | | |
+------------------+----------+--------------+------------------+-------------------+
d.查詢數據
mysql> select * from tb_person where name ="person_2" or name="person_1";
+----+----------+---------+-----+--------+
| id | name | address | sex | other |
+----+----------+---------+-----+--------+
| 6 | person_1 | beijing | man | test-1 |
| 7 | person_2 | beijing | man | test-2 |
+----+----------+---------+-----+--------+
e.刪除一條: delete from tb_person where name ="person_2";
mysql> select * from tb_person where name ="person_2" or name="person_1";
+----+----------+---------+-----+--------+
| id | name | address | sex | other |
+----+----------+---------+-----+--------+
| 6 | person_1 | beijing | man | test-1 |
+----+----------+---------+-----+--------+
f. binlog恢復(指定pos點恢復/部分恢復)
mysqlbinlog --start-position=1847 --stop-position=2585 mysql-bin.000008 > test.sql
mysql> source /var/lib/mysql/3306/test.sql
d.數據恢復完成
mysql> select * from tb_person where name ="person_2" or name="person_1";
+----+----------+---------+-----+--------+
| id | name | address | sex | other |
+----+----------+---------+-----+--------+
| 6 | person_1 | beijing | man | test-1 |
| 7 | person_2 | beijing | man | test-2 |
+----+----------+---------+-----+--------+
e.總結
恢復,就是讓mysql將保存在binlog日誌中指定段落區間的sql語句逐個從新執行一次而已複製代碼
本文簡要介紹binlog原理及其在恢復、複製中的使用方法;工具
此文已由做者受權雲加社區發佈,轉載請註明文章出處;