MySQL半同步复制原理与配置详解(2)

#半同步功能主要是下面两个插件
[root@master ~]# ls -l /application/mysql/lib/plugin/
......
-rwxr-xr-x 1 mysql mysql 173396 Sep 15  2017 semisync_master.so
-rwxr-xr-x 1 mysql mysql  94066 Sep 15  2017 semisync_slave.so

#分别在主从库加载上面两个插件
master:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
slave:
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';

#查看插件是否加载成功,有两种方法
1)mysql> show plugins;
| rpl_semi_sync_master    | ACTIVE  | REPLICATION        | semisync_master.so | GPL    |2)mysql> select plugin_name,plugin_status from information_schema.plugins where plugin_name like '%semi%';
+----------------------+---------------+| plugin_name          | plugin_status |
+----------------------+---------------+| rpl_semi_sync_master | ACTIVE        |
+----------------------+---------------+1 row in set (0.10 sec)

2、开启半同步复制

  在安装完插件后,半同步复制默认是关闭的,这时需设置参数来开启半同步。

mater:
mysql> set global rpl_semi_sync_master_enabled = 1;
slave:
mysql> set global rpl_semi_sync_slave_enabled = 1;
#以上的启动方式是在命令行操作,是临时生效的;永久生效需将如下设置写在配置文件中。
master:
plugin-load = rpl_semi_sync_master=semisync_master.so  #此项可以让plugin在任何时候都被mysql加载
rpl_semi_sync_master_enabled = 1
slave:
plugin-load = rpl_semi_sync_slave=semisync_slave.so
rpl_semi_sync_slave_enabled = 1
#在有的高可用架构下,master和slave需同时启动,以便在切换后能继续使用半同步复制
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1

3、重启slave上的IO线程使半同步生效

mysql> stop slave io_thread;

mysql> start slave io_thread;
#如果没有重启,则默认还是异步复制,重启后,slave会在master上注册为半同步复制的slave角色。

#查看半同步是否在运行
master:
mysql> show status like 'Rpl_semi_sync_master_status';
+-----------------------------+-------+
| Variable_name              | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON    |
+-----------------------------+-------+

slave:
mysql> show status like 'Rpl_semi_sync_slave_status';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+

4、半同步复制测试

master:
mysql> create database db;
mysql> use db
Database changed
mysql> create table t1(id int);
mysql> insert into t1 values(1);
mysql> select * from t1;
+------+| id  |+------+|    1 |+------+1 row in set (0.00 sec)
slave:
mysql> select * from db.t1;
+------+| id  |+------+|    1 |+------+1 row in set (0.00 sec)

#可以看到数据很快同步到了从库上,下面关闭io_thread测试
slave:
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.00 sec)
master:
mysql> insert into t1 values(2);  #此处有一个10s的超时等待时间,超时后转为异步插入
Query OK, 1 row affected (10.11 sec)
mysql> show status like 'Rpl_semi_sync_master_status';  #半同步已失效
+-----------------------------+-------+| Variable_name              | Value |
+-----------------------------+-------+| Rpl_semi_sync_master_status | OFF  |
+-----------------------------+-------+
slave:
mysql> show status like 'Rpl_semi_sync_slave_status';  #从库的半同步也失效
+----------------------------+-------+| Variable_name              | Value |
+----------------------------+-------+| Rpl_semi_sync_slave_status | OFF  |
+----------------------------+-------+1 row in set (0.01 sec)
slave:
mysql> start slave io_thread;  #从库开启io线程
Query OK, 0 rows affected (0.00 sec)
master:
mysql> show status like 'Rpl_semi_sync_master_status';  #又重新转为半同步复制
+-----------------------------+-------+| Variable_name              | Value |
+-----------------------------+-------+| Rpl_semi_sync_master_status | ON    |
+-----------------------------+-------+1 row in set (0.00 sec)
slave:
mysql> select * from db.t1;  #从库上数据已同步
+------+| id |+------+| 1 || 2 |+------+2 rows in set (0.00 sec)

5、其他说明

1)环境变量

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/309decbebc17312273c4f51dfd5b828e.html