本文主要介绍 MySQL 主备延迟,延迟产生的原因和主备切换策略。
主备延迟
主备同步过程中主要有三个时间点:
[En]
There are three main time points in the process of master / slave synchronization:
所谓主备延迟,指的就是同一个事务下,T3-T1 的时间差值。
主备延迟的计算,可以在备库下,通过 show slave status
命令查看,返回的结果中 seconds_behind_master
代表的就是主备延迟,单位是秒。它会等于(备机执行 binlog 时间戳 – binlog 记录的主机执行时间戳)。
一般在网络正常的情况下,备机接收主机发送的 binlog 很快,即 T2-T1 很小,可以忽略不计。主备延迟的主要来自于 T3-T2,这个时间差值代表备机执行中继日志(relay log)的速度比主机产生 binlog 日志的速度慢多少。
此外,您可能想知道主备系统的不一致是否会影响主备延迟。
[En]
In addition, you may wonder whether the inconsistency between the active and standby systems will affect the active / standby latency.
答案是否定的。因为主备机连接后,备机会获取主机的系统时间,然后与本地系统时间进行比较,计算出差值,在以后的计算中会随身携带。
[En]
The answer is no. Because after the main and standby machines are connected, the standby machine will obtain the system time of the host, and then compare it with the local system time to calculate the difference, which will be taken with it in the later calculation.
产生原因
1. 备机的性能比主机差
目前这种部署较少,因为考虑到要进行主备倒换,一般采用对称部署,即主备使用相同的规格。
[En]
At present, this kind of deployment is less, because considering that the master / slave switch will be carried out, symmetrical deployment is generally adopted, that is, the master / slave uses the same specification.
2. 备机压力大
主机用来完成写操作,备机可以用来完成一些读操作,也可以用后台操作来做一些数据分析工作。备机的压力不小于主机的压力,导致备机的同步延迟。
[En]
The host is used to complete the write operation, while the standby machine may be used to complete some read operations, or the background operation to do some data analysis work. The pressure of the standby is no less than that of the host, which leads to the synchronization delay of the standby.
解决方案:
1、一主多从,多个从机分担查询压力
2、对于数据分析场景,可以将 binlog 同步到外部系统,像 hadoop。
3. 大事务
即便保证主备机压力基本一致,但大事务情况下,主备机都要耗费相近的执行时间,从而导致备机的同步延迟。所以,DBA 往往会叮嘱开发不要执行大事务。就算一定要执行,也要将大事务分割成多个小事务来执行。
4. 大表DDL
大表DDL会占用很多的 CPU 和 IO 资源,很容易造成主备延迟。所以,要比较安全的操作,建议采用 gh-ost 进行。
开发在线上执行 SQL 时一定要注意,不能执行大事务或者大表DDL这种耗时操作,尤其是比较重要的服务,否则会造成主备延迟,影响业务。
主备切换策略
1. 可靠性优先策略
切换步骤:
可靠性优先的缺点是主备机在一段时间内不可用,不可用的时长取决于步骤3,直到步骤5完成才会恢复。
[En]
The disadvantage of reliability priority is that the main and standby machines are unavailable for a period of time, and the length of unavailability depends on step 3, which is not restored until the completion of step 5.
2. 可用性优先策略
为了确保可用性优先级,首先执行步骤4和5,使不可用时间为0。但这可能会导致主机和备用计算机之间的数据不一致。
[En]
In order to ensure availability priority, steps 4 and 5 are performed first so that the unavailability time is 0. But this may cause data inconsistencies between the host and standby computers.
Original: https://www.cnblogs.com/flowers-bloom/p/mysql45-ha.html
Author: flowers-bloom
Title: MySQL45讲之保证高可用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508078/
转载文章受原作者版权保护。转载请注明原作者出处!