MySQL半同步复制(5.5之后引入)

时间:2022-05-07 07:54:21

半同步复制架构在主库提交一个事务后,commit完成即反馈客户端,无需等待推送binlog完成,如图:

MySQL半同步复制(5.5之后引入)

半同步复制在主库完成一个事务后,需等待事务信息写入binlog日志并且至少有一个从库写入relay log日志后才能commit完成反馈客户端。架构设计如图:

MySQL半同步复制(5.5之后引入)

半同步复制在msyql5.5使用插件实现,主库和从库使用不同的插件。

半同步复制配置:

1)查看mysql服务器是否支持动态增加插件:

mysql> select @@have_dynamic_loading;

+------------------------+

| @@have_dynamic_loading |

+------------------------+

| YES                    |

+------------------------+

1 row in set (0.00 sec)

2)确认支持动态增加插件后,检查mysql安装目录下是否存在插件,一般默认在$MYSQL_HOME/lib/plugin目录下存在主库插件semisync_master.so和从库插件semisync_slave.so:

[root@chavinking plugin]# pwd

/usr/local/mysql/lib/plugin

[root@chavinking plugin]# ll *master.so *slave.so

-rwxr-xr-x 1 mysql mysql 416897 Mar 26  2015 semisync_master.so

-rwxr-xr-x 1 mysql mysql 250126 Mar 26  2015 semisync_slave.so

在主库上安装插件semisync_master.so:

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

Query OK, 0 rows affected (0.11 sec)

mysql> select * from mysql.plugin;

+----------------------+--------------------+

| name                 | dl                 |

+----------------------+--------------------+

| rpl_semi_sync_master | semisync_master.so |

+----------------------+--------------------+

1 row in set (0.00 sec)

在从库上安装插件semisync_slave.so:

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

Query OK, 0 rows affected (0.09 sec)

mysql> select * from mysql.plugin;

+---------------------+-------------------+

| name                | dl                |

+---------------------+-------------------+

| rpl_semi_sync_slave | semisync_slave.so |

+---------------------+-------------------+

1 row in set (0.00 sec)

3)需要分别在主库和从库上配置参数打开半同步semi_sync,默认半同步是不打开的,主库上配置全局参数:

MSYQL>set global rpl_semi_sync_master_enabled=1;

MYSQL>set global rpl_semi_sync_master_timeout=30000;

从库上配置全局参数:

MYSQL>set global rpl-semi_sync_slave_enabled=1;

重启从库上的io线程(异步复制->半同步复制,全新配置不需要):

mysql> stop slave io_thread;

Query OK, 0 rows affected (0.01 sec)

mysql> start slave io_thread;

Query OK, 0 rows affected (0.00 sec)

至此,在mysql异步复制基础上配置mysql半同步复制配置完成

检查复制状态:

mysql> show status like '%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)

mysql> show status like '%semi_sync%'                                             --从库

-> ;

+----------------------------+-------+

| Variable_name              | Value |

+----------------------------+-------+

| Rpl_semi_sync_slave_status | ON    |

+----------------------------+-------+

1 row in set (0.02 sec)

mysql> insert into t1 values(8),(9);

Query OK, 2 rows affected (0.05 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> show status like '%semi_sync%';

+--------------------------------------------+-------+

| Variable_name                              | Value |

+--------------------------------------------+-------+

| Rpl_semi_sync_master_clients               | 1     |

| Rpl_semi_sync_master_net_avg_wait_time     | 20232 |

| Rpl_semi_sync_master_net_wait_time         | 20232 |

| 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.02 sec)