Mysql binlog主从延迟,relay_log更新不完。磁盘io高

本文探讨了MySQL中UPDATE语句执行不改变数据却记录在binlog的现象,分析了ROW、STATEMENT和MIXED模式的影响,以及如何通过调整binlog_format和innodb_flush_log_at_trx_commit参数来优化主从延迟。

现象①

根据binlog发现imdb.im_alarm update执行次数过多问题,但实际不更新数据,重复update sql。例如state是0仍然update state=0.也就是说执行了update,但没有更新数据,并且该sql记录在binlog中。

mysqlbinlog --base64-output=decode-rows --verbose mysql-log.001491 > temp.txt

测试MySQL中update修改数据与原数据相同会再次执行

测试环境

Centos8
Mysql8.0.23
服务器12与服务器13做主主复制

查看当前配置

M_USER=root
M_AUTH=pass
mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_row_image";' 2>/dev/null
mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_format";' 2>/dev/null
mysql -u $M_USER -p$M_AUTH -e 'show variables like "transaction_isolation";' 2>/dev/null

ROW模式下测试Update

binlog_formant配置

[root@manyun mysql]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_row_image";' 2>/dev/null
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| binlog_row_image | FULL  |
+------------------+-------+
[root@manyun mysql]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_format";' 2>/dev/null
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
[root@manyun mysql]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "transaction_isolation;"' 2>/dev/null
+-----------------------+-----------------+
| Variable_name         | Value           |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+

测试开始

# 先创建一个测试库
CREATE DATABASE  IF NOT EXISTS test;
use test;
# 创建这个测试表
CREATE TABLE `info` (
  `id` int NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `gender` varchar(255) DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


INSERT INTO `test`.`info`(`id`, `name`, `gender`, `age`) VALUES (1, 'wayne', '1', 27);

# 查看master postion
## 服务器12
*************************** 1. row ***************************
             File: mysql-log.000006
         Position: 3883
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

## 服务器13
*************************** 1. row ***************************
             File: mysql-log.000015
         Position: 1241
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

# 执行更新
mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0
# 查看master postion
## 服务器12
*************************** 1. row ***************************
             File: mysql-log.000006
         Position: 3883
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
## 服务器13
*************************** 1. row ***************************
             File: mysql-log.000015
         Position: 1241
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

Statement模式下测试update

binlog_formant配置

[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_row_image";' 2>/dev/null
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| binlog_row_image | FULL  |
+------------------+-------+
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_format";' 2>/dev/null
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "transaction_isolation";' 2>/dev/null
+-----------------------+-----------------+
| Variable_name         | Value           |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+

测试开始

# 查看master postion
## 服务器12
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show  master status\G;' 2>/dev/null
*************************** 1. row ***************************
             File: mysql-log.000007
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

## 服务器13
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show  master status\G;' 2>/dev/null
*************************** 1. row ***************************
             File: mysql-log.000016
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

# 执行更新
mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

# 查看master postion
## 服务器12
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000007
         Position: 537
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
## 服务器13
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show  master status\G;' 2>/dev/null
*************************** 1. row ***************************
             File: mysql-log.000016
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

# 以防万一我又做了两遍update测试,发现postion还是变化了
mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000007
         Position: 918
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000007
         Position: 1299
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

MIXED模式下测试update

binlog_formant配置

[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_row_image";' 2>/dev/null
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| binlog_row_image | FULL  |
+------------------+-------+
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "binlog_format";' 2>/dev/null
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show variables like "transaction_isolation";' 2>/dev/null
+-----------------------+-----------------+
| Variable_name         | Value           |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+

测试开始

# 查看master postion
## 服务器12
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show  master status\G;' 2>/dev/null
*************************** 1. row ***************************
             File: mysql-log.000008
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

## 服务器13
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show  master status\G;' 2>/dev/null
*************************** 1. row ***************************
             File: mysql-log.000017
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

# 执行更新
mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

# 查看master postion
## 服务器12
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000008
         Position: 529
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

## 服务器13
[root@manyun /]# mysql -u $M_USER -p$M_AUTH -e 'show  master status\G;' 2>/dev/null
*************************** 1. row ***************************
             File: mysql-log.000017
         Position: 156
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

# 以防万一我又做了三遍update测试,发现postion还是变化了
mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000008
         Position: 902
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000008
         Position: 1275
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

mysql> UPDATE `test`.`info` SET `name` = 'wayne', `gender` = '1', `age` = 27 WHERE `id` = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-log.000008
         Position: 1648
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

解决问题

binlog_format说明

mysql复制主要有三种方式:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复制(mixed-based replication, MBR)。对应的,binlog的格式也有三种:STATEMENT,ROW,MIXED。

① STATEMENT模式(SBR)

每一条会修改数据的sql语句会记录到binlog中。优点是并不需要记录每一条sql语句和每一行的数据变化,减少了binlog日志量,节约IO,提高性能。缺点是在某些情况下会导致master-slave中的数据不一致(如sleep()函数, last_insert_id(),以及user-defined functions(udf)等会出现问题)

②ROW模式(RBR)

不记录每条sql语句的上下文信息,仅需记录哪条数据被修改了,修改成什么样了。而且不会出现某些特定情况下的存储过程、或function、或trigger的调用和触发无法被正确复制的问题。缺点是会产生大量的日志,尤其是alter table的时候会让日志暴涨。

③ MIXED模式(MBR)

以上两种模式的混合使用,一般的复制使用STATEMENT模式保存binlog,对于STATEMENT模式无法复制的操作使用ROW模式保存binlog,MySQL会根据执行的SQL语句选择日志保存方式。

业务逻辑

根据业务逻辑,不需要将update不变动的sql保留。根据binlog_format,statement是适合我们的。修改后。重复update不在记录。

add.binlog_format=row

现象②

即使修改了binlog_format后,主从仍然延迟。

解决问题

优化bin_log flush到磁盘的过程。

## 默认 0:log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行。该模式下在事务提交的时候,不会主动触发写入磁盘的操作
## 2:每次事务提交时MySQL都会把log buffer的数据写入log file,但是flush(刷到磁盘)操作并不会同时进行。该模式下,MySQL会每秒执行一次 flush(刷到磁盘)操作
innodb_flush_log_at_trx_commit=2
## 当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。
sync_binlog=1000

优化参数解析

Mysql8.0优化参数项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大锅霍皮久

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值