Mysql Replication Concise Guide

Ye Hao study, analyze problems, all want to have a system of thinking.

When others still remain in only a cognitive Mysql instance, you know, actually produce Mysql environment, at least they should be long like this:

7143349-83fb008ee2aeb53c.png
from https://dev.mysql.com/doc/refman/5.7/en/replication-solutions-switch.html

Why master-slave

Almost every system on the production environment, can not be stand-alone, usually using the master-slave architecture.

And why should a master-slave, just remember these few keywords: SPOF , separate read and write , Backup , Scale-Out .

Summarize, in fact, on two points.

1,
to prevent SPOF : SPOF, SINGLE Point of failure, if you have only one Mysql instance, then it's dead, it means terminate your database service, which means you Ben collapse of the entire system. But if you hang a Mysql, Mysql there are hundreds of thousands to the top, then some of the little cow B.

2,
separate read and write : In general, our business are reading more than writing, this time you can write to the Master, to read to other Slave.

For example, you want to back up (backup) data , if executed mysqldump directly on the Master, obviously, will affect the write line, this time you can find a slave, quietly executed;

For example, you find more and more of a read request, this time only need a few Cadogan Slave, to share the next read request, this is the Scale-Out, scalability ;

As another example, you want to analyze data, statistics about recent days various types of orders, this time, quietly operating on a Slave, or from one Slave, export data to hadoop, go to the hadoop analysis, can be.

Two actions

Each support master-slave architecture of the system, we must consider how to achieve two actions: master-slave replication and from the main switch .

Usually the respective data from the Slave to the Master copy; and in the Master down, it will have to switch over the primary, which will switch to a Master Slave.

Here to talk about how to achieve these two Mysql action.

Master-slave replication

After the Master to write data, the data needs to synchronize to Slave, which is copied from the master.

So Master these data, what kind of protocols, formats sent to Slave? Since the different functions of different systems, their data structure for storing data is different, so that, between them replication vary.

For example redis, hair is rdb or aof files, Mysql, the hair is bin log.

bin log three formats, statement / row / mixed, specifically with reference to the Replication Formats .

So how bin log is sent to Slave from the Master, and how it is executed Slave?

Mysql spent three threads , to achieve this process, one in the Master, two in Slave:

  • Dump the Thread binlog : responsible bin log sent to slave
  • The I Slave / O the Thread : responsible for receiving master sent me bin log, and put it in a temporary storage called relay log log file
  • SQL the Thread Slave : read relay log, execute the contents inside

Mysql as to why such a design, in fact, look closely, you will find this whole process, process is a bit like messaging middleware processes the message, so the question becomes before I wrote another article: Why use messaging middleware

You see, knowledge is interconnected .

Master-slave delay

In speaking before switching from the main, we talked to from the main delay.

There are master-slave system, there will be a delay, that is written in the Master, Slave synchronization immediately change things happen, think about the know, it does not exist.

From then causes the main reason for the delay, what does?

May be the Slave machine performance is poor , the implementation of the Master 1s statement, to be executed in Slave 5s, usually will do it in the cash-strapped company, engaged in a very general machine, do the Slave;

Let's not bad money, then let Master and Slave machines as good.

But this is still a great master-slave delay, why?

Oh, that we put too much analysis business put a slave , various business end, when analyzing the data, in order not to affect the Master, have chosen to take the Slave, Slave too much pressure, naturally delayed.

So we have a master multi-slave, Slave you're not too busy to do, then I'll find a few more helpers.

This time delay should be small, right?

And no, we found that the system occasionally there will still be a great delay, check for a long time, found to be a time-consuming affairs Slave 10s in the implementation, such as when executing the commit, Slave has been delayed 10s.

This time try not know why write large transactions it.

From the switching master

Now in terms of "master-slave switch" a.

It is precisely because of the above "master-slave delay", only when the Master is down, we seek priority tangle between consistency and availability.

In fact, that is, in theory choose CAP often encountered in the case of P (partition tolerance) must be satisfied, in the end is to select C (strong consistency) or A (availability).

7143349-66f2b7086e38cf1a.png
from https://dev.mysql.com/doc/refman/5.7/en/replication-solutions-switch.html

如果要保证强一致性,那么切换时就有这几个步骤:

  • 把原来的Master,设置为read-only = true,这个时刻记为time1;
  • 等待Slave执行完Master发给它的bin log,直到Slave和Master一致,这个时刻记为time2;
  • 现在一致了,可以把slave设置为read-only = false,即可写入

在这个过程中,从time1到time2的时间,系统是无法被写入的,即不可用。

这个不可用的时间,取决于切换的时候,slave和master的延迟时间,延迟的越长,同步完成需要的时间就越长,不可用的时间也就越长。

保证强一致性,就会牺牲可用性,如果你不想系统有不可用的时间呢?那就得牺牲强一致性,因为你必须在Slave和Master还没完全同步时,就把Slave切换为Master。

主从数据不一致,会有什么问题呢?思考题。

总结

这篇文章只是在讲Mysql的主从吗?

我们总会说,技术那么多,还日新月异的,好像每天都有新的很厉害的技术出来,学不动。

但其实,如果学一项技术,就只是在学一项技术,而不去举一反三,发现知识背后通用的规则,那你之前学的知识,就只是在占用你大脑的内存,对你之后学的东西,没任何帮助。

但是如果你可以把所学的知识,串起来,形成一个体系,用系统的角度去看他们,那就不一样了。

比如今天我学了Mysql主从,那么当我们下次在学习其他系统的主从架构时,比如说redis,就可以照着今天这个思路去学习:

  • redis的主从架构长什么样子?
  • redis主从之间如何进行复制,数据格式是怎么样的?
  • redis主从延迟的原因有哪些?
  • master宕机时,redis如何进行主从切换?
  • ...

知识都是通的。

留个问题

现在我们学会用「主从」的角度来看待mysql,而不再是一个独立的机器。

有什么用呢?留个问题好了。

We often use foreign key in the business, such as there are two tables, one is the "plan table plan," the other is "scheme applicable to employees table plan_staff" plan_staff there is a field, plan_id, pointing to the plan table the primary key to do so, there is a problem?

reference

Reproduced in: https: //www.jianshu.com/p/8cda3202c80d

Guess you like

Origin blog.csdn.net/weixin_33910460/article/details/91247034