手机
当前位置:查字典教程网 >编程开发 >mysql数据库 >逐步分析MySQL从库com_insert无变化的原因
逐步分析MySQL从库com_insert无变化的原因
摘要:大家都知道com_insert等com_xxx参数可以用来监控数据库实例的访问量,也就是我们常说的QPS。并且基于MySQL的复制原理,所有...

大家都知道com_insert等com_xxx参数可以用来监控数据库实例的访问量,也就是我们常说的QPS。并且基于MySQL的复制原理,所有主库执行的操作都会在从库重放一遍保证数据一致,那么主库的com_insert和从库的com_insert理论上应该是相等的。

如下面显示,第二列代表主库,第三列代表从库:

复制代码 代码如下:

com_select 22 1138

com_update 36 37

com_insert 133 135

com_delete 0 0

qcache_hits 0 0

Com_replace 0 0

Connections 13 24

但是我们看另外一个业务:

复制代码 代码如下:

com_select 0 95

com_update 0 0

com_insert 92 0

com_delete 20 0

qcache_hits 0 6

Com_replace 0 0

Connections 0 6

我们可以很明显的看出来,主库有92个写,但是从库0个写,这是为什么呢?

这2个业务唯一的区别就是binlog_format的设置不一样。

复制代码 代码如下:

第一个业务

show global variables like '%binlog_format%';

+---------------+-----------+

| Variable_name | Value |

+---------------+-----------+

| binlog_format | STATEMENT |

+---------------+-----------+

第二个业务

show global variables like '%binlog_format%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| binlog_format | ROW |

+---------------+-------+

我们来看下com_xxx的官方文档定义:

The Com_xxx statement counter variables indicate the number of times each xxx statement has been executed. There is one status variable for each type of statement. For example, Com_delete and Com_update count DELETE and UPDATE statements, respectively. Com_delete_multi and Com_update_multi are similar but apply to DELETE and UPDATE statements that use multiple-table syntax.

从上述文档,我们只能看到com_xxx是如何运作的,但是并不能解释为什么使用RBR之后com_insert就不变化了。

接下来我们结合下面这段文档来一起看看。

You cannot examine the logs to see what statements were executed, nor can you see on the slave what statements were received from the master and executed. However, you can see what data was changed using mysqlbinlog with the options --base64-output=DECODE-ROWS and --verbose.

这2段话结合来看,原因应该是这样的:

1、主库上接收的是statement的语句,所以com_insert符合触发条件,会随着业务增加。

2、而从库是拿到主库的binlog后重放更新数据,但是主库的日志格式是row format,这就导致了binlog中记录的不是statement语句,而是data的变化记录。

3、这样从库虽然依然能进行更新记录,但是无法解析出来这些data变化是一条statement语句导致的还是多条statment语句导致,所以就不在更新com_insert这个statment counter了。

基本上推论符合现实情况,但是没有code证明,比较遗憾。

另外,如果我们无法通过com_insert来监控从库的写入情况,那么我们应该监控那个status呢?

个人建议对于row格式的实例,通过监控innodb_rows_inserted来监控写入情况。

复制代码 代码如下:

show global status like 'innodb_rows_inserted';

+----------------------+------------+

| Variable_name | Value |

+----------------------+------------+

| Innodb_rows_inserted | 2666049650 |

+----------------------+------------+

附:(两个文档的官方文档链接)

http://dev.mysql.com/doc/refman/5.6/en/server-status-variables.html#statvar_Com_xxx

http://dev.mysql.com/doc/refman/5.5/en/replication-sbr-rbr.html

【逐步分析MySQL从库com_insert无变化的原因】相关文章:

在MySQL数据库中复位根用户的密码的方法

利用MySQL函数实现判断视频扩展名的代码

MySQL从MyISAM引擎转换到InnoDB引擎需要注意的地方

MySQL 在触发器里中断记录的插入或更新?

MySQL 性能优化的最佳20多条经验分享

MySQL Administrator 登录报错的解决方法

MySQL 获得当前日期时间的函数小结

如何选择合适的MySQL存储引擎

防止服务器宕机时MySQL数据丢失的几种方案

MySQL查询和修改auto_increment的方法

精品推荐
分类导航