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

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)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 模式后,能解决很多原先出现的主键重复问题。

MySQL复制应用中继日志解析的更多相关文章

  1. MySQL复制(二):二进制日志、二进制日志的结构和内容

    通常只有即将执行完毕的语句才会写入到二进制日志中.但是一些特殊情况:语句附加的信息或直接代替语句被写入. 二进制日志记录的内容 作用:记录数据库中表的更变,用于复制和PITP(即时恢复) 基于语句SB ...

  2. MySQL redo与undo日志解析

    前言: 前面文章讲述了 MySQL 系统中常见的几种日志,其实还有事务相关日志 redo log 和 undo log 没有介绍.相对于其他几种日志而言, redo log 和 undo log 是更 ...

  3. mysql 复制(主从复制)

    一.概述 让一台服务器的数据与其他服务器数据保持同步.一台主库的数据可以同步到多台备库上,而备库本身也可以配置成其他服务器的主库. 主要应用: 1) 数据分布 2) 负载均衡 3) 伪备份.在备份基础 ...

  4. MySQL复制线程状态转变

    一.主库线程状态(State)值 以下列表显示了主从复制中主服务器的Binlog Dump线程的State列中可能看到的最常见状态(SHOW PROCESSLIST).如果Binlog Dump线程在 ...

  5. 转载:阿里canal实现mysql binlog日志解析同步redis

    from: http://www.cnblogs.com/duanxz/p/5062833.html 背景 早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数 ...

  6. canal 基于Mysql数据库增量日志解析

    canal 基于Mysql数据库增量日志解析  1.前言  最近太多事情 工作的事情,以及终身大事等等 耽误更新,由于最近做项目需要同步监听 未来电视 mysql的变更了解到公司会用canal做增量监 ...

  7. Mysql日志解析

    修改Mysql配置 Mysql配置地址为: C:\Program Files (x86)\MySQL\MySQL Server 5.5 如果无法修改可以把my.ini拷贝出来,修改完后,再拷贝回去! ...

  8. mysql之 日志体系(错误日志、查询日志、二进制日志、事务日志、中继日志)

    一. mysql错误日志:错误日志记录的事件:a).服务器启动关闭过程中的信息b).服务器运行过程中的错误信息c).事件调试器运行一个事件时间生的信息d).在从服务器上启动从服务器进程时产生的信息lo ...

  9. mysql基础之日志管理(查询日志、慢查询日志、错误日志、二进制日志、中继日志、事务日志)

    日志文件记录了MySQL数据库的各种类型的活动,MySQL数据库中常见的日志文件有 查询日志,慢查询日志,错误日志,二进制日志,中继日志 ,事务日志. 修改配置或者想要使配置永久生效需将内容写入配置文 ...

随机推荐

  1. DTcms列表隔行换色;loop自带行号

    <%loop cdr2 bcategoryList%> <%if(cdr2__loop__id==1)%> <a class="no-bg" href ...

  2. asp.net 点击按钮,页面没有任何变化,后台代码不触发

    asp.net 点击按钮,页面没有任何变化,后台代码不触发 和可能是 asp.net button  缺少validationGroup 导致的,需要查看页面的validation并且让他们抛出错误信 ...

  3. 【Delphi】无标题移动窗体

    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Inte ...

  4. 使用SQLyog远程访问mysql数据库设置

    mysql数据库远程访问设置方法 1.修改localhost更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从&q ...

  5. what is the purpose of channel coding?(信道编码的作用?)

    信道.信道编码及其作用 1.信道(channel) 信道和通信电路并不等同,用来表示向某一个方向传送信息的媒体.因此一条通信线路往往包含一条发送信道和一条接收信道. 从通信的双方信息交互方式看有三个基 ...

  6. 帝国cms中 内容分页的SEO优化

    关于内容页如果存在分页的话,我们想区分第一页和后面数页,当前的通用做法是在标题上加入分页码,帝国cms中如何做到呢.我们可以修改在e/class/functions.php中的源码.找到找到GetHt ...

  7. C#写的SQL聚合函数

    SQL Server 字符串连接聚合函数. 注册程序集: 拷贝“SqlStrConcate.dll”至<sql安装根目录>/MSSQL.1/MSSQL/Binn目录下,执行下面的SQL: ...

  8. 让用户打开你app的位置功能

    +运动 http://www.ccidnet.com/2015/0819/10014152.shtml 让你的app不再是一个购物网站, 而是一种生活方式, 逛街,在实体店逛街积累里程,兑换积分  送 ...

  9. 蜗牛历险记(二) Web框架(上)

    接上篇所说,本篇主要内容是讲述如何使用Autofac来管理整个平台的生命周期(初级). 一.简述 插件式Web开发的同学应该还会记得PreApplicationStartMethod这个Assembl ...

  10. Hibernate从入门到精通(三)Hibernate配置文件

    在上次的博文Hibernate从入门到精通(二)Hibernate实例演示我们已经通过一个实例的演示对Hibernate的基本使用有了一个简单的认识,这里我们在此简单回顾一下Hibernate框架的使 ...