您好,登錄后才能下訂單哦!
小編給大家分享一下如何配置與監(jiān)控MySQL 5.5半同步復制,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
在MySQL的復制環(huán)境中,數(shù)據(jù)同步默認是異步的,Master節(jié)點將事件寫入二進制日志,但是不知道Slave節(jié)點在什么時候獲取并應用這些事件;如果Master節(jié)點宕機,則已經提交的事務可能沒有被傳輸?shù)絊lave節(jié)點。
在MySQL 5.5及以上版本,支持半同步方式的復制。
這個功能是通過插件方式來實現(xiàn)的,它具有如下特點:
當Slave節(jié)點連接到Master節(jié)點時,它會指示它是否是半同步的狀態(tài);
如果Master節(jié)點啟用了半同步復制的特性,則至少要有一個半同步的Slave節(jié)點。當Master節(jié)點有一個線程提交了一個事務時,事務不會立刻提交,而是處于阻塞狀態(tài),它會等待至少一個Slave節(jié)點發(fā)出響應信息給Master節(jié)點,或者等到超時為止;
只有當Slave節(jié)點將接收到來自Master節(jié)點的事務事件寫入中繼日志并同步到磁盤上后,它才會向Master節(jié)點發(fā)送響應信息;
當沒有任何Slave節(jié)點響應并超時后,Master節(jié)點將變成異步傳輸狀態(tài)。當至少一個Slave節(jié)點處于半同步狀態(tài)后,Master節(jié)點會恢復到半同步復制的狀態(tài);
半同步復制必須在Master和Slave節(jié)點同時啟用。如果Master節(jié)點沒有啟用半同步復制,或者所有Slave節(jié)點沒有啟用半同步復制,則Master節(jié)點會使用異步復制。
--顯示插件所在目錄
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節(jié)點加載插件
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節(jié)點加載插件
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)
--重要的參數(shù)如下:
rpl_semi_sync_master_enabled 控制Master節(jié)點半同步復制是否開啟
rpl_semi_sync_slave_enabled 控制Slave節(jié)點半同步復制是否開啟
rpl_semi_sync_master_timeout 控制Master節(jié)點等待Slave節(jié)點的響應時間,單位是毫秒,默認值是10000毫秒,也就是10秒
--Master節(jié)點,查看半同步復制的相關參數(shù)
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節(jié)點,打開半同步復制特性,并設置超時時間為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)
如果想讓參數(shù)在數(shù)據(jù)庫重啟后生效,需要將參數(shù)分別加Master和Slave節(jié)點到配置文件中。
--Slave節(jié)點,打開半同步復制特性
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)
--配置好系統(tǒng)變量后,重啟Slave節(jié)點的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)
--半同步復制環(huán)境的監(jiān)控
--相關參數(shù)
rpl_semi_sync_master_clients 配置半同步復制的Slave節(jié)點數(shù)量,只有當Master節(jié)點的半同步復制插件安裝后,這個參數(shù)才會生效
Rpl_semi_sync_master_status 指示Master節(jié)點的半同步復制特性是否開啟。如果這個參數(shù)的值時ON,則半同步復制插件已經啟用且提交響應已經發(fā)生;如果這個參數(shù)的值是OFF,則說明半同步復制插件沒有啟用,或者
Master節(jié)點因為提交響應超時而回到異步復制模式
Rpl_semi_sync_slave_status 指示Slave節(jié)點的半同步復制特性是否開啟。如果這個參數(shù)的值為ON,則說明半同步復制插件已經開啟且Slave節(jié)點的I/O線程在運行。只有當Slave節(jié)點安裝半同步復制插件后,這個參數(shù)才
會生效。
Rpl_semi_sync_master_no_tx 沒有被Slave節(jié)點成功響應的提交事務數(shù)
Rpl_semi_sync_master_yes_tx 被Slave節(jié)點成功響應的提交事務數(shù)
Rpl_semi_sync_master_net_wait_time Master節(jié)點等待Slave節(jié)點回復的總時間,單位是微秒。只有當Master節(jié)點安裝了半同步復制插件后,這個參數(shù)才會生效
Rpl_semi_sync_master_net_avg_wait_time Master節(jié)點等待Slave節(jié)點回復的平均時間,單位是微秒
--在Master節(jié)點監(jiān)控半同步復制的狀態(tài)
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節(jié)點監(jiān)控半同步復制的狀態(tài)
mysql> show status like '%semi%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.00 sec)
看完了這篇文章,相信你對“如何配置與監(jiān)控MySQL 5.5半同步復制”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。