本文环境  

主库:CentOS6.5 x64 192.168.0.65 mysql-5.6.29    
备库:CentOS6.5 x64 192.168.0.66 mysql-5.6.29

接上文: mysql-5.6主从同步配置示例

半同步复制的概念:    

mysql5.5.x以上版本支持半同步复制,当Slave主机连接到Master时,能够查看其是否处于半同步复制的机制。当Master上开启半同步复制的功能时,至少应该有一个Slave开启其功能。此时,一个线程在Master上提交事务将受到阻塞,直到得知一个已开启半同步复制功能的Slave已收到此事务的所有事件,或等待超时。当一个事务的事件都已写入其relay-log中且已刷新到磁盘上,Slave才会告知已收到。如果等待超时,也就是Master没被告知已收到,此时Master会自动转换为异步复制的机制。当至少一个半同步的Slave赶上了,Master与其Slave自动转换为半同步复制的机制。半同步复制的功能要在Master,Slave都开启,半同步复制才会起作用;否则,只开启一边,它依然为异步复制。

一、半同步主服务器配置

1. mysql主服务器配置

(1) 在主服务器上加入半自动配置文件参数

# vi /etc/my.cnf

[mysqld]  

log-bin                = master-bin    
log-bin-index          = master-bin.index    
binlog_format          = mixed    
server-id              = 1    
expire-logs-days       = 7    
rpl_semi_sync_master_enabled = 1    
rpl_semi_sync_master_timeout = 1000 # 1 second

(2) 安装半同步配置插件

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';

(3) 配置半同步插件

mysql> show global variables like '%rpl%';  

+------------------------------------+-------+    
| Variable_name                      | Value |    
+------------------------------------+-------+    
| rpl_recovery_rank                  | 0     |    
| rpl_semi_sync_master_enabled       | OFF   |    
| rpl_semi_sync_master_timeout       | 10000 |    
| rpl_semi_sync_master_trace_level   | 32    |    
| rpl_semi_sync_master_wait_no_slave | ON    |    
+------------------------------------+-------+

(4) 启用半同步插件

mysql> set global rpl_semi_sync_master_enabled =1;

(5) 配置超时

mysql> set rpl_semi_sync_master_timeout 1000;

mysql> show global status like 'rpl_semi%';  

+--------------------------------------------+-------+    
| Variable_name                              | Value |    
+--------------------------------------------+-------+    
| Rpl_semi_sync_master_clients               | 0     |    
| 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)

其它一些性能及相关参数:

2. 重启主服务器,使配置文件生效

# service mysqld restart 

二、半同步从服务器配置

1. mysql从服务器配置

(1) 加入如下半同步配置。

# vi /etc/my.cnf

[mysqld]  

log-bin                = mysql-bin    
binlog_format          = mixed    
server-id              = 11    
relay-log              = slave-relay-bin    
relay-log-index        = slave-relay-bin.index    
replicate_wild_ignore_table = mysql.%    
rpl_semi_sync_slave_enabled = 1

(2) 安装半同步配置插件

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';  

Query OK, 0 rows affected (0.00 sec)

(3) 配置半同步插件

mysql> show global variables like '%rpl%';  

+---------------------------------+----------+    
| Variable_name                   | Value    |    
+---------------------------------+----------+    
| rpl_semi_sync_slave_enabled     | ON       |    
| rpl_semi_sync_slave_trace_level | 32       |    
| rpl_stop_slave_timeout          | 31536000 |    
+---------------------------------+----------+    
3 rows in set (0.00 sec)

(4) 启用半同步插件

mysql> set global rpl_semi_sync_master_enabled =1;  

Query OK, 0 rows affected (0.00 sec)

mysql> show global variables like '%rpl%';  

+---------------------------------+----------+    
| Variable_name                   | Value    |    
+---------------------------------+----------+    
| rpl_semi_sync_slave_enabled     | ON       |    
| rpl_semi_sync_slave_trace_level | 32       |    
| rpl_stop_slave_timeout          | 31536000 |    
+---------------------------------+----------+    
3 rows in set (0.00 sec)

(5) 重启同步进程

mysql> stop slave;  

Query OK, 0 rows affected (0.02 sec)

mysql> start slave;  

Query OK, 0 rows affected (0.02 sec)

mysql> show slave status \G;  

*************************** 1. row ***************************    
               Slave_IO_State: Waiting for master to send event    
                  Master_Host: 192.168.0.65    
                  Master_User: repl    
                  Master_Port: 3306    
                Connect_Retry: 60    
              Master_Log_File: master-bin.000005    
          Read_Master_Log_Pos: 120    
               Relay_Log_File: testdb-relay-bin.000012    
                Relay_Log_Pos: 284    
        Relay_Master_Log_File: master-bin.000005    
             Slave_IO_Running: Yes    
            Slave_SQL_Running: Yes    
              Replicate_Do_DB:    
          Replicate_Ignore_DB:    
           Replicate_Do_Table:    
       Replicate_Ignore_Table:    
      Replicate_Wild_Do_Table:    
  Replicate_Wild_Ignore_Table: mysql.%    
                   Last_Errno: 0    
                   Last_Error:    
                 Skip_Counter: 0    
          Exec_Master_Log_Pos: 120    
              Relay_Log_Space: 622    
              Until_Condition: None    
               Until_Log_File:    
                Until_Log_Pos: 0    
           Master_SSL_Allowed: No    
           Master_SSL_CA_File:    
           Master_SSL_CA_Path:    
              Master_SSL_Cert:    
            Master_SSL_Cipher:    
               Master_SSL_Key:    
        Seconds_Behind_Master: 0    
Master_SSL_Verify_Server_Cert: No    
                Last_IO_Errno: 0    
                Last_IO_Error:    
               Last_SQL_Errno: 0    
               Last_SQL_Error:    
  Replicate_Ignore_Server_Ids:    
             Master_Server_Id: 1    
                  Master_UUID: c8bb22a1-024e-11e6-a1e8-000c29225fa0    
             Master_Info_File: /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/data/master.info    
                    SQL_Delay: 0    
          SQL_Remaining_Delay: NULL    
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it    
           Master_Retry_Count: 86400    
                  Master_Bind:    
      Last_IO_Error_Timestamp:    
     Last_SQL_Error_Timestamp:    
               Master_SSL_Crl:    
           Master_SSL_Crlpath:    
           Retrieved_Gtid_Set:    
            Executed_Gtid_Set:    
                Auto_Position: 0    
1 row in set (0.00 sec)

2. 在主服务器上可以查看有1个从服务器连接过来

mysql> show global status like 'rpl_semi%';  

+--------------------------------------------+-------+    
| 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)

3. 重启从服务器,使配置文件生效

# service mysqld restart