所謂半同步機(jī)制,master節(jié)點(diǎn)只要確認(rèn)有至少一個(gè)slave節(jié)點(diǎn)接收到了事物,即可向客戶端返回操作成功的信息,master節(jié)點(diǎn)甚至不需要等待slave節(jié)點(diǎn)也成功執(zhí)行完這個(gè)事物,只要至少有一個(gè)slave節(jié)點(diǎn)接收到這個(gè)事物,并且將之成功寫入到本地的中繼日志文件,就算成功。
一,MySQL的半同步復(fù)制是以插件形式提供,查看目前是否安裝
-
(root@localhost) [(none)]> show plugins;
-
+----------------------------+----------+--------------------+---------+---------+
-
| Name | Status | Type | Library | License |
-
+----------------------------+----------+--------------------+---------+---------+
-
| binlog | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL |
-
| sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL |
-
| MEMORY | 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_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_CMP_PER_INDEX_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 |
-
| INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_DATAFILES | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| INNODB_SYS_VIRTUAL | ACTIVE | INFORMATION SCHEMA | NULL | GPL |
-
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| CSV | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL |
-
| BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL |
-
| ngram | ACTIVE | FTPARSER | NULL | GPL |
-
+----------------------------+----------+--------------------+---------+---------+
-
44 rows in set (0.00 sec)
二,本例目前還沒有安裝,查看plugin目錄位置
-
(root@localhost) [(none)]> show variables like 'plugin_dir';
-
+---------------+------------------------------+
-
| Variable_name | Value |
-
+---------------+------------------------------+
-
| plugin_dir | /usr/local/mysql/lib/plugin/ |
-
+---------------+------------------------------+
-
1 row in set (0.00 sec)
三,在plugin目錄下有兩個(gè)與半同步相關(guān)的插件
-
[root@oracle11gtest plugin]# ls -al semisync*
-
-rwxr-xr-x 1 root root 688816 Feb 6 19:44 semisync_master.so
-
-rwxr-xr-x 1 root root 150555 Feb 6 19:44 semisync_slave.so
四,在master端安裝semisync_master.so,在slave端安裝semisync_slave.so
-
(root@localhost) [(none)]> install plugin rpl_semi_sync_master SONAME 'semisync_master.so' ;
-
Query OK, 0 rows affected (0.04 sec)
-
-
(root@localhost) [(none)]> install plugin rpl_semi_sync_slave SONAME 'semisync_slave.so';
-
Query OK, 0 rows affected (0.01 sec)
五,安裝好之后分別用show plugins命令check下六,修改主端半同步相關(guān)參數(shù)
-
(root@localhost) [(none)]> show variables like '%rpl_semi_sync%';
-
+-------------------------------------------+------------+
-
| 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_for_slave_count | 1 |
-
| rpl_semi_sync_master_wait_no_slave | ON |
-
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
-
+-------------------------------------------+------------+
-
6 rows in set (0.00 sec)
-
-
(root@localhost) [(none)]> set global rpl_semi_sync_master_enabled=1; ---用于控制是否在主端啟用半同步復(fù)制,
-
Query OK, 0 rows affected (0.00 sec)
-
-
(root@localhost) [(none)]> set global rpl_semi_sync_master_timeout=3000; ---用于指導(dǎo)主端等待slave響應(yīng)的時(shí)間,單位是毫秒,我這里設(shè)為3秒
-
Query OK, 0 rows affected (0.00 sec)
七,修改slave端半同步相關(guān)參數(shù)
-
(root@localhost) [(none)]> show variables like '%rpl_semi_sync%';
-
+---------------------------------+-------+
-
| Variable_name | Value |
-
+---------------------------------+-------+
-
| rpl_semi_sync_slave_enabled | OFF |
-
| rpl_semi_sync_slave_trace_level | 32 |
-
+---------------------------------+-------+
-
2 rows in set (0.01 sec)
-
-
(root@localhost) [(none)]> set global rpl_semi_sync_slave_enabled=1; ---用于控制是否在slave端啟用半同步復(fù)制,
-
Query OK, 0 rows affected (0.00 sec)
八,重啟slave節(jié)點(diǎn)的io_thread線程,目的是讓slave重新連接master,注冊(cè)成為半同步的slave身份
-
(root@localhost) [(none)]> stop slave io_thread;
-
Query OK, 0 rows affected (0.01 sec)
-
-
(root@localhost) [(none)]> start slave io_thread;
-
Query OK, 0 rows affected (0.00 sec)
九,測(cè)試
在master端插入數(shù)據(jù)
insert into oms3.gl values('zg');
-
insert into oms3.gl values('gl');
查看slave是否應(yīng)用
-
(root@localhost) [(none)]> select * from oms3.gl;
-
+------+
-
| abcd |
-
+------+
-
| test |
-
| gl |
-
| zg |
-
+------+
查看master相關(guān)變量
-
(root@localhost) [mysql]> show status like 'rpl_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 | 417 |
-
| Rpl_semi_sync_master_tx_wait_time | 834 |
-
| Rpl_semi_sync_master_tx_waits | 2 |
-
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
-
| Rpl_semi_sync_master_wait_sessions | 0 |
-
| Rpl_semi_sync_master_yes_tx | 1 |
-
主要關(guān)注四個(gè)參數(shù):Rpl_semi_sync_master_status ---表示當(dāng)前master節(jié)點(diǎn)是否啟用了半同步模式
Rpl_semi_sync_master_no_tx ---表示當(dāng)前未成功發(fā)送到slave節(jié)點(diǎn)的事物數(shù)量
Rpl_semi_sync_master_yes_tx ---表示當(dāng)前已成功發(fā)送到slave節(jié)點(diǎn)的事物數(shù)量
Rpl_semi_sync_master_clients---表示當(dāng)前處于半同步模式的slave節(jié)點(diǎn)數(shù)量
5.7的半同步機(jī)制變化
一:rpl_semi_sync_master_wait_slave_count參數(shù)設(shè)置有幾個(gè)slave接收到了binlog才成功返回客戶端請(qǐng)求,缺省是一臺(tái),但不可以指定是具體哪臺(tái)。
二:rpl_semi_sync_master_wait_point 參數(shù),它有兩種選擇:
AFTER_SYNC(5.7缺省值,5.6中無此選項(xiàng))
主庫把每一個(gè)事務(wù)寫到二進(jìn)制日志并且發(fā)送給從庫,主庫在等待從庫寫到自己的relay-log里的確認(rèn)信息,在接到確認(rèn)信息后,主數(shù)據(jù)庫把事務(wù)寫到存儲(chǔ)引擎里并把相應(yīng)結(jié)果反饋給客戶端。
AFTER_COMMIT(5.6缺省值)
主庫把每一個(gè)事務(wù)寫到二進(jìn)制日志并且發(fā)送給從庫,然后馬上就把事務(wù)寫到存儲(chǔ)引擎里;主庫在等待從庫寫到自己的relay-log里的信息確認(rèn)后,主庫把相應(yīng)結(jié)果反饋給客戶端。
他們倆個(gè)的區(qū)別:
AFTER_COMMIT模式的弊端在于,雖然主庫一直沒有告訴提交事務(wù)的客戶端事務(wù)已經(jīng)成功(在從庫成功確認(rèn)前)但是實(shí)際已經(jīng)提交了,其它客戶端此時(shí)已經(jīng)可以看到事務(wù)的結(jié)果了。
也就是對(duì)于事務(wù)提交者自身,的確保證了只要收到成功就一定成功了,但是其它客戶端卻可能存在發(fā)現(xiàn)事務(wù)成功了,但最后卻沒成功,因?yàn)閿?shù)據(jù)已經(jīng)到了存儲(chǔ)引擎層。
比如A客戶端執(zhí)行事務(wù)將字段id從0修改為1
1.A提交事務(wù)到主庫
2.主庫寫binlog
3.主庫發(fā)送給從庫,同時(shí)主庫提交事務(wù)到存儲(chǔ)引擎
此時(shí)還未收到從庫確認(rèn),此時(shí)A還在等待結(jié)果,但是此時(shí)另外客戶端B已經(jīng)可以看到字段id為1了。
假如此時(shí)主庫宕機(jī),如果從庫實(shí)際收到剛才的事務(wù)僅僅是主庫未收到確認(rèn),那么此時(shí)從庫的數(shù)據(jù)還是正確的也是id=1,客戶端切換到從庫后,都看到id=1
但是如果從庫沒有實(shí)際收到剛才的事務(wù),那么此時(shí)從庫上id=0,對(duì)于客戶端A,這個(gè)并沒有問題,因?yàn)锳先前提交的事務(wù)沒有收到任何反饋,所以A需要通過其它方式來確定先前事務(wù)是否成功,也就是A可以接受id=0或id=1。
但是對(duì)于客戶端B來說,已經(jīng)id=1的變成id=0則很可能是無法接受的
AFTER_SYNC模式則可以解決這個(gè)問題,繼續(xù)上面的例子:
1.A提交事務(wù)到主庫
2.主庫寫binlog
3.主庫發(fā)送給從庫
4.主庫等待從庫確認(rèn),此時(shí)id=0,沒有任何客戶端能看到id=1的結(jié)果,因?yàn)闆]有提交
5.主庫收到從庫確認(rèn),主庫開始提交到存儲(chǔ)引擎
6.主庫返回結(jié)果給客戶端
假如第4步時(shí)主庫宕機(jī),客戶端切換到從庫,如果從庫實(shí)際接收到事務(wù),那么此時(shí)id=1,如果從庫未接收到事務(wù),那么此時(shí)id=0,
無論哪種狀態(tài),對(duì)于所有客戶端數(shù)據(jù)庫都是一致,事務(wù)都沒有丟失,同樣客戶端A需要自己檢查事務(wù)狀態(tài),因?yàn)樗麤]收到反饋,業(yè)務(wù)上他本身就允許兩種可能(提交or回滾)
分享題目:MySQL5.7半同步機(jī)制
當(dāng)前鏈接:
http://weahome.cn/article/pcgccj.html