MySQL複製應用中繼日誌解析
作者 :沃趣科技高階資料庫專家 邱文輝
1.從一個大神那邊得到一張圖片,SQL執行緒應用中繼日誌流程,下面就實驗驗證一下:(PS,我個人認為這張圖binlog_format為ROW格式是正確的)
2.驗證有PK表情況
在主庫建立表結構
CREATE TABLE `table_pk` (
`id` int(11) NOT NULL ,
`name` varchar(20) NOT NULL ,
`age` tinyint NOT NULL ,
`sex` tinyint NOT NULL COMMENT '0,man,1,woman' ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
插入測試資料
insert into table_pk (`id`,`name`,`age`,`sex`) values(111,'zhangsan',20,0);
insert into table_pk (`id`,`name`,`age`,`sex`) values(222,'lisi',22,1);
insert into table_pk (`id`,`name`,`age`,`sex`) values(333,'wangwu',22,1);
insert into table_pk (`id`,`name`,`age`,`sex`) values(444,'lilei',32,0);
insert into table_pk (`id`,`name`,`age`,`sex`) values(555,'hanmeimei',30,1);
(dg6)[email protected] [(none)]> use mytest; (dg6)[email protected] [mytest]> select * from table_pk; +-----+-----------+-----+-----+ | id | name | age | sex | +-----+-----------+-----+-----+ | 111 | zhangsan | 20 | 0 | | 222 | lisi | 22 | 1 | | 333 | wangwu | 22| 1 | | 444 | lilei | 32 | 0 | | 555 | hanmeimei | 30 | 1 | +-----+-----------+-----+-----+ 5 rows in set (0.00 sec) (dg6)[email protected] [mytest]> show global variables like '%binlog_format%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | ROW | +---------------+-------+ 1 row in set (0.00 sec)
那麼我們去從庫看看
(dg7)[email protected] [mytest]> select * from table_pk; +-----+-----------+-----+-----+ | id | name | age | sex | +-----+-----------+-----+-----+ | 111 | zhangsan | 20 | 0 | | 222 | lisi | 22 | 1 | | 333 | wangwu | 22 | 1 | | 444 | lilei | 32 | 0 | | 555 | hanmeimei | 30 | 1 | +-----+-----------+-----+-----+ 5 rows in set (0.00 sec) (dg7)[email protected] [mytest]> show global variables like '%binlog_format%'; +--------------------------+-------------------+ | Variable_name | Value | +--------------------------+-------------------+ | binlog_format | ROW | +--------------------------+-------------------+ 8 rows in set (0.00 sec) (dg7)[email protected] [mytest]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.80.106 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: dg6-logbin.000001 Read_Master_Log_Pos: 4469 Relay_Log_File: dg7-relay-bin.000002 Relay_Log_Pos: 4681 Relay_Master_Log_File: dg6-logbin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 4469 Relay_Log_Space: 4883 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: b888e1ea-9739-11e4-a24e-000c29b24887 Master_Info_File: /data/mydata/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: b888e1ea-9739-11e4-a24e-000c29b24887:1-17 Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:1-9, b888e1ea-9739-11e4-a24e-000c29b24887:1-17 Auto_Position: 1 1 row in set (0.00 sec)
資料是複製過來的,MySQL主從複製是正常的,那麼我們為了驗證MySQL複製SQL執行緒是居於剛才那張圖的流程,有主鍵,就按主鍵更新匹配更新記錄。
那麼我們在從庫修改一行資料,故意製造不一致。
(dg7)[email protected] [mytest]> UPDATE `table_pk` SET `name`='laowang' WHERE `id`=333; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 (dg7)[email protected] [mytest]> select * from table_pk; +-----+-----------+-----+-----+ | id | name | age | sex | +-----+-----------+-----+-----+ | 111 | zhangsan | 20 | 0 | | 222 | lisi | 22 | 1 | | 333 | laowang | 22 | 1 | | 444 | lilei | 32 | 0 | | 555 | hanmeimei | 30 | 1 | +-----+-----------+-----+-----+ 5 rows in set (0.00 sec)
這時候主從資料不一致了
主庫 (dg6)[email protected] [mytest]> select * from table_pk where id=333; +-----+--------+-----+-----+ | id | name | age | sex | +-----+--------+-----+-----+ | 333 | wangwu | 22 | 1 | +-----+--------+-----+-----+ 1 row in set (0.00 sec) 從庫 (dg7)[email protected] [mytest]> select * from table_pk where id=333; +-----+---------+-----+-----+ | id | name | age | sex | +-----+---------+-----+-----+ | 333 | laowang | 22 | 1 | +-----+---------+-----+-----+ 1 row in set (0.00 sec) (dg7)[email protected] [mytest]>
那麼,我們在主庫更新一行資料。
(dg6)[email protected] [mytest]> UPDATE `table_pk` SET `name`='wangzi' WHERE `id`=333; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 (dg6)[email protected] [mytest]> select * from table_pk where id=333; +-----+--------+-----+-----+ | id | name | age | sex | +-----+--------+-----+-----+ | 333 | wangzi | 22 | 1 | +-----+--------+-----+-----+ 1 row in set (0.00 sec)
我們來看一下從庫狀態,是不是主庫的更新給複製過來了,見證奇蹟的時候到了
###############################################
(dg7)[email protected] [mytest]> select * from table_pk where id=333; +-----+---------+-----+-----+ | id | name | age | sex | +-----+---------+-----+-----+ | 333 | laowang | 22 | 1 | +-----+---------+-----+-----+ 1 row in set (0.00 sec)
######################### 神奇的是主庫的更新過來了############################################# (dg7)[email protected] [mytest]> select * from table_pk where id=333; +-----+--------+-----+-----+ | id | name | age | sex | +-----+--------+-----+-----+ | 333 | wangzi | 22 | 1 | +-----+--------+-----+-----+ 1 row in set (0.00 sec)
#########################那麼看一下MySQL主從複製狀態看看,也是正常的###################### (dg7)[email protected] [mytest]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.80.106 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: dg6-logbin.000001 Read_Master_Log_Pos: 5249 Relay_Log_File: dg7-relay-bin.000002 Relay_Log_Pos: 5461 Relay_Master_Log_File: dg6-logbin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 5249 Relay_Log_Space: 5663 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: b888e1ea-9739-11e4-a24e-000c29b24887 Master_Info_File: /data/mydata/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: b888e1ea-9739-11e4-a24e-000c29b24887:1-20 Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:1-11, b888e1ea-9739-11e4-a24e-000c29b24887:1-20 Auto_Position: 1 1 row in set (0.00 sec) (dg7)[email protected] [mytest]>
3.驗證沒有索引的情況
主庫建立表和插入記錄
CREATE TABLE `table_index` ( `id` int(11) NOT NULL, `name` varchar(20) NOT NULL, `age` tinyint(4) NOT NULL, `sex` tinyint(4) NOT NULL COMMENT '0,man,1,woman' ) ENGINE=InnoDB (dg6)[email protected] [mytest]> select * from table_index; +-----+-----------+-----+-----+ | id | name | age | sex | +-----+-----------+-----+-----+ | 111 | zhangsan | 20 | 0 | | 222 | lisi | 22 | 1 | | 333 | wangwu | 22 | 1 | | 444 | lilei | 32 | 0 | | 555 | hanmeimei | 30 | 1 | +-----+-----------+-----+-----+ 5 rows in set (0.00 sec) (dg6)[email protected] [mytest]>
從庫看看
(dg7)[email protected] [mytest]> select * from table_index; +-----+-----------+-----+-----+ | id | name | age | sex | +-----+-----------+-----+-----+ | 111 | zhangsan | 20 | 0 | | 222 | lisi | 22 | 1 | | 333 | wangwu | 22 | 1 | | 444 | lilei | 32 | 0 | | 555 | hanmeimei | 30 | 1 | +-----+-----------+-----+-----+ 5 rows in set (0.00 sec)
我們在從庫繼續搞破壞,把name為lisi的age修改為33,這時候主從已經不一致了。
(dg7)[email protected] [mytest]> select * from table_index where name='lisi';
+-----+------+-----+-----+
| id | name | age | sex |
+-----+------+-----+-----+
| 222 | lisi | 22 | 1 |
+-----+------+-----+-----+
1 row in set (0.00 sec)
(dg7)[email protected] [mytest]> update table_index set age=33 where name='lisi'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 (dg7)[email protected] [mytest]> select * from table_index where name='lisi'; +-----+---------+-----+-----+ | id | name | age | sex | +-----+---------+-----+-----+ | 222 | lisi | 33 | 1 | +-----+---------+-----+-----+ 1 row in set (0.00 sec) (dg7)[email protected] [mytest]>
那麼我們還是在主庫更新一下記錄。把lisi的age修改成30,看看從庫能不能更新過來
相關推薦
MySQL複製應用中繼日誌解析
作者 :沃趣科技高階資料庫專家 邱文輝 1.從一個大神那邊得到一張圖片,SQL執行緒應用中繼日誌流程,下面就實驗驗證一下:(PS,我個人認為這張圖binlog_format為ROW格式是正確的) 2.驗證有PK表情況 在主庫
MySQL bin日誌解析
目錄 一. BinLog簡介 二. mysqlbinlog 工具 三. 解析方式對比 很多時候,當我們的業務資料產生了不正常的變化,但卻無法得知這類操作是在哪裡進行,並且如何進行,單單從程式當面排查很費力。那麼就需要通過分析資料庫日誌來得到歷史執行SQL,根據SQL執行邏輯來確認程
45、mysql主從複製應用擴充套件
mysql主從複製應用擴充套件 1.主從伺服器的時間要同步(NTP) 建立一個時間伺服器 */5 * * * * /user/sbin/ntpdate ntpserver &> /dev/null 2.如何限制從伺服器只讀(配置在mysqld中) 在從伺服器上啟動read
mysql日誌解析工具canal介紹
文章轉自https://github.com/alibaba/canal/wiki/Introduction 背景 早期,阿里巴巴B2B公司因為存在杭州和美國雙機房部署,存在跨機房同步的業務需求。不過早期的資料庫同步業務,主要是基於trigger的方式獲取增量變更,不
mysql之 日誌體系(錯誤日誌、查詢日誌、二進位制日誌、事務日誌、中繼日誌)
一、 mysql錯誤日誌: 錯誤日誌記錄的事件: a)、伺服器啟動關閉過程中的資訊 b)、伺服器執行過程中的錯誤資訊 c)、事件偵錯程式執行一個事件時間生的資訊 d)、在從伺服器上啟動從伺服器程序時產生的資訊 log_error引數指定錯誤日誌存放路徑以及檔名。如果不指定檔
mysql複製環境清理二進位制日誌
1.檢視slave上正在讀取的日誌 mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_
mysql binlog解析(mysql一共四種日誌型別)
概述 MySQL關於Binlog的官方文件:The Binary Log 什麼是 Binlog MySQL Server 有四種類型的日誌——Error Log、General Query Log、Binary Log 和 Slow Query Log。 第一個是錯誤日誌,記錄 mysqld 的一些錯誤
canal 基於Mysql資料庫增量日誌解析
canal 基於Mysql資料庫增量日誌解析 1.前言 最近太多事情 工作的事情,以及終身大事等等 耽誤更新,由於最近做專案需要同步監聽 未來電視 mysql的變更瞭解到公司會用canal做增量監聽,就嘗試使用了一下 這裡做個demo 簡單的記錄一下。 2.canal簡介 canal:主要用途是基於
MySQL複製(二)--基於二進位制日誌檔案(binlog)配置複製
基礎環境: 主庫 從庫 伺服器IP地址 192.168.10.11 192.168.10.12 版本 5.7.24 5.7.24 已存在的資料庫 mysql> show databases; +--------------------+ | Databa
MySQL基礎應用
命令使用1.登入到MySQL服務器[[email protected]/* */ ~]# mysql -u root -pEnter password: //根據提示輸入正確的密碼2.執行MySQL操作語句以root登入打“mysql>”環境後,執行"show master l
Jquery回調函數應用實例解析
light wid cnblogs 動作 body span 廣泛 使用 我們 jQuery代碼中對回調函數有著廣泛的應用,對其有精準的理解是非常有必要的,下面就通過實例對此方法進行簡單的介紹。 代碼實例如下: 利用回調函數,當div全部隱藏之後彈出一個提示框。 &l
MySQL配置文件簡單解析
spa art ini update rep 模式 buffer lda reads 1 [mysqld] 2 basedir = /data/mysql 3 datadir = /data/mysqldata 4 tmpd
MySQL二進制日誌(binary log)總結
何事 mysqld 支持 col mysql數據庫 內存大小 報錯 正常 ora 本文出處:http://www.cnblogs.com/wy123/p/7182356.html (保留出處並非什麽原創作品權利,本人拙作還遠遠達不到,僅僅是為了鏈接到原文,因為後續對可
學會讀懂 MySql 的慢查詢日誌
操作 get time user net 假死 幸運 存在 報錯 在前邊的博客《何時、怎樣開啟 MySql 日誌?》中,我們了解到了怎樣啟用 MySql 的慢查詢日誌。今天我們來看一下怎樣去讀懂這些慢查詢日誌。在跟蹤慢查詢日誌之前。首先你得保證最少發生過一次慢查詢。假設
MySQL 死鎖與日誌二三事
mysql索引 open 靜態變量 ... 硬盤 永久 state stack 應該 最近線上 MySQL 接連發生了幾起數據異常,都是在淩晨爆發,由於業務場景屬於典型的數據倉庫型應用,白天壓力較小無法復現。甚至有些異常還比較詭異,最後 root cause 分析頗費周
MySQL 基本應用 count() 與 group by
技術 .cn ima count() 應用 nbsp .com count 基本 MySQL 基本應用 count() 與 group by
MySQL 相關知識細節及解析
開始 細節 strong 相關 方式 新建 delet 使用 mysql 1,刪除表中所有記錄使用delete from 表名;還是用truncate table 表名 刪除方式:delete 一條一條刪除,不清空auto_increment記錄數
linux中MySQL的bin-log日誌格式更改
ges form 格式 .com nbsp images onf logs png 查看與配置binlog格式 1.查看binlog_format 進入mysql show variables like ‘binlog_format‘ 修改binlog日誌格式 修改配置文
F5負載的應用IIS日誌記錄的不是真實IP的處理方法
mage 日誌 .cn 如果 應該 沒有 名稱 技術 https 如果沒有這一項,在服務裏添加上 將F5XForwardedFor.dll拷貝到應用目錄下 添加篩選器: 名稱:F5XForwardedFo
MySQL都有哪些日誌?分別都代表什麽?
mysql日誌MySQL日誌:主要包含:錯誤日誌、查詢日誌、慢查詢日誌、事務日誌、二進制日誌、中繼日誌;日誌是mysql數據庫的重要組成部分。日誌文件中記錄著mysql數據庫運行期間發生的變化;也就是說用來記錄mysql數據庫的客戶端連接狀況、SQL語句的執行情況和錯誤信息等。當數據庫遭到意外的損壞時,可以通