MySQL复制应用中继日志解析

时间:2023-03-09 13:36:08
MySQL复制应用中继日志解析

前言:SQL线程应用中继日志,在binlog_format是row格式的时候,是居于主键更新,下面结合一张图来证明

1.从一个大神那边得到一张图片,SQL线程应用中继日志流程,下面就实验验证一下:(PS,我个人认为这张图binlog_format为ROW格式是正确的)

MySQL复制应用中继日志解析

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)root@localhost [(none)]> use mytest;

(dg6)root@localhost [mytest]> select * from table_pk;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg6)root@localhost [mytest]> show global variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
row in set (0.00 sec)

那么我们去从库看看

(dg7)root@localhost [mytest]> select * from table_pk;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg7)root@localhost [mytest]> show global variables like '%binlog_format%';
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| binlog_format | ROW |
+--------------------------+-------------------+
rows in set (0.00 sec) (dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec)

数据是复制过来的,MySQL主从复制是正常的,那么我们为了验证MySQL复制SQL线程是居于刚才那张图的流程,有主键,就按主键更新匹配更新记录。

那么我们在从库修改一行数据,故意制造不一致。

(dg7)root@localhost [mytest]> UPDATE `table_pk` SET `name`='laowang' WHERE `id`=333;
Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: (dg7)root@localhost [mytest]> select * from table_pk;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | laowang | | |
| | lilei | | |
| | hanmeimei | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec)

这时候主从数据不一致了

主库
(dg6)root@localhost [mytest]> select * from table_pk where id=;
+-----+--------+-----+-----+
| id | name | age | sex |
+-----+--------+-----+-----+
| | wangwu | | |
+-----+--------+-----+-----+
row in set (0.00 sec) 从库 (dg7)root@localhost [mytest]> select * from table_pk where id=;
+-----+---------+-----+-----+
| id | name | age | sex |
+-----+---------+-----+-----+
| | laowang | | |
+-----+---------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]>

那么,我们在主库更新一行数据。

(dg6)root@localhost [mytest]> UPDATE `table_pk` SET `name`='wangzi' WHERE `id`=;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: (dg6)root@localhost [mytest]> select * from table_pk where id=;
+-----+--------+-----+-----+
| id | name | age | sex |
+-----+--------+-----+-----+
| | wangzi | | |
+-----+--------+-----+-----+
row in set (0.00 sec)

我们来看一下从库状态,是不是主库的更新给复制过来了,见证奇迹的时候到了

###############################################
(dg7)root@localhost [mytest]> select * from table_pk where id=;
+-----+---------+-----+-----+
| id | name | age | sex |
+-----+---------+-----+-----+
| | laowang | | |
+-----+---------+-----+-----+
row in set (0.00 sec)
######################### 神奇的是主库的更新过来了############################################# (dg7)root@localhost [mytest]> select * from table_pk where id=;
+-----+--------+-----+-----+
| id | name | age | sex |
+-----+--------+-----+-----+
| | wangzi | | |
+-----+--------+-----+-----+
row in set (0.00 sec)

#########################那么看一下MySQL主从复制状态看看,也是正常的######################
(dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec) (dg7)root@localhost [mytest]>

3.验证没有索引的情况

主库创建表和插入记录

 CREATE TABLE `table_index` (
`id` int() NOT NULL,
`name` varchar() NOT NULL,
`age` tinyint() NOT NULL,
`sex` tinyint() NOT NULL COMMENT '0,man,1,woman'
) ENGINE=InnoDB (dg6)root@localhost [mytest]> select * from table_index;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg6)root@localhost [mytest]>

从库看看

(dg7)root@localhost [mytest]> select * from table_index;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec)

我们在从库继续搞破坏,把name为lisi的age修改为33,这时候主从已经不一致了。

(dg7)root@localhost [mytest]> select * from table_index where name='lisi';
+-----+------+-----+-----+
| id  | name | age | sex |
+-----+------+-----+-----+
| 222 | lisi |  22 |   1 |
+-----+------+-----+-----+
1 row in set (0.00 sec) (dg7)root@localhost [mytest]> update table_index set age= where name='lisi';
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: (dg7)root@localhost [mytest]> select * from table_index where name='lisi';
+-----+---------+-----+-----+
| id | name | age | sex |
+-----+---------+-----+-----+
| | lisi | | |
+-----+---------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]>

那么我们还是在主库更新一下记录。把lisi的age修改成30,看看从库能不能更新过来

(dg6)root@localhost [mytest]> select * from table_index where name='lisi';
+-----+------+-----+-----+
| id | name | age | sex |
+-----+------+-----+-----+
| | lisi | | |
+-----+------+-----+-----+
row in set (0.00 sec) (dg6)root@localhost [mytest]> update table_index set age= where name='lisi';
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: (dg6)root@localhost [mytest]> select * from table_index where name='lisi';
+-----+------+-----+-----+
| id | name | age | sex |
+-----+------+-----+-----+
| | lisi | | |
+-----+------+-----+-----+
row in set (0.00 sec) (dg6)root@localhost [mytest]>

回到从库看看,数据没有更新过来,lisi的年龄还是33,这时候主从复制也是异常的,提示1032错误(找不到记录)

(dg7)root@localhost [mytest]> select * from table_index where name='lisi';
+-----+------+-----+-----+
| id | name | age | sex |
+-----+------+-----+-----+
| | lisi | | |
+-----+------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error: Could not execute Update_rows event on table mytest.table_index; Can't find record in 'table_index', Error_code: 1032; Corrupted replication event was detected, Error_code: 1610; handler error HA_ERR_END_OF_FILE; the event's master log dg6-logbin., end_log_pos
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error: Could not execute Update_rows event on table mytest.table_index; Can't find record in 'table_index', Error_code: 1032; Corrupted replication event was detected, Error_code: 1610; handler error HA_ERR_END_OF_FILE; the event's master log dg6-logbin., end_log_pos
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: ::
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec) (dg7)root@localhost [mytest]>

4.验证有唯一索引情况

测试方法都一样,下面步骤我都就贴结果了。(核心思想就是,从库先修改记录,做成主从数据不一致这种情况,然后主库再更新,看看从库有没有同步主库记录)

(dg6)root@localhost [mytest]> select * from table_index;
+-----+-----+-----------+-----+-----+
| id | sid | name | age | sex |
+-----+-----+-----------+-----+-----+
| | | zhangsan | | |
| | | lisi | | |
| | | wangzi | | |
| | | lilei | | |
| | | hanmeimei | | |
+-----+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg6)root@localhost [mytest]> select * from table_index where sid=;
+-----+-----+--------+-----+-----+
| id | sid | name | age | sex |
+-----+-----+--------+-----+-----+
| | | wangzi | | |
+-----+-----+--------+-----+-----+
row in set (0.00 sec) (dg6)root@localhost [mytest]> update table_index set name='wangwu' where sid=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (dg6)root@localhost [mytest]> select * from table_index where sid=;
+-----+-----+--------+-----+-----+
| id | sid | name | age | sex |
+-----+-----+--------+-----+-----+
| | | wangwu | | |
+-----+-----+--------+-----+-----+
row in set (0.00 sec) (dg6)root@localhost [mytest]>

从库看看,能更新过来,而且主从复制状态是正常的

(dg7)root@localhost [mytest]> select * from table_index;
+-----+-----+-----------+-----+-----+
| id | sid | name | age | sex |
+-----+-----+-----------+-----+-----+
| | | zhangsan | | |
| | | lisi | | |
| | | wangzi | | |
| | | lilei | | |
| | | hanmeimei | | |
+-----+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg7)root@localhost [mytest]> update table_index set name='laowang' where sid=;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: (dg7)root@localhost [mytest]> select * from table_index where sid=;
+-----+-----+---------+-----+-----+
| id | sid | name | age | sex |
+-----+-----+---------+-----+-----+
| | | laowang | | |
+-----+-----+---------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec) (dg7)root@localhost [mytest]> select * from table_index where sid=;
+-----+-----+--------+-----+-----+
| id | sid | name | age | sex |
+-----+-----+--------+-----+-----+
| | | wangwu | | |
+-----+-----+--------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec) (dg7)root@localhost [mytest]>

5.验证有主键和有普通索引情况

(dg6)root@localhost [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg6)root@localhost [mytest]> update table_key set name='zhangsir' where age=;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings: (dg6)root@localhost [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsir | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg6)root@localhost [mytest]>

从库看看

(dg7)root@localhost [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg7)root@localhost [mytest]> desc update table_key set name='xiaozhang' where age=;
+----+-------------+-----------+-------+---------------+-----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+-----------+---------+-------+------+-------------+
| | SIMPLE | table_key | range | age_index | age_index | | const | | Using where |
+----+-------------+-----------+-------+---------------+-----------+---------+-------+------+-------------+
row in set (0.00 sec) (dg7)root@localhost [mytest]> update table_key set name='xiaozhang' where age=;
Query OK, row affected (0.03 sec)
Rows matched: Changed: Warnings: (dg7)root@localhost [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | xiaozhang | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg7)root@localhost [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsir | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec) (dg7)root@localhost [mytest]>

6.验证只有普通索引情况

主库

 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',
key age_index (`age`)
  ) ENGINE=InnoDB
(dg6)root@localhost [mytest]>select * from table_key where age=;
+-----+----------+-----+-----+
| id | name | age | sex |
+-----+----------+-----+-----+
| | zhangsir | | |
+-----+----------+-----+-----+
row in set (0.00 sec) (dg6)root@localhost [mytest]>update table_key set name='zhaoliu' where age=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (dg6)root@localhost [mytest]>select * from table_key where age=;
+-----+---------+-----+-----+
| id | name | age | sex |
+-----+---------+-----+-----+
| | zhaoliu | | |
+-----+---------+-----+-----+
row in set (0.00 sec)

从库

(dg7)root@localhost [mytest]> select * from table_key where age=;
+-----+----------+-----+-----+
| id | name | age | sex |
+-----+----------+-----+-----+
| | zhangsir | | |
+-----+----------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]> update table_key set name='zhangsan' where age=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (dg7)root@localhost [mytest]> select * from table_key where age=;
+-----+----------+-----+-----+
| id | name | age | sex |
+-----+----------+-----+-----+
| | zhangsan | | |
+-----+----------+-----+-----+
row in set (0.00 sec) (dg7)root@localhost [mytest]> select * from table_key where age=;
+-----+----------+-----+-----+
| id | name | age | sex |
+-----+----------+-----+-----+
| | zhangsan | | |
+-----+----------+-----+-----+
row in set (0.00 sec)
##########################提示1032错误,找不到记录 (dg7)root@localhost [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.106
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg6-logbin.
Read_Master_Log_Pos:
Relay_Log_File: dg7-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg6-logbin.
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error: Could not execute Update_rows event on table mytest.table_key; Can't find record in 'table_key', Error_code: 1032; Column 'name' cannot be null, Error_code: 1048; Column 'age' cannot be null, Error_code: 1048; Column 'sex' cannot be null, Error_code: 1048; handler error HA_ERR_END_OF_FILE; the event's master log dg6-logbin., end_log_pos
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error: Could not execute Update_rows event on table mytest.table_key; Can't find record in 'table_key', Error_code: 1032; Column 'name' cannot be null, Error_code: 1048; Column 'age' cannot be null, Error_code: 1048; Column 'sex' cannot be null, Error_code: 1048; handler error HA_ERR_END_OF_FILE; the event's master log dg6-logbin., end_log_pos
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: b888e1ea--11e4-a24e-000c29b24887
Master_Info_File: /data/mydata/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: ::
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: b888e1ea--11e4-a24e-000c29b24887:-
Executed_Gtid_Set: a9926b45-975d-11e4-a339-000c29b24888:-,
b888e1ea--11e4-a24e-000c29b24887:-
Auto_Position:
row in set (0.00 sec) (dg7)root@localhost [mytest]>

7.binlog格式是sbr,mbr格式的时候(PS,因为我使用了GTID,所以找了另外两台机测试)

主库

(dg1)root@127.0.0.1 [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg1)root@127.0.0.1 [mytest]> show global variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
row in set (0.00 sec) (dg1)root@127.0.0.1 [mytest]> select * from table_key;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangsan | | |
| | lisi | | |
| | wangwu | | |
| | lilei | | |
| | hanmeimei | | |
| | lucy | | |
| | lili | | |
| | lintao | | |
+-----+-----------+-----+-----+
rows in set (0.00 sec) (dg1)root@127.0.0.1 [mytest]> update table_key set name='zhangzong' where age=;
Query OK, row affected (0.01 sec)
Rows matched: Changed: Warnings:

从库看一下

(dg2)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+----------+-----+-----+
| id | name | age | sex |
+-----+----------+-----+-----+
| | zhangsan | | |
+-----+----------+-----+-----+
row in set (0.01 sec) (dg2)root@127.0.0.1 [mytest]> update table_key set name='zhangsir' where age=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (dg2)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+----------+-----+-----+
| id | name | age | sex |
+-----+----------+-----+-----+
| | zhangsir | | |
+-----+----------+-----+-----+
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+-----------+-----+-----+
| id | name | age | sex |
+-----+-----------+-----+-----+
| | zhangzong | | |
+-----+-----------+-----+-----+
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.101
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg1.
Read_Master_Log_Pos:
Relay_Log_File: mysql3307-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg1.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: fbc1bdf1-829b-11e4-9bdf-000c29b24882
Master_Info_File: /data//master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]>

删除索引,再测试一下

(dg1)root@127.0.0.1 [mytest]> alter table table_key drop key age_index;
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: (dg1)root@127.0.0.1 [mytest]> insert into table_key (id,name,age,sex) values(,'user1',);
ERROR (21S01): Column count doesn't match value count at row 1
(dg1)root@127.0.0.1 [mytest]> insert into table_key (id,name,age,sex) values(,'user1',,);
Query OK, row affected (0.00 sec) (dg1)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+-------+-----+-----+
| id | name | age | sex |
+-----+-------+-----+-----+
| | user1 | | |
+-----+-------+-----+-----+
row in set (0.00 sec) (dg1)root@127.0.0.1 [mytest]> update table_key set name='user3' where age=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (dg1)root@127.0.0.1 [mytest]>

从库看一下

(dg2)root@127.0.0.1 [mytest]> show create table table_key;
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| table_key | CREATE TABLE `table_key` (
`id` int() NOT NULL,
`name` varchar() NOT NULL,
`age` tinyint() NOT NULL,
`sex` tinyint() NOT NULL COMMENT '0,man,1,woman'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+-------+-----+-----+
| id | name | age | sex |
+-----+-------+-----+-----+
| | user1 | | |
+-----+-------+-----+-----+
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]> update table_key set name='user2' where age=;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (dg2)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+-------+-----+-----+
| id | name | age | sex |
+-----+-------+-----+-----+
| | user2 | | |
+-----+-------+-----+-----+
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]> select * from table_key where age=;
+-----+-------+-----+-----+
| id | name | age | sex |
+-----+-------+-----+-----+
| | user3 | | |
+-----+-------+-----+-----+
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.80.101
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: dg1.
Read_Master_Log_Pos:
Relay_Log_File: mysql3307-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: dg1.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: fbc1bdf1-829b-11e4-9bdf-000c29b24882
Master_Info_File: /data//master.info
SQL_Delay:
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:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
row in set (0.00 sec) (dg2)root@127.0.0.1 [mytest]>

总结:

1、SQL线程应用中继日志,在binlog_format是row格式的时候,确实是居于主键更新(Innodb表,如果没有显示指定主键,如果没有显式定义主键,则InnoDB会选择第一个不包含有NULL值的唯一索引作为主键索引),所以这张图是binlog_format=ROW格式是基本正确的。

2、使用自增列(INT/BIGINT类型)做主键,这样数据分布基本是有序的与B+数叶子节点分裂顺序一致,性能相对比较好;

3、形象的证明了RBR模式下,在有主键和唯一键的情况下MySQL复制SQL线程在应用中继日志的时候,锁范围比语句模式要少

参考资料 :SBR RBR两种模式的优缺点

SBR和RBR两种模式各自的优缺点: SBR 的优点:
  • 历史悠久,技术成熟。
  • binlog文件较小。
  • binlog中包含了所有数据库更改信息,可以据此来审核数据库的安全等情况。
  • binlog可以用于实时的还原,而不仅仅用于复制。
  • 主从版本可以不一样,从服务器版本可以比主服务器版本高。
SBR 的缺点:
  • 不是所有的UPDATE语句都能被复制,尤其是包含不确定操作的时候。
  • 调用具有不确定因素的 UDF 时复制也可能出问题
  • 使用以下函数的语句也无法被复制: * LOAD_FILE() * UUID() * USER() * FOUND_ROWS() * SYSDATE() (除非启动时启用了 --sysdate-is-now 选项)
  • INSERT ... SELECT 会产生比 RBR 更多的行级锁
  • 复制需要进行全表扫描(WHERE 语句中没有使用到索引)的 UPDATE 时,需要比 RBR 请求更多的行级锁
  • 对于有 AUTO_INCREMENT 字段的 InnoDB表而言,INSERT 语句会阻塞其他 INSERT 语句
  • 对于一些复杂的语句,在从服务器上的耗资源情况会更严重,而 RBR 模式下,只会对那个发生变化的记录产生影响
  • 存储函数(不是存储过程)在被调用的同时也会执行一次 NOW() 函数,这个可以说是坏事也可能是好事
  • 确定了的 UDF 也需要在从服务器上执行
  • 数据表必须几乎和主服务器保持一致才行,否则可能会导致复制出错
  • 执行复杂语句如果出错的话,会消耗更多资源
RBR 的优点:
  • 任何情况都可以被复制,这对复制来说是最安全可靠的
  • 和其他大多数数据库系统的复制技术一样
  • 多数情况下,从服务器上的表如果有主键的话,复制就会快了很多
  • 复制以下几种语句时的行锁更少: * INSERT ... SELECT * 包含 AUTO_INCREMENT 字段的 INSERT * 没有附带条件或者并没有修改很多记录的 UPDATE 或 DELETE 语句
  • 执行 INSERT,UPDATE,DELETE 语句时锁更少
  • 从服务器上采用多线程来执行复制成为可能
RBR 的缺点:
  • binlog 大了很多
  • 复杂的回滚时 binlog 中会包含大量的数据
  • 主服务器上执行 UPDATE 语句时,所有发生变化的记录都会写到 binlog 中,而 SBR 只会写一次,这会导致频繁发生 binlog 的并发写问题
  • UDF 产生的大 BLOB 值会导致复制变慢
  • 无法从 binlog 中看到都复制了写什么语句
  • 当在非事务表上执行一段堆积的SQL语句时,最好采用 SBR 模式,否则很容易导致主从服务器的数据不一致情况发生
另外,针对系统库 mysql 里面的表发生变化时的处理规则如下:
  • 如果是采用 INSERT,UPDATE,DELETE 直接操作表的情况,则日志格式根据 binlog_format 的设定而记录
  • 如果是采用 GRANT,REVOKE,SET PASSWORD 等管理语句来做的话,那么无论如何都采用 SBR 模式记录
注:采用 RBR 模式后,能解决很多原先出现的主键重复问题。