MySQL 复制滞后与延时复制

1:MySQL 复制滞后解决
MySQL复制被普遍认为是十分有效的,主服务器进行更改后,从服务器可在几秒内做出相应的改动。但如果发生两者之间同步缓慢的问题, 那么主要有以下两个原因:
从结点磁盘问题: 复制操作对每个数据库都是由一个线程来完成,通常执行变更时的滞后是由磁盘延迟引起的。在这种情况下,您应该考虑使用SSD加速这个过程。
带宽低/网络延迟高: 如果两个服务器位于远程位置(高延迟的情况下)或服务器之间的存在带宽较低的问题,我们应使用下面的方法之一或者两者结合使用,以最大限度地减少服务器间通信量。
使用基于语句的复制:基于行的复制会为数据库中每一行的变更创建一个SQL 语句。基于语句的复制是应用程序发送的实际SQL语句的记录。通常基于语句的复制在记录大小方面更为有效。然而,你应该意识到,当你使用UPDATE … LIMIT1时,基于语句的复制可能并不十分有效
压缩通信量: MySQL支持使用 slave_compressed_protocol参数进行日志压缩复制。这种方法将减少高达80%的服务器之间的通信。然而,压缩是计算密集型的,所以你应该意识到这样会产生一些额外的CPU利用率(这通常不属于数据库中的问题)。这个参数应该在两个服务器上都启用:
动态的从MySQL命令行输入:SET GLOBALslave_compressed_protocol = 1;
在MySQL配置文件中进行配置:

compress master-slave communication

slave_compressed_protocol = 1
最起码,要理解你的复制行为为何滞后,然后了解如何使用正确的方法来解决滞后问题。是的,它就是这么容易,且十分有效。
来自:http://www.searchdatabase.com.cn/showcontent_87714.htm

2:MySQL 延时复制设置
Mysql (需5.6以上版本)延迟复制配置,通过设置Slave上的MASTER TO MASTER_DELAY参数实现:
CHANGE MASTER TO MASTER_DELAY = N;
N为多少秒,该语句设置从数据库延时N秒后,再与主数据库进行数据同步复制
具体操作:
登陆到Slave数据库服务器
mysql>stop slave;
mysql>CHANGE MASTER TO MASTER_DELAY = 600;
mysql>start slave;
mysql>show slave status \G;
查看SQL_Delay的值为600,表示设置成功。
注释:
SQL_Delay:一个非负整数,表示秒数,Slave滞后多少秒于master。
SQL_Remaining_Delay:当 Slave_SQL_Running_State 等待,直到MASTER_DELAY秒后,Master执行的事件,
此字段包含一个整数,表示有多少秒左右的延迟。在其他时候,这个字段是0。
来自:http://my.oschina.net/emolee/blog/197916

3:可以查看mysql的官网
MySQL 5.6 supports delayed replication such that a slave server deliberately lags behind the master by at least a specified amount of time. The default delay is 0 seconds. Use the MASTER_DELAY option for CHANGE MASTER TO to set the delay to N seconds:
CHANGE MASTER TO MASTER_DELAY = N;
An event received from the master is not executed until at least N seconds later than its execution on the master. The exceptions are that there is no delay for format description events or log file rotation events, which affect only the internal state of the SQL thread.
Delayed replication can be used for several purposes:
重点:To protect against user mistakes on the master. A DBA can roll back a delayed slave to the time just before the disaster.
To test how the system behaves when there is a lag. For example, in an application, a lag might be caused by a heavy load on the slave. However, it can be difficult to generate this load level. Delayed replication can simulate the lag without having to simulate the load. It can also be used to debug conditions related to a lagging slave.
To inspect what the database looked like long ago, without having to reload a backup. For example, if the delay is one week and the DBA needs to see what the database looked like before the last few days’ worth of development, the delayed slave can be inspected.
START SLAVE and STOP SLAVE take effect immediately and ignore any delay. RESET SLAVE resets the delay to 0.
SHOW SLAVE STATUS has three fields that provide information about the delay:
SQL_Delay: A nonnegative integer indicating the number of seconds that the slave must lag the master.
SQL_Remaining_Delay: When Slave_SQL_Running_State is Waiting until MASTER_DELAY seconds after master executed event, this field contains an integer indicating the number of seconds left of the delay. At other times, this field is NULL.
Slave_SQL_Running_State: A string indicating the state of the SQL thread (analogous to Slave_IO_State). The value is identical to the State value of the SQL thread as displayed by SHOW PROCESSLIST.
When the slave SQL thread is waiting for the delay to elapse before executing an event, SHOW PROCESSLIST displays its State value as Waiting until MASTER_DELAY seconds after master executed event.
The relay-log.info file now contains the delay value, so the file format has changed. See Section 17.2.2.2, “Slave Status Logs”. In particular, the first line of the file now indicates how many lines are in the file. If you downgrade a slave server to a version older than MySQL 5.6, the older server will not read the file correctly. To address this, modify the file in a text editor to delete the initial line containing the number of lines.
from:http://dev.mysql.com/doc/refman/5.6/en/replication-delayed.html

你可能感兴趣的:(mysql,主从复制设置延时,主从复制延时)