原文地址:http://www.codeweblog.com/mysql-replication-principle/

1, the replication process 
Mysql replication (replication) is an asynchronous replication, from a Mysql instace (called Master) to another Mysql instance (call it Slave). Implement the copy operation completed mainly by three processes, two processes in the Slave (Sql process and the IO process), another process in the Master (IO process) on the

To implement replication, you must first open the Master end of the binary log (bin-log) function, or can not be achieved. Because the whole process is actually copied from the Master Slave terminal access to the log and then himself fully in the implementation of the order of log records in the various operations. 
Copy the basic process is as follows: 
(1) Slave IO above the process connection Master, and requests the specified log file from the specified location (or from the beginning of the log) after the log contents; 
(2) Master Slave receives the IO process from request, through the IO is responsible for copying the information, upon request, to read the development process of the log after the log information specified location and return to the Slave of the IO process. Return information in addition to the information contained in the log, but also including this information has been returned to the Master end of the bin-log file name and the bin-log position; 
(3) Slave of the IO process of receiving information, will receive the log contents in turn added to the Slave end of the relay-log files of the end, and read to the Master end of the bin-log file name and location of the records to the master- info file so next time can be clearly read the high-speed Master "I need a bin-log of where to begin later in the log, please send it to me"; 
(4) Slave to Sql process detected a new relay-log increase in the content, it will immediately relay-log parsing the contents of a real implementation of the Master side executable content of those times, and in its implementation.

In fact the old version of Mysql replication to achieve two processes in the Slave side is not done, but by a process to complete. But later discovered there is a big risk to do so and performance issues, notably the following: 
First, a process to copy the bin-log to log and parse the log and its implementation process in a serial process, subject to certain restrictions on the performance, asynchronous replication of the delay will be longer. 
In addition, Slave-side from the Master to get bin-log over the side, the need to then parse the log, then in its implementation. In this process, Master client may have a lot of changes and has claimed a large number of logs. Master-side at this stage if the storage can not fix the error occurred, then generated at this stage all the changes will never be recovered. If the pressure in the Slave side, when relatively large, this process may take longer. 
Therefore, the latter version of Mysql To address this risk and improve the performance of replication, replication of the Slave-side to the two processes to complete. The improved scheme proposed Yahoo! who is an engineer "Jeremy Zawodny". This will not only solve the performance problem, but also shorten the delay time of asynchronous, but also reduces the amount of possible loss of data. Of course, even if two threads into this deal now after the slave data is also still exists the possibility of delay and data loss, after all, the replication is asynchronous. As long as the data changes are not a thing, these problems will always exist. If you want to completely avoid these problems, the cluster can only be used to solve the mysql. However, the cluster is the memory mysql database solution, you need to load all data into memory, so that the memory requirements of very large, and for general applications is not much sex can be implemented

2, copy to achieve level 
Mysql replication can be based on a statement (Statement level), it can be based on a record (Row level), configuration parameters can be set in Mysql replication level of the different levels of replication settings affect the Master end of the bin-log records into different forms.
(1) Row Level: log data will be recorded as each line is a modified form, and then again in the slave side to modify the same data. 
Benefits: In the row level mode, bin-log records can be executed sql statement without the context of relevant information, it only need to record a record that is modified, what kind of a modified. Therefore, the contents of row level log record will be very clear details of each line of data modification is very easy to understand. And will not appear under certain circumstances a stored procedure or function, and the trigger and trigger a call to the problem can not be copied correctly.
Disadvantages: row level, all statements executed when the records to the log, it also will record each line to record the changes, this may produce a large number of log content, such as there is such an update statement: update product set owner_member_id = 'b' where owner_member_id = 'a', after the implementation of the log is not recorded in this update statement, the amount corresponding to the event (mysql in the form of events to record bin-log log), but this statement for each of the Updated a record of the changes, so a lot of records into the records to be updated many events. Nature, bin-log log volume will be enormous. Especially when the implementation of alter table statements like, when the log volume is staggering. Mysql alter table because the table structure changes like handling statement is a record in the table are required for each change, in fact, is to rebuild the entire table. Then the table will be recorded for each record to the log. 
(2) Statement Level: modifies the data in each record to the sql will be the bin-log in the master. slave sql process in the copy when the original master will resolve to the same end sql executed to run again. 
Advantages: statement level benefits under the first is to solve the shortcomings of the next row level, the data do not record changes in each row, reducing the amount of bin-log log, save IO, improve performance. Because he only recorded in the Master's statement on the implementation details and the implementation of the context when the information statement. 
Disadvantages: Since the implementation of his statement is recorded, so, in order to make these statements in the slave side can be correctly implemented, then he must also record the time each statement in the implementation of some information, that is, context information, to ensure that all statements When the slave side can be implemented and the implementation of the master side when the same results. The other is, You Yu Mysql is relatively fast development, many continue to add new features to make mysql be reproduced encountered no small challenge, when natural reproduction involves the more complex content, bug the easier it appears. In the statement level, the current number of cases have been found in the copy will cause problems with mysql, when the data was modified to use some particular function or functions will occur when, for example: sleep () function in some version can not be true copy, in the stored procedure used last_insert_id () function may cause the slave and the master id and so on are inconsistent. The row level is based on each line to record the change, so no similar problem. 
Seen from the official documents, has been before the only Mysql statement based replication mode, until the 5.1.5 version of Mysql started to support the row level replication. From 5.0, Mysql replication has been solved a lot of the old version can not appear in the correct copy of the issue. However, due to the emergence of a stored procedure, a copy of Mysql brings even greater challenges. Also, see the official documents that the 5.1.8 version from the start, Mysql provides a Statement Level and Row Level than a third beyond the copy mode: Mixed, is actually a combination of the first two modes. In Mixed mode, Mysql based on the specific implementation of each sql statement to distinguish between the log form of treatment records, that is, choose one between the Statement and Row. The new version of the Statment level as before, only the implementation of the statement recorded. The new version of Mysql Squadron row level model has also been optimized to do, not all changes will be to record the row level, as encountered when the table structure will change the mode to record the statement, if the sql statement to do is update or delete statements modify data, etc., then the line will still record all the changes.

3, copy common framework 
Mysql replication environment is a more than 90% with one or more Master Slave architecture model, mainly used for reading the application of pressure for a low-cost expansion of the database-side solutions. Master and slave as long as the pressure is not too great (especially the slave-side pressure), then asynchronous replication is generally very little delay. Especially since the slave side of the replication processed into two processes, but also reduces the slave side of the delay. The benefits that real-time requirements for the data is not particularly sensitive to the application, just by cheap pc server to expand the number of slave, will read the pressure distributed to multiple slave machines above, you can solve the database side Reading pressure bottleneck. This is in large part to resolve the current pressure on many small and medium site's database bottlenecks, and even some large sites are using similar programs to address the bottlenecks in the database. 
Master slave with a number of very simple implementation of the framework, a number of slave and implementation of a single slave, and there is not much difference. In the Master does not care how many client connected to the master side slave, slave as long as the process of certification by the connection to the information he requested binlog, he would connect up the io process in accordance with the requirements, read their own binlog information returned to the slave of the IO process. Configuration details for the slave, the official documents in the Mysql has been said above, very clearly, and even introduced a variety of methods to achieve the configuration of slave.

Mysql does not support the more than subordinate to the Master Slave instance of a structure. That is, a slave instance can only accept a master sync source, I heard a patch that can be improved features, but not practiced. Mysql AB does not realized the reason why such a function, mainly on account of conflict resolution issues.

Mysql can be built into a dual master mode, which means that the two Mysql instance each other's Master, also for the other side of the Slave. But most of this architecture is only one end of the service, to avoid conflicts. Even if both sides of the implementation of changes is in order, due to asynchronous replication mechanism to achieve the same result even if the changes made in the evening may also be covered as early as the changes made, like the following scenario: 
Time Mysql A Mysql B 
An update is recorded as 10 x table y 
2 updates are recorded as 20 x table y 
3 A log and access to the application, update the x table y record is 10 (does not meet expectations) 
Get updated 4 x table y B log record is 20 (in line with expectations) 
Thus, not only in the B library user data is not above the desired results, A and B also appeared on both sides of the data inconsistency. Except under certain conditions can write separately in A and B fixed at both ends to ensure no cross-write, to be able to avoid the above problems

mysql replication principle--转的更多相关文章

  1. MySQL Replication需要注意的问题

    MySQL Replication 大家都非常熟悉了,我也不会写怎么搭建以及复制的原理,网上相关文章非常多,大家可以自己去搜寻.我在这里就是想总结一下mysql主从复制需要注意的地方.有人说主从复制很 ...

  2. MySql Replication配置

    一.前言 Mysql Replication作为读写分离的廉价解决方案,支持一主多备的方式进行数据存储,采用二进制日志传送,目前存在着广泛应用,网上相关概念也比较多,不再重复介绍.引用一张官方提供的R ...

  3. MySQL Replication 优化和技巧、常见故障解决方法

    MySQL 主从同步错误(error)解决(转) sql_slave_skip_counter参数 附: 一些错误信息的处理,主从服务器上的命令,及状态信息. 在从服务器上使用show slave s ...

  4. MySQL Replication浅析

    MySQL Replication是MySQL非常出色的一个功能,该功能将一个MySQL实例中的数据复制到另一个MySQL实例中.整个过程是异步进行的,但由于其高效的性能设计,复制的延时非常小.MyS ...

  5. 浅谈MySQL Replication(复制)基本原理

    1.MySQL Replication复制进程MySQL的复制(replication)是一个异步的复制,从一个MySQL instace(称之为Master)复制到另一个MySQL instance ...

  6. 搭建mysql主从复制---Mysql Replication

    主从复制原理 Mysql的Replication是一个异步的复制过程,从一个Mysql Instance(master)复制到另一个Mysql Instance(slave).中间需要三个线程slav ...

  7. MySQL Replication主从复制

    MySQL Replication:NySQL复制,MySQL的复制默认为异步工作模式   mysql的复制功能是mysql内置的,装上它之后就具备了这个功能,而mysql复制是mysql实现大规模高 ...

  8. MySQL Replication, 主从和双主配置

    MySQL Replication, 主从和双主配置 MySQL的Replication是一种多个MySQL的数据库做主从同步的方案,特点是异步,广泛用在各种对MySQL有更高性能,更高可靠性要求的场 ...

  9. 浅析 MySQL Replication(本文转自网络,非本人所写)

    作者:卢飞 来源:DoDBA(mysqlcode) 0.导读 本文几乎涵盖了MySQL Replication(主从复制)的大部分知识点,包括Replication原理.binlog format.复 ...

随机推荐

  1. 利用nodejs实现登录并转码视频(原创)

    nodejs的出现让前端人员可以使用js打造后台,也许哪天就真的摆脱了对java或者php的依赖了. 今天跟大家分享一个利用nodejs接受前端post请求,并实现视频转码的这样一个例子.视频转码用到 ...

  2. 伪元素小tips

    1.伪元素的是dom看不见的,表现为行内元素.我这里说的伪元素是指::before ::after.其他的像:first-letter :visited 则属于伪类. 2.因为dom不可见,所以伪元素 ...

  3. java 泛型的几点备忘

    1.在java虚拟机中是没有泛型的,所有泛型类的参数都会被擦除,如下: public void test(List<String> list, int num){} public void ...

  4. node(md5)

    md5是一种信息-摘要算法,即针对一段明文给出一个hash值,在密码学中最经典的用法是验证数据的完整性,因为一旦原始数据发生改变那么生成的摘要也必将不同. 网络中md5可以用于用户密码的加密,即在数据 ...

  5. 从分布式一致性谈到CAP理论、BASE理论

    问题的提出 在计算机科学领域,分布式一致性是一个相当重要且被广泛探索与论证问题,首先来看三种业务场景. 1.火车站售票 假如说我们的终端用户是一位经常坐火车的旅行家,通常他是去车站的售票处购买车票,然 ...

  6. Java虚拟机8:虚拟机性能监控与故障处理工具

    前言 定位系统问题的时候,知识.经验是基础,数据是依据,工具是运用知识处理数据的手段.这里说的数据包括:运行日志.异常堆栈.GC日志.线程快照.堆转储快照等.经常使用适当的虚拟机监控和分析的工具可以加 ...

  7. PSP记录个人项目耗时

    PSP2.1 Personal Software Process Stage Time Planning 计划 90 ·Estimate ·估计这个任务需要多长时间 90 Development 开发 ...

  8. Wix 安装部署(一)同MSBuild 自动生成打包文件

    因为项目需要,最近在研究Wix打包部署,园子里也有一些关于wix的博客,方方面面,讲的点各不同.我自己也在测试过程中,写下过程,以供参考.最新版本WiX Toolset v3.7,如何安装的就不说了, ...

  9. [.net 面向对象编程基础] (19) LINQ基础

    [.net 面向对象编程基础] (19)  LINQ基础 上两节我们介绍了.net的数组.集合和泛型.我们说到,数组是从以前编程语言延伸过来的一种引用类型,采用事先定义长度分配存储区域的方式.而集合是 ...

  10. Java多线程系列--“JUC锁”09之 CountDownLatch原理和示例

    概要 前面对"独占锁"和"共享锁"有了个大致的了解:本章,我们对CountDownLatch进行学习.和ReadWriteLock.ReadLock一样,Cou ...