這篇文章主要介紹了半同步與異步的MySQL主從復(fù)制如何配置,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),張店企業(yè)網(wǎng)站建設(shè),張店品牌網(wǎng)站建設(shè),網(wǎng)站定制,張店網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,張店網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
一、MySQL主從復(fù)制原理
這里我以MySQL5.5為例來(lái)說一下MySQL的主從復(fù)制的原理:
首先由備節(jié)點(diǎn)的I/O線程負(fù)責(zé)向主節(jié)點(diǎn)請(qǐng)求數(shù)據(jù),主節(jié)點(diǎn)驗(yàn)證通過以后會(huì)由dump線程把數(shù)據(jù)發(fā)送給備用節(jié)點(diǎn)。備用節(jié)點(diǎn)的I/O線程收到資源后會(huì)把把這些數(shù)據(jù)寫入到中繼日志,備節(jié)點(diǎn)的SQL線程檢測(cè)到中繼日志變更后會(huì)立刻根據(jù)中繼日志的內(nèi)容跟新備庫(kù)的內(nèi)容。這樣就完成了同步的過程。
二、常見的復(fù)制模型
1、一主一從模型
這種架構(gòu)的優(yōu)點(diǎn)就是比較簡(jiǎn)單,搭建和維護(hù)都比較容易,成本也比較低。對(duì)于一些負(fù)載量不是特別大、可靠性要求不是特別高的場(chǎng)合,完全可以采用這種模型。但是對(duì)于一些負(fù)載比較大站點(diǎn),和對(duì)可用性要求比較高的場(chǎng)合,這種架構(gòu)就不太適用了。因?yàn)槿绻L問量比較大,Master節(jié)點(diǎn)的壓力會(huì)比較的,另外如果 Master崩潰,也會(huì)導(dǎo)致業(yè)務(wù)的終止.
2、一主多從模型
在絕大多數(shù)場(chǎng)景中,我們的應(yīng)用都是讀多寫。我們使用這種架構(gòu),通過讀寫分離的技術(shù),可以有效降低Master上讀的壓力。我們?cè)诤蠖说膕lave上可以做一些數(shù)據(jù)備份,數(shù)據(jù)挖掘等方面的工作。但是如果備庫(kù)比較多,同時(shí)主庫(kù)又要負(fù)責(zé)其他的請(qǐng)求時(shí),主庫(kù)的壓力會(huì)明顯增大,此時(shí)主庫(kù)會(huì)成為整個(gè)系統(tǒng)的性能瓶頸。
當(dāng)然,還有其他的復(fù)制模型,比如多級(jí)中繼,和環(huán)狀復(fù)制等,這些復(fù)制的基本原理都和上面的差不多,這里不再詳細(xì)的解釋了。
3、配置主從復(fù)制
(1)、異步復(fù)制
主從同步的條件:
Master:
a:?jiǎn)⒂枚M(jìn)制日志;
b:選擇一個(gè)server-id
c:創(chuàng)建具有復(fù)制權(quán)限的用戶
Slave:
a:?jiǎn)⒂弥欣^日志
b:選擇一個(gè)唯一的server-id
c:連接主服務(wù)器,并開始復(fù)制數(shù)據(jù)
A、首先在主庫(kù)上建立用于復(fù)制的最小權(quán)限的用戶
grant replication slave,replication client on *.* to repl@'10.12.%' identified by '123456';
B、在從庫(kù)上連接主庫(kù)
CHANGE MASTER TO MASTER_HOST='10.12.128.19',MASTER_PORT=3306,MASTER_USER='repl',
MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=451;
#查看復(fù)制的狀態(tài)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.12.128.19
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 1512
Relay_Log_File: relay_index.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1512
Relay_Log_Space: 452
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 3306
Master_UUID: 97f33396-ed12-11e4-921a-000c29e8ee06
Master_Info_File: /mydata/data5.6/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
C、然后再?gòu)膸?kù)上執(zhí)行:
啟動(dòng)復(fù)制
start slave;
也可以單獨(dú)啟動(dòng)IO線程和SQL線程。
(如果從庫(kù)的IO線程狀態(tài)一直是connecting的狀態(tài),可能是防火墻的原因,一般來(lái)說關(guān)閉防火墻或者配置防火墻規(guī)則就可以了)
(2)、半同步復(fù)制
半同步復(fù)制是基于Google為MySQL開發(fā)的半同步復(fù)制的插件。半同步復(fù)制的原理是,一個(gè)事務(wù)在主服務(wù)器上執(zhí)行完成后,必須至少確保至少在一臺(tái)從服務(wù)器上執(zhí)行完成后,事務(wù)才算提交成功。如果在一定時(shí)間內(nèi)從服務(wù)器沒有響應(yīng),則會(huì)自動(dòng)降級(jí)為異步復(fù)制。
這個(gè)半同步復(fù)制是建立在異步復(fù)制的基礎(chǔ)之上進(jìn)行的。
首先需要安裝Google的半同步插件:
master:
install plugin rpl_semi_sync_master soname 'semisync_master.so';
slave:
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
然后開啟半同步的功能
master:
set global rpl_semi_sync_master_enabled = 1;
set global rpl_semi_sync_master_timeout = 100; //以毫秒計(jì)算
slave:
set global rpl_semi_sync_slave_enabled = ON;
在從庫(kù)上還需要重啟IO線程:
stop slave IO_thread;
start slave IO_thread;
分別在主庫(kù)和備庫(kù)上查看半同步插件運(yùn)行的狀態(tài):
mysql> show global status like 'rpl%';
+--------------------------------------------+-------+
| 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 | 1 |
| Rpl_semi_sync_master_no_tx | 8 |
| 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 | 0 |
+--------------------------------------------+-------+
14 rows in set (0.04 sec)
mysql> show global status like 'rpl%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.04 sec)
可以看到,主庫(kù)和備庫(kù)的半同步插件都已經(jīng)處于啟用狀態(tài)。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“半同步與異步的MySQL主從復(fù)制如何配置”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
分享標(biāo)題:半同步與異步的MySQL主從復(fù)制如何配置
URL地址:http://bm7419.com/article4/pscjoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、標(biāo)簽優(yōu)化、商城網(wǎng)站、全網(wǎng)營(yíng)銷推廣、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)