MySQL半同步复制

1、概述


主从复制存在三种类型:异步复制、同步复制以及半同步复制,下面根据手册上解释逐一说明一下。

异步复制:

主库将更新的事件写入binlog,准备好的从库获取这些binlong进行回放。这无法保证所有从库都接到这些事件。

With asynchronous replication, the master writes events to its binary log and slaves request them when they are ready. There is no guarantee that any event will ever reach any slave.

同步复制:

主库提交一个事务,在返回结果给客户端前,需要等待所有从库都提交了这个事务。缺点就是主库在完成一个事务前存在很多延迟

在MySQL Cluster NDB中是同步的

http://dev.mysql.com/doc/refman/5.5/en/replication.html

With fully synchronous replication, when a master commits a transaction, all slaves also will have committed the transaction before the master returns to the session that performed the transaction. The drawback of this is that there might be a lot of delay to complete a transaction.

半同步复制:

异步复制和同步复制之间的折中。主库在commit的时候,只需要等待至少一个从库确认收到主库更新,并将其记录到relay-log中。

不需要等待所有从库都确认,只需要一个就可以。也不需要这些更新的事件在从库上完成执行,只需要记录到relay-log中(刷新到磁盘上)

Semisynchronous replication falls between asynchronous and fully synchronous replication. The master waits after commit only until at least one slave has received and logged the events. It does not wait for all slaves to acknowledge receipt, and it requires only receipt, not that the events have been fully executed and committed on the slave side.

 

2、异步复制原理


 

3、异步复制存在的问题


 

 

4、半同步复制原理


 

 

 

The slave acknowledges receipt of a transaction's events only after the events have been written to its relay log and flushed to disk. 

这里面说到写入relay-log且flush到disk后返回,flush to disk是写入缓存中,带有raid卡的缓存

5、半同步复制配置


5.1 插件式安装

安装前提:

1、只适合在MySQL5.5或者更高的版本中安装

2、MySQL的server必须有自动加载功能,通过参数have_dynamic_loading变量进行检查

3、主从的异步复制已经正常工作

关于plugin_nameshared_library_name对应关系可以参照

http://www.cnblogs.com/gsblog/p/3737675.html

5.2 主库

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';


mysql> show variables like '%rpl%';
+------------------------------------+----------+
| Variable_name                      | Value    |
+------------------------------------+----------+
| rpl_semi_sync_master_enabled       | ON       |
| rpl_semi_sync_master_timeout       | 10000    |
| rpl_semi_sync_master_trace_level   | 32       |
| rpl_semi_sync_master_wait_no_slave | OFF      |
| rpl_stop_slave_timeout             | 31536000 |
+------------------------------------+----------+

 修改主库配置文件:

[mysqld]
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=1000 # 1 second
sync_log = 1

5.3 从库

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

mysql> show slave status\G

修改从库的配置文件:

rpl_semi_sync_slave_enabled = 1

尽管主从之间的server-id不同,但是在开启同步出现server-uuid冲突问题,主要原因在于从库的mysql是直接从主库上拷贝过来的(包括二进制以及数据等所有文件),在data目录下存在auto.cnf文件:

[root@typhoeus81 ice_test_2 data]# pwd
/data1/guosong/mysql5616/data
[root@typhoeus81 ice_test_2 data]# more auto.cnf 
[auto]
server-uuid=b9d59578-df02-11e3-b112-001e4f1f39cf

 手动将该文件清理后,重启mysql会根据server-id重新产生一个文件

当然对于线上服务的话,主从都需要安装这两个插件,保证高可用。

5.4 相关配置以及参数

主要配置参数

*rpl_semi_sync_master_enabled
主库是否开启半同步配置

*rpl_semi_sync_master_timeout
主库等待从库的确认超时时间,默认是10s,超时之后改为异步模式

*rpl_semi_sync_slave_enabled
从库是否开启半同步配置

可以用于监控半同步状态的主要状态参数:

*Rpl_semi_sync_master_clients
设置半同步从库的个数

*Rpl_semi_sync_master_status
主库是否开启半同步的标志

*Rpl_semi_sync_master_no_tx
从库未成功确认的commits个数

*Rpl_semi_sync_master_yes_tx
从库成功确认的commits个数

*Rpl_semi_sync_slave_status
从库是否开启半同步的标志

 http://dev.mysql.com/doc/refman/5.5/en/replication-semisync-interface.html

 http://dev.mysql.com/doc/refman/5.5/en/replication-semisync-installation.html

6、半同步复制注意点


6.1 超时机制,半同步自动切换为异步

测试:

和参数rpl_semi_sync_master_timeout有关,默认值为10s太长,文档中建议修改为1s

6.2

 

 7、参考文献


 

http://blog.itpub.net/29254281/viewspace-1084938/

 

posted @ 2014-05-19 11:16  小郭学路  阅读(775)  评论(0编辑  收藏  举报