如何配置与监控MySQL 5.5半同步复制
小编给大家分享一下如何配置与监控MySQL 5.5半同步复制,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
在MySQL的复制环境中,数据同步默认是异步的,Master节点将事件写入二进制日志,但是不知道Slave节点在什么时候获取并应用这些事件;如果Master节点宕机,则已经提交的事务可能没有被传输到Slave节点。
在MySQL 5.5及以上版本,支持半同步方式的复制。
这个功能是通过插件方式来实现的,它具有如下特点:
当Slave节点连接到Master节点时,它会指示它是否是半同步的状态;
如果Master节点启用了半同步复制的特性,则至少要有一个半同步的Slave节点。当Master节点有一个线程提交了一个事务时,事务不会立刻提交,而是处于阻塞状态,它会等待至少一个Slave节点发出响应信息给Master节点,或者等到超时为止;
只有当Slave节点将接收到来自Master节点的事务事件写入中继日志并同步到磁盘上后,它才会向Master节点发送响应信息;
当没有任何Slave节点响应并超时后,Master节点将变成异步传输状态。当至少一个Slave节点处于半同步状态后,Master节点会恢复到半同步复制的状态;
半同步复制必须在Master和Slave节点同时启用。如果Master节点没有启用半同步复制,或者所有Slave节点没有启用半同步复制,则Master节点会使用异步复制。
--显示插件所在目录
mysql> show variables like 'plugin_dir';
+---------------+-----------------------+
| Variable_name | Value |
+---------------+-----------------------+
| plugin_dir | /software/lib/plugin/ |
+---------------+-----------------------+
1 row in set (0.00 sec)
--查看插件目录里面的内容
[root@localhost ~]# cd /software/lib/plugin/
You have new mail in /var/spool/mail/root
[root@localhost plugin]# ls
adt_null.so auth_socket.so daemon_example.ini libdaemon_example.so qa_auth_client.so qa_auth_server.so semisync_slave.so
auth.so auth_test_plugin.so debug mypluglib.so qa_auth_interface.so semisync_master.so
--查看半同步插件,分别为主从插件
[root@localhost plugin]# ll /software/lib/plugin/semisync_*
-rwxr-xr-x. 1 root mysql 175571 Apr 11 06:46 /software/lib/plugin/semisync_master.so
-rwxr-xr-x. 1 root mysql 93818 Apr 11 06:46 /software/lib/plugin/semisync_slave.so
--在Master节点加载插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.24 sec)
mysql> select plugin_name,plugin_type,plugin_status from plugins where plugin_name='rpl_semi_sync_master';
+----------------------+-------------+---------------+
| plugin_name | plugin_type | plugin_status |
+----------------------+-------------+---------------+
| rpl_semi_sync_master | REPLICATION | ACTIVE |
+----------------------+-------------+---------------+
1 row in set (0.14 sec)
--在Slave节点加载插件
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.57 sec)
mysql> show plugins;
+--------------------------+--------+--------------------+-------------------+---------+
| Name | Status | Type | Library | License |
+--------------------------+--------+--------------------+-------------------+---------+
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| mysql_old_password | ACTIVE | AUTHENTICATION | NULL | GPL |
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
| MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL |
| FEDERATED | ACTIVE | STORAGE ENGINE | NULL | GPL |
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
| InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL |
| INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
| rpl_semi_sync_slave | ACTIVE | REPLICATION | semisync_slave.so | GPL |
+--------------------------+--------+--------------------+-------------------+---------+
24 rows in set (0.03 sec)
--重要的参数如下:
rpl_semi_sync_master_enabled 控制Master节点半同步复制是否开启
rpl_semi_sync_slave_enabled 控制Slave节点半同步复制是否开启
rpl_semi_sync_master_timeout 控制Master节点等待Slave节点的响应时间,单位是毫秒,默认值是10000毫秒,也就是10秒
--Master节点,查看半同步复制的相关参数
mysql> show variables like 'rpl_semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| 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 rows in set (0.01 sec)
--Master节点,打开半同步复制特性,并设置超时时间为3秒
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set global rpl_semi_sync_master_timeout=3000;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'rpl_semi%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 3000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
4 rows in set (0.00 sec)
如果想让参数在数据库重启后生效,需要将参数分别加Master和Slave节点到配置文件中。
--Slave节点,打开半同步复制特性
mysql> show variables like 'rpl_semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.02 sec)
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'rpl_semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
--配置好系统变量后,重启Slave节点的IO_THREAD线程
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)
--半同步复制环境的监控
--相关参数
rpl_semi_sync_master_clients 配置半同步复制的Slave节点数量,只有当Master节点的半同步复制插件安装后,这个参数才会生效
Rpl_semi_sync_master_status 指示Master节点的半同步复制特性是否开启。如果这个参数的值时ON,则半同步复制插件已经启用且提交响应已经发生;如果这个参数的值是OFF,则说明半同步复制插件没有启用,或者
Master节点因为提交响应超时而回到异步复制模式
Rpl_semi_sync_slave_status 指示Slave节点的半同步复制特性是否开启。如果这个参数的值为ON,则说明半同步复制插件已经开启且Slave节点的I/O线程在运行。只有当Slave节点安装半同步复制插件后,这个参数才
会生效。
Rpl_semi_sync_master_no_tx 没有被Slave节点成功响应的提交事务数
Rpl_semi_sync_master_yes_tx 被Slave节点成功响应的提交事务数
Rpl_semi_sync_master_net_wait_time Master节点等待Slave节点回复的总时间,单位是微秒。只有当Master节点安装了半同步复制插件后,这个参数才会生效
Rpl_semi_sync_master_net_avg_wait_time Master节点等待Slave节点回复的平均时间,单位是微秒
--在Master节点监控半同步复制的状态
mysql> show status like '%semi_sync%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 752 |
| Rpl_semi_sync_master_net_wait_time | 1505 |
| Rpl_semi_sync_master_net_waits | 2 |
| 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 | 2 |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)
--在Slave节点监控半同步复制的状态
mysql> show status like '%semi%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.00 sec)
看完了这篇文章,相信你对"如何配置与监控MySQL 5.5半同步复制"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!