【整理】MySQL 复制的工作方式

      写这篇文章的原因是,看到开源中国上的一篇投稿翻译文章,其中很多地方被翻译的错误百出。所以还是将原文保存在下面供参考吧。

  • master 上写 binlog 是相对轻量的动作(顺序写+会被缓存到操作系统缓存里,轻量与否和磁盘同步设置相关);
  • slave 在主从复制中跟随很紧的情况下,会直接从 master 所在操作系统缓存中读出 binlog 事件;若在几小时或几天后才进行同步则将导致 master 进行读盘操作,耗费相应的 IO 资源;
  • Master_Log_File/Read_Master_Log_Pos/Relay_Master_Log_File/Exec_Master_Log_Pos 的含义
  • 由于 slave 上 IO 线程所引起的复制延迟是很少见的,如果确实是,一个可以尝试解决的方法是,使用备机数据压缩协议;若确认是由于 slave 上 SQL 线程导致的复制延迟,则可以通过 vmstat 来确定此情况属于“CPU密集型操作”还是“IO密集型操作”;另外可以通过 mpstat 来对  CPU 进行监控。
  • 如果确认了当前复制延迟是由于 IO 密集导致的,那么意味着在大多数情况下,sql 线程会因为 slave 单线程读的实现而无法快速获取所需的事件。所以在这种情况下,确实是由于“读”操作而导致复制性能的受限,而不是“写”。

=================

How does MySQL Replication really work?

While we do have many blog posts on replication on our blog, such as on replication being single-threaded, on semi-synchronous replication or on estimating replication capacity, I don’t think we have one that covers the very basics of how MySQL replication really works on the high level. Or it’s been so long ago I can’t even find it. So, I decided to write one now.

Of course, there are many aspects of MySQL replication, but my main focus will be the logistics – how replication events are written on the master, how they are transferred to the replication slave and then how they are applied there. Note that this is NOT a HOWTO setup replication, but rather a howstuffworks type of thing.

Replication events 
I say replication events in this article because I want to avoid discussion about different replication formats. These are covered pretty well in the MySQL manual here. Put simply, the events can be one of two types:

Statement based – in which case these are write queries 
Row based – in this case these are changes to records, sort of row diffs if you will 
But other than that, I won’t be going back to differences in replication with different replication formats, mostly because there’s very little that’s different when it comes to transporting the data changes.

On the master 
So now let me start with what is happening on the master. For replication to work, first of all master needs to be writing replication events to a special log called binary log. This is usually very lightweight activity (assuming events are not synchronized to disk), because writes are buffered and because they are sequential. The binary log file stores data that replication slave will be reading later.

Whenever a replication slave connects to a master, master creates a new thread for the connection (similar to one that’s used for just about any other server client) and then it does whatever the client – replication slave in this case – asks. Most of that is going to be (a) feeding replication slave with events from the binary log and (b) notifying slave about newly written events to its binary log.

Slaves that are up to date will mostly be reading events that are still cached in OS cache on the master, so there is not going to be any physical disk reads on the master in order to feed binary log events to slave(s). However, when you connect a replication slave that is few hours or even days behind, it will initially start reading binary logs that were written hours or days ago – master may no longer have these cached, so disk reads will occur. If master does not have free IO resources, you may feel a bump at that point.

On the replica 
Now let’s see what is happening on the slave. When you start replication, two threads are started on the slave:

1. IO thread

This process called IO thread connects to a master, reads binary log events from the master as they come in and just copies them over to a local log file called relay log. That’s all.

Even though there’s only one thread reading binary log from the master and one writing relay log on the slave, very rarely copying of replication events is a slower element of the replication. There could be a network delay, causing a steady delay of few hundred milliseconds, but that’s about it.

If you want to see where IO thread currently is, check the following in “show slave status\G”:

Master_Log_File – last file copied from the master (most of the time it would be the same as last binary log written by a master) 
Read_Master_Log_Pos – binary log from master is copied over to the relay log on the slave up until this position. 
And then you can compare it to the output of “show master status\G” from the master.

2. SQL thread

The second process – SQL thread – reads events from a relay log stored locally on the replication slave (the file that was written by IO thread) and then applies them as fast as possible.

This thread is what people often blame for being single-threaded. Going back to “show slave status\G”, you can get the current status of SQL thread from the following variables:

Relay_Master_Log_File – binary log from master, that SQL thread is “working on” (in reality it is working on relay log, so it’s just a convenient way to display information) 
Exec_Master_Log_Pos – which position from master binary log is being executed by SQL thread. 
 

Replication lag 
Now I want to briefly touch the subject of replication lag in this context. When you are dealing with replication lag, first thing you want to know is which of the two replication threads is behind. Most of the time it will be the SQL thread, still it makes sense to double check. You can do that by comparing the replication status variables mentioned above to the master binary log status from the output of “show master status\G” from the master.

If it happens to be IO thread, which, as I mentioned many times already, is very rare, one thing you may want to try to get that fixed is enabling slave compressed protocol.

Otherwise, if you are sure it is SQL thread, then you want to understand what is the reason and that you can usually observe by vmstat. Monitor server activity over time and see if it is “r” or “b” column that is “scoring” most of the time. If it is “r”, replication is CPU-bound, otherwise – IO. If it is not conclusive, mpstat will give you better visibility by CPU thread.

Note this assumes that there is no other activity happening on the server. If there is some activity, then you may also want to look at diskstats or even do a query review for SQL thread to get a good picture.

If you find that replication is CPU bound, this maybe very helpful.

If it is IO bound, then fixing it may not be as easy (or rather, as cheap). Let me explain. If replication is IO bound, most of the time that means that SQL thread is unable to read fast enough because reads are single threaded. Yes, you got that right – it is readsthat are limiting replication performance, not writes. Let me explain this further.

Assume you have a RAID10 with a bunch of disks and write-back cache. Writes, even though they are serialized, will be fast because they are buffered in the controller cache and because internally RAID card can parallelize writes to disks. Hence replication slave with similar hardware can write just as fast as master can.

Now Reads. When your workset does not fit in memory, then the data that is about to get modified is going to have to be read from disk first and this is where it is limited by the single-threaded nature of the replication, because one thread will only ever read from one disk at a time.

That being said, one solution to fix IO-bound replication is to increase the amount of memory so working set fits in memory. Another – get IO device that can do much more IO operations per second even with a single thread – fastest traditional disks can do up to 250 iops, SSDs – in the order of 10,000 iops.

 

时间: 2024-09-14 17:15:46

【整理】MySQL 复制的工作方式的相关文章

《MySQL技术内幕:InnoDB存储引擎第2版》——2.5 Master Thread工作方式

2.5 Master Thread工作方式 在2.3节中我们知道了,InnoDB存储引擎的主要工作都是在一个单独的后台线程Master Thread中完成的,这一节将具体解释该线程的具体实现及该线程可能存在的问题.2.5.1 InnoDB 1.0.x版本之前的Master ThreadMaster Thread具有最高的线程优先级别.其内部由多个循环(loop)组成:主循环(loop).后台循环(backgroup loop).刷新循环(flush loop).暂停循环(suspend loop

【转载】MySQL复制的概述、安装、故障、技巧、工具

概述 首先主服务器把数据变化记录到主日志,然后从服务器通过I/O线程读取主服务器上的主日志,并且把它写入到从服务器的中继日志中,接着SQL线程读取中继日志,并且在从服务器上重放,从而实现MySQL复制.具体如下图所示: MySQL复制 整个过程反映到从服务器上,对应三套日志信息,可在从服务器上用如下命令查看: mysql> SHOW SLAVE STATUS;   Master_Log_File & Read_Master_Log_Pos:下一个传输的主日志信息. Relay_Master_

详细讲解MySQL复制

同MongoDB,Redis这样的NoSQL数据库的复制相比,MySQL复制显得相当复杂! 概述 首先主服务器把数据变化记录到主日志,然后从服务器通过I/O线程读取主服务器上的主日志,并且把它写入到从服务器的中继日志中,接着SQL线程读取中继日志,并且在从服务器上重放,从而实现MySQL复制.具体如下图所示: MySQL复制 整个过程反映到从服务器上,对应三套日志信息,可在从服务器上用如下命令查看: mysql> SHOW SLAVE STATUS; Master_Log_File & Re

MySQL复制的概述、安装、故障、技巧、工具(火丁分享)_Mysql

同MongoDB,Redis这样的NoSQL数据库的复制相比,MySQL复制显得相当复杂! 概述 首先主服务器把数据变化记录到主日志,然后从服务器通过I/O线程读取主服务器上的主日志,并且把它写入到从服务器的中继日志中,接着SQL线程读取中继日志,并且在从服务器上重放,从而实现MySQL复制.具体如下图所示: 整个过程反映到从服务器上,对应三套日志信息,可在从服务器上用如下命令查看: 复制代码 代码如下: mysql> SHOW SLAVE STATUS; Master_Log_File &

理解MySQL——复制(Replication)

原文地址: http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html 1.复制概述 1.1.复制解决的问题数据复制技术有以下一些特点:(1)    数据分布(2)    负载平衡(load balancing)(3)    备份(4)    高可用性(high availability)和容错 1.2.复制如何工作从高层来看,复制分成三步:(1)    master将改变记录到二进制日志(binary log)中(这些记录叫做二

mysql双向加密解密方式用法详解_Mysql

如果你使用的正是mysql数据库,那么你把密码或者其他敏感重要信息保存在应用程序里的机会就很大.保护这些数据免受黑客或者窥探者的获取是一个令人关注的重要问题,因为您既不能让未经授权的人员使用或者破坏应用程序,同时还要保证您的竞争优势.幸运的是,MySQL带有很多设计用来提供这 种类型安全的加密函数.本文概述了其中的一些函数,并说明了如何使用它们,以及它们能够提供的不同级别的安全. 双向加密 就让我们从最简单的加密开始:双向加密.在这里,一段数据通过一个密钥被加密,只能够由知道这个密钥的人来解密.

MySQL复制(1) 原理和架构

复制的架构 MySQL复制解决的基本问题是让一台服务器的数据与其它服务器保持同步,它具有如下的特征: 1)异步:这意味着,在同一时间点上备库的数据可能与主库不一致,并无法保证它们之间的延迟: 2)向后兼容:也就是说,备库的版本可以高于主库的版本,但不能低于主库的版本 下面是MySQL复制的架构图: 大致分为三个步骤: 1)主库所有的数据更改都记录到二进制日志 2)备库通过I/O线程把主库的二进制日志复制到备库的中继日志 3)备库通过SQL线程读取并应用中继日志 上面这种复制架构使得I/O线程和S

数据库-求助一个关于mysql复制的问题

问题描述 求助一个关于mysql复制的问题 各位大神好: 小弟是个新人,接手一个现有系统的维护,因想弄套测试环境想把正在使用的mysql数据库在另一台机器上复制一个,倒腾半天无法成功,求大神答惑. 背景:原服务器是windows2003系统,新服务器的系统相同. 1.首先在新服务上安装了一个mysql数据库,由于不了解原来的数据库配置所以瞎配置了一通, 也就是在运行MySQLInstanceConfig.exe的时候选择的内容可能跟原来的不同. 2.将目标服务器上的mysql通过net stop

科技正在改变人们的工作方式和生活习惯

科技正在改变人们的工作方式和生活习惯,颠覆着传统社会的一切. 十年前,或许没有人知道在一年后市场上会出现一个叫iphone的通信产品,更没有人知道在接下来的几年时间里,这个产品会颠覆了大部分人对 手机 .对互联网的认知,甚至这个产品还开创了一个基于移动端的商业生态,也就是大家今天见到的移动互联网生态. 五年前,或许也没有人知道在五年后的今天中国电商会如此发达,你甚至都不用出门,上网就可以购买生活所需的柴米油盐,而且数小时内就能送到你的手中.用不了几年这个过程还会更短,真正做到急需即得,这就是今天