您好,登錄后才能下訂單哦!
小編給大家分享一下如何配置與監控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半同步復制”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。