半同步机制:
MySQL复制环境中的同步,默认采用异步的方式。从MySQL5.5开始提供半同步复制机制(Semisynchronous Replication)。Master在返回操作成功(或失败)信息给发起请求的客户端前,还是要将事务发送给Slave节点,Master节点只需要确认至少一个Slave接收到了事务,即可向发起请求的客户端返回操作成功的信息,Master节点不需要等待Slave节点成功执行完这个事务。每个成功提交的事务,都代表这份数据至少存在两个节点上。
查看当前系统中安装的所有插件:
1、配置半同步复制环境
Master > show plugins;
MySQL软件有个插件目录,专门存放各种自带但没有安装,或者来自第三方的插件,为系统变量plugin_dir所指定的位置。
Master > show variables like 'plugin_dir';
+---------------+------------------------------+
| Variable_name | Value |
+---------------+------------------------------+
| plugin_dir | /usr/local/mysql/lib/plugin/ |
+---------------+------------------------------+
1 row in set (0.00 sec)
[mysql@mysql1 ~]$ ll /usr/local/mysql/lib/plugin/semi*
-rwxr-xr-x. 1 mysql mysql 413321 Jul 9 22:37 /usr/local/mysql/lib/plugin/semisync_master.so
-rwxr-xr-x. 1 mysql mysql 248087 Jul 9 22:37 /usr/local/mysql/lib/plugin/semisync_slave.so
加载指定的插件,在Master节点执行命令,加载semisynchronous插件
Master > install plugin rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.12 sec)
加载指定的插件,在Slave节点执行命令,加载semisynchronous插件
RelaySlave > install plugin rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.09 sec)
设置rpl_semi_sync_*参数的配置,以启用半同步复制功能。进行配置时必须是双向的。建议将参数写到初始化参数文件中。
Master节点执行:
Master > set global rpl_semi_sync_master_enabled=1;用于控制是否在Master节点启用半同步复制,默认为1即启用状态
Query OK, 0 rows affected (0.00 sec)
Master > set global rpl_semi_sync_master_timeout=3000;超出指定时间Slave节点仍无响应,那么当前复制环境临时被转换为异步复制。默认为10000即10秒,这里设置为3秒
Query OK, 0 rows affected (0.00 sec)
Slave节点执行:
RelaySlave > set global rpl_semi_sync_slave_enabled=1;用来控制Slave节点是否启用半同步复制机制
Query OK, 0 rows affected (0.00 sec)
配置好系统变量后,必须重启Slave节点的IO_THREAD线程
Slave节点执行:
RelaySlave > stop slave io_thread;
Query OK, 0 rows affected (0.01 sec)
RelaySlave > start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
2、 监控半同步复制环境
在Slave节点,与半同步复制相关的状态变量只有一项:
Rpl_semi_sync_slave_status:标识当前Slave是否 启用了半同步模式
RelaySlave > show status like 'rpl_semi_sync%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.00 sec)
在Master节点,与半同步复制相关的状态变量有以下几项:
Rpl_semi_sync_master_wait_for_slave_count = N :用户可以设置应答Slave的数量
Rpl_semi_sync_master_clients:当前处于半同步模式的Slave节点数量
Rpl_semi_sync_master_status:当前Master节点是否启用了半同步模式
Rpl_semi_sync_master_no_tx:未成功发送到Slave节点的事务数量
Rpl_semi_sync_master_yse_tx:成功发送到Slave节点的事务数量
Master > show status like 'rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
进行插入数据测试
Master > show status like 'rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 1776 |
| Rpl_semi_sync_master_net_wait_time | 1776 |
| Rpl_semi_sync_master_net_waits | 1 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 1 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)