《MySQL半同步復(fù)制》要點(diǎn):
本文介紹了MySQL半同步復(fù)制,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
一、前言
二、Mysql 根基知識(shí)
三、Mysql 復(fù)制(Replication)
四、Mysql 復(fù)制(Replication)類型
五、Mysql 主從復(fù)制基本步調(diào)
六、Mysql 主從復(fù)制(異步)
七、Mysql 主從復(fù)制(半同步)
八、Mysql 復(fù)制對(duì)象
九、Mysql 復(fù)制注意事變
十、Mysql 復(fù)制過濾
一、前言
從這一篇博客開始我們就來學(xué)習(xí)mysql的高檔課程,在前面的幾篇博客我們講解了mysql基礎(chǔ)知識(shí)、mysql日志類型、mysql配置文件、mysql備份策略,這一篇博客中我們來講解mysql的復(fù)制.
二、mysql根基知識(shí)
1.mysql日記類型
二進(jìn)制日記
事務(wù)日記
差錯(cuò)日志
一般查詢?nèi)沼?/p>
中繼日記
慢查詢?nèi)沼?/p>
注,有博友對(duì)mysql日記不怎么了解,可以參考這篇博客http://freeloda.blog.51cto.com/2033581/1253991
2.mysql二進(jìn)制日記詳解
闡明:默認(rèn)開啟,精確的記錄了用戶對(duì)數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行操作的命令和操作的數(shù)據(jù)對(duì)象.
二進(jìn)制日記文件的作用:
提供了增量備份的功效
提供了數(shù)據(jù)基于光陰點(diǎn)的恢復(fù),這個(gè)恢復(fù)的光陰點(diǎn)可以由用戶控制
為mysql的復(fù)制架構(gòu)提供根基,將這主服務(wù)器的二進(jìn)制日志復(fù)制到從服務(wù)器上并執(zhí)行同樣的操作,就可將數(shù)據(jù)進(jìn)行同步
實(shí)現(xiàn)數(shù)據(jù)的高可用
分擔(dān)當(dāng)載
二進(jìn)制日志默認(rèn)寄存位置:
數(shù)據(jù)目錄下,以mysql-bin.XXXXXX定名的日志
二進(jìn)制日志格局:
基于語句 statement
基于行 row
混合方式 mixed
二進(jìn)制日記滾動(dòng)方式:
重啟服務(wù)
日志到達(dá)最大上限
執(zhí)行flush logs
二進(jìn)制日志變亂:
position 基于位置,也便是offset(偏移量)
datetime 基于光陰
timestamp
二進(jìn)制日記的查看與刪除方式:
mysql>show master status; 查看當(dāng)前正在使用的二進(jìn)制日志
mysql>show binlog events in'mysql-bin.000001'; 查看二進(jìn)制日志記錄的變亂[from position]
mysql>flush logs; 二進(jìn)制日志滾動(dòng)
mysql>show binary logs; 查看所有二進(jìn)制日志
mysql>purge binary logs to 'mysql-bin.000003'; 刪除二進(jìn)制日志
文件系統(tǒng)中查看二進(jìn)制日志的命令:
mysqlbinlog相關(guān)選項(xiàng),--start-position #開始位置--stop-position #結(jié)束位置
--start-datetime
'yyyy-mm-dd hh:mm:ss'
;
#開始時(shí)間
--stop-datetime ''; #結(jié)束時(shí)間
sql_log_bin = {ON|OFF}
#用于控制二進(jìn)制日志信息是否記錄進(jìn)日志文件.默認(rèn)為ON,表示啟用記錄功能.用戶可以在會(huì)話級(jí)別修改此變量的值,但其必須具有SUPER權(quán)限
binlog_cache_size = 32768
#默認(rèn)值32768 Binlog Cache 用于在打開了二進(jìn)制日志(binlog)記錄功能的環(huán)境,是 MySQL 用來提高binlog的記錄效率而設(shè)計(jì)的一個(gè)用于短時(shí)間內(nèi)臨時(shí)緩存binlog數(shù)據(jù)的內(nèi)存區(qū)域.一般來說,如果我們的數(shù)據(jù)庫中沒有什么大事務(wù),寫入也不是特別頻繁,2MB~4MB是一個(gè)合適的選擇.但是如果我們的數(shù)據(jù)庫大事務(wù)較多,寫入量比較大,可與適當(dāng)調(diào)高binlog_cache_size.同時(shí),我們可以通過binlog_cache_use 以及 binlog_cache_disk_use來分析設(shè)置的binlog_cache_size是否足夠,是否有大量的binlog_cache由于內(nèi)存大小不夠而使用臨時(shí)文件(binlog_cache_disk_use)來緩存了
binlog_stmt_cache_size = 32768
#當(dāng)非事務(wù)語句使用二進(jìn)制日志緩存,但是超出binlog_stmt_cache_size時(shí),使用一個(gè)臨時(shí)文件來存放這些語句
log_bin = mysql-bin
#指定binlog的位置,默認(rèn)在數(shù)據(jù)目錄下
binlog-
format
= {ROW|STATEMENT|MIXED}#指定二進(jìn)制日志的類型,默認(rèn)為MIXED.如果設(shè)定了二進(jìn)制日志的格式,卻沒有啟用二進(jìn)制日志,則MySQL啟動(dòng)時(shí)會(huì)產(chǎn)生警告日志信息并記錄于錯(cuò)誤日志中.
sync_binlog = 10
#設(shè)定多久同步一次二進(jìn)制日志至磁盤文件中,0表示不同步,任何正數(shù)值都表示對(duì)二進(jìn)制每多少次寫操作之后同步一次.當(dāng)autocommit的值為1時(shí),每條語句的執(zhí)行都會(huì)引起二進(jìn)制日志同步,否則,每個(gè)事務(wù)的提交會(huì)引起二進(jìn)制日志同步
max_binlog_cache_size = {4096 .. 18446744073709547520}
#二進(jìn)定日志緩存空間大小,5.5.9及以后的版本僅應(yīng)用于事務(wù)緩存,其上限由max_binlog_stmt_cache_size決定.
max_binlog_stmt_cache_size = {4096 .. 18446744073709547520}
#二進(jìn)定日志緩存空間大小,5.5.9及以后的版本僅應(yīng)用于事務(wù)緩存expire_log_days = {0..99}
#設(shè)定二進(jìn)制日志的過期天數(shù),超出此天數(shù)的二進(jìn)制日志文件將被自動(dòng)刪除.默認(rèn)為0,表示不啟用過期自動(dòng)刪除功能.如果啟用此功能,自動(dòng)刪除工作通常發(fā)生在MySQL啟動(dòng)時(shí)或FLUSH日志時(shí)
注:一般建議將binlog日志與數(shù)據(jù)文件分開存放,不但可以提高mysql性能,還可以增加平安性!
1.Mysql 復(fù)制造用
負(fù)載均衡(load balancing)
備份
高可用性(high availability)和容錯(cuò)
2.Mysql 復(fù)制若何工作
主要有三步(如下圖):
master將改變記錄到二進(jìn)制日志(binary log)中(這些記錄叫做二進(jìn)制日志變亂,binary log events);
slave將master的binary log events拷貝到它的中繼日記(relay log);
slave重做中繼日志中的事件,將改變反映它本身的數(shù)據(jù).
具體闡明:
該過程的第一部分便是master記錄二進(jìn)制日志.在每個(gè)事務(wù)更新數(shù)據(jù)完成之前,master在二日志記錄這些改變.MySQL將事務(wù)串行的寫入二進(jìn)制日志,即使事務(wù)中的語句都是交叉執(zhí)行的.在事件寫入二進(jìn)制日志完成后,master通知存儲(chǔ)引擎提交事務(wù).
下一步就是slave將master的binary log拷貝到它本身的中繼日志.首先,slave開始一個(gè)工作線程——I/O線程.I/O線程在master上打開一個(gè)普通的連接,然后開始binlog dump process.Binlog dump process從master的二進(jìn)制日志中讀取事件,如果已經(jīng)跟上master,它會(huì)睡眠并等待master產(chǎn)生新的事件.I/O線程將這些事件寫入中繼日志.
SQL slave thread處理該過程的最后一步.SQL線程從中繼日志讀取事件,更新slave的數(shù)據(jù),使其與master中的數(shù)據(jù)一致.只要該線程與I/O線程堅(jiān)持一致,中繼日志通常會(huì)位于OS的緩存中,所以中繼日志的開銷很小.
此外,在master中也有一個(gè)工作線程:和其它MySQL的連接一樣,slave在master中打開一個(gè)連接也會(huì)使得master開始一個(gè)線程.復(fù)制過程有一個(gè)很重要的限制——復(fù)制在slave上是串行化的,也就是說master上的并行更新操作不能在slave上并行操作.所以slave上數(shù)據(jù)一般要慢于master上數(shù)據(jù).即master與slave之間的數(shù)據(jù)在必定時(shí)間內(nèi)會(huì)不同步.
四、Mysql 復(fù)制(Replication)類型
1.mysql復(fù)制類型有以下一些基來源根基則
每個(gè)slave只能有一個(gè)master;
每個(gè)slave只能有一個(gè)獨(dú)一的服務(wù)器ID;
每個(gè)master可以有許多slave;
假如你設(shè)置log_slave_updates,slave可以是其它slave的master,從而擴(kuò)散master的更新.
注,MySQL不支持多主服務(wù)器復(fù)制(Multimaster Replication)——即一個(gè)slave可以有多個(gè)master.但是,通過一些簡單的組合,我們卻可以建立靈活而強(qiáng)大的復(fù)制體系布局.
2.mysql復(fù)制類型
(1).一主多從模式
注,由一個(gè)master和一個(gè)slave構(gòu)成復(fù)制系統(tǒng)是最簡單的情況.Slave之間并不相互通信,只能與master進(jìn)行通信.
MySQL通過復(fù)制(Replication)實(shí)現(xiàn)存儲(chǔ)系統(tǒng)的高可用.今朝,MySQL支持的復(fù)制方式有:
異步復(fù)制(Asynchronous Replication):原理最簡單,性能最好.但是主備之間數(shù)據(jù)紛歧致的概率很大.
半同步復(fù)制(Semi-synchronous Replication):相比異步復(fù)制,半同步復(fù)制犧牲了必定的性能,提升了主備之間數(shù)據(jù)的一致性(有一些情況還是會(huì)出現(xiàn)主備數(shù)據(jù)不一致).
mysql> SHOW GLOBAL STATUS LIKE 'rpl_semi%'; +--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 1560 |
| Rpl_semi_sync_master_net_wait_time | 10920 |
| Rpl_semi_sync_master_net_waits | 7 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 1 |
| Rpl_semi_sync_master_status | OFF |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 985 |
| Rpl_semi_sync_master_tx_wait_time | 985 |
| Rpl_semi_sync_master_tx_waits | 1 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 6 |
+--------------------------------------------+-------+
14 rows
in
set
(0.00 sec)
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW GLOBAL STATUS LIKE
'rpl_semi%'
;
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF |
+----------------------------+-------+
1 row
in
set
(0.00 sec)
組復(fù)制(Group Replication):基于Paxos算法實(shí)現(xiàn)分布式數(shù)據(jù)復(fù)制的強(qiáng)一致性.只要年夜多數(shù)機(jī)器存活就能保證系統(tǒng)可用.相比半同步復(fù)制,Group Replication的數(shù)據(jù)一致性和系統(tǒng)可用性更高
具體闡明:
主動(dòng)的Master-Master復(fù)制有一些特殊的用處.例如,地理上分布的兩個(gè)部分都需要本身的可寫的數(shù)據(jù)副本.這種結(jié)構(gòu)最大的問題就是更新沖突.假設(shè)一個(gè)表只有一行(一列)的數(shù)據(jù),其值為1,如果兩個(gè)服務(wù)器分別同時(shí)執(zhí)行如下語句:
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.01 sec)
master:
mysql> create table user1 (
id
int(10));
Query OK, 0 rows affected (1.03 sec)
在第一個(gè)服務(wù)器上執(zhí)行:
那么結(jié)果是多少呢?一臺(tái)服務(wù)器是4,另一個(gè)服務(wù)器是3,但是,這并不會(huì)產(chǎn)生差錯(cuò).
mysql> UPDATE tbl SET col=col + 1;
在第二個(gè)服務(wù)器上執(zhí)行:
mysql> UPDATE tbl SET col=col * 2;
mysql> create table user (
id
int(10));
Query OK, 0 rows affected (0.42 sec)
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| user |
+----------------+
1 row
in
set
(0.00 sec)
mysql> insert user value (1);
Query OK, 1 row affected (0.34 sec)
實(shí)際上,MySQL并不支持其它一些DBMS支持的多主服務(wù)器復(fù)制(Multimaster Replication),這是MySQL的復(fù)制功能很大的一個(gè)限制(多主服務(wù)器的難點(diǎn)在于辦理更新沖突),但是,如果你實(shí)在有這種需求,你可以采用MySQL Cluster,以及將Cluster和Replication結(jié)合起來,可以建立強(qiáng)大的高性能的數(shù)據(jù)庫平臺(tái).但是,可以通過其它一些方式來模擬這種多主服務(wù)器的復(fù)制.
(3).主從模式
注,這是master-master結(jié)構(gòu)變化而來的,它避免了M-M的缺點(diǎn),實(shí)際上,這是一種具有容錯(cuò)和高可用性的系統(tǒng).它的分歧點(diǎn)在于其中一個(gè)服務(wù)只能進(jìn)行只讀操作.
mysql> SHOW GLOBAL STATUS LIKE
'rpl_semi%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row
in
set
(0.01 sec)
(4).帶從服務(wù)器的Master-Master布局(Master-Master with Slaves)
注,這種結(jié)構(gòu)的優(yōu)點(diǎn)就是提供了冗余.在地理上分布的復(fù)制結(jié)構(gòu),它不存在單一節(jié)點(diǎn)故障問題,而且還可以將讀密集型的哀求放到slave上.
mysql> SHOW GLOBAL STATUS LIKE
'rpl_semi%'
;
+--------------------------------------------+-------+
| 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 | 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.00 sec)
總結(jié):一般常用的兩種復(fù)制類型一種是主從模式,另一種是一主多從模式.在這一篇博客中我們主要講授主從模式復(fù)制.
五、Mysql 主從復(fù)制基本步調(diào)
[root@node2 ~]
# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS!
1.master 設(shè)置裝備擺設(shè)
啟用二進(jìn)制日記
設(shè)置裝備擺設(shè)一個(gè)唯一的server-id
[root@node2 ~]
# vim /etc/my.cnf
[mysqld]
rpl_semi_sync_slave_enabled=1
#啟用半同步復(fù)制
創(chuàng)立具有復(fù)制權(quán)限的用戶
2.slave 設(shè)置裝備擺設(shè)
啟用中繼日記
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME
'semisync_slave.so'
;
Query OK, 0 rows affected (0.38 sec)
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.01 sec)
設(shè)置裝備擺設(shè)一個(gè)唯一的server-id
連接主服務(wù)器,并開端復(fù)制數(shù)據(jù)
啟動(dòng)數(shù)據(jù)復(fù)制
[root@node1 ~]
# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.. SUCCESS!
注,基本步調(diào)我們就說到這里,下面我們來具體演示一下主從復(fù)制.
六、Mysql 主從復(fù)制(異步)
1.mysql異步復(fù)制
[root@node1 ~]
# vim /etc/my.cnf
[mysqld]
rpl_semi_sync_master_enabled=1
#啟用半同步
rpl_semi_sync_master_timeout=1000
#超時(shí)時(shí)間為1s
異步復(fù)制:MySQL自己支持單向的、異步的復(fù)制.異步復(fù)制意味著在把數(shù)據(jù)從一臺(tái)機(jī)器拷貝到另一臺(tái)機(jī)器時(shí)有一個(gè)延時(shí),最重要的是這意味著當(dāng)應(yīng)用系統(tǒng)的事務(wù)在主服務(wù)器上提交并確認(rèn)時(shí)數(shù)據(jù)并不能在同一時(shí)刻拷貝或應(yīng)用到從服務(wù)器上.通常這個(gè)延時(shí)是由網(wǎng)絡(luò)帶寬、資源可用性和系統(tǒng)負(fù)載決定的.然而,使用正確的組件并且調(diào)優(yōu),復(fù)制能做到接近瞬時(shí)完成.
當(dāng)主庫有更新的時(shí)候,主庫會(huì)把更新操作的SQL寫入二進(jìn)制日志(Bin log),并維護(hù)一個(gè)二進(jìn)制日志文件的索引,以便于日志文件輪回(Rotate).在從庫啟動(dòng)異步復(fù)制的時(shí)候,從庫會(huì)開啟兩個(gè)I/O線程,其中一個(gè)線程連接主庫,要求主庫把二進(jìn)制日志的變化部分傳給從庫,并把傳回的日志寫入當(dāng)?shù)卮疟P.另一個(gè)線程則負(fù)責(zé)讀取當(dāng)?shù)貙懭氲亩M(jìn)制日志,并在當(dāng)?shù)貓?zhí)行,以反映出這種變化.較老的版本在復(fù)制的時(shí)候只啟用一個(gè)I/O線程,實(shí)現(xiàn)這兩部分的功能.下面我們來具體演示一下mysql的異步復(fù)制.
2.試驗(yàn)拓?fù)?/p>
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME
'semisync_master.so'
;
Query OK, 0 rows affected (0.39 sec)
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 = 1000;
Query OK, 0 rows affected (0.00 sec)
注,Active (master,node1) 192.168.1.201 , Passive (slave,node2)192.168.1.202
3.情況配置
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
|
test
|
+--------------------+
5 rows
in
set
(0.03 sec)
光陰同步
4.操作體系
[root@node1 ~]
# ntpdate 202.120.2.101
[root@node2 ~]
# ntpdate 202.120.2.101
mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
|
test
|
+--------------------+
5 rows
in
set
(0.00 sec)
CentOS 6.4 X86_64
5.軟件版本
Mysql 5.5.33 (注,這里用的是mysql 5.5.33二進(jìn)制通用安裝包,解壓就能用)
查看master上二進(jìn)制日志
mysql> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 107 | | |
+-------------------+----------+--------------+------------------+
1 row
in
set
(0.00 sec)
銜接master服務(wù)器
mysql> change master to master_host=
'192.168.1.201'
,master_user=
'repluser'
,master_password=
'replpass'
,master_log_file=
'master-bin.000001'
,master_log_pos=107;
Query OK, 0 rows affected (0.07 sec)
查看一下slave狀態(tài)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.201
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
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: 107
Relay_Log_Space: 107
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: NULL
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: 0
1 row
in
set
(0.00 sec)
啟動(dòng)復(fù)制并查看狀態(tài)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting
for
master to send event
Master_Host: 192.168.1.201
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 254
Relay_Master_Log_File: master-bin.000001
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: 107
Relay_Log_Space: 404
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: 1
1 row
inset
(0.00 sec)
6.安裝并配置mysql
master:
(1).解壓并鏈接
[root@node2 ~]
# service mysqld restart
Shutting down MySQL..... SUCCESS!
Starting MySQL........... SUCCESS!
[root@node1 src]
# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/
[root@node1 src]
# cd /usr/local/
[root@node1
local
]
# ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64 mysql
"mysql"
->
"/usr/local/mysql-5.5.33-linux2.6-x86_64"
[root@node1
local
]
# cd mysql
[root@node1 mysql]
# ll
總用量 200
drwxr-xr-x 2 root root 4096 8月 24 17:58 bin
-rw-r--r-- 1 7161 wheel 17987 7月 15 20:01 COPYING
drwxr-xr-x 3 root root 4096 8月 24 17:58 data
drwxr-xr-x 2 root root 4096 8月 24 17:58 docs
drwxr-xr-x 3 root root 4096 8月 24 17:58 include
-rw-r--r-- 1 7161 wheel 134493 7月 15 20:01 INSTALL-BINARY
drwxr-xr-x 3 root root 4096 8月 24 17:58 lib
drwxr-xr-x 4 root root 4096 8月 24 17:58
man
drwxr-xr-x 10 root root 4096 8月 24 17:58 mysql-
test
-rw-r--r-- 1 7161 wheel 2496 7月 15 20:01 README
drwxr-xr-x 2 root root 4096 8月 24 17:58 scripts
drwxr-xr-x 27 root root 4096 8月 24 17:58 share
drwxr-xr-x 4 root root 4096 8月 24 17:58 sql-bench
drwxr-xr-x 3 root root 4096 8月 24 17:58 support-files
(2).新建mysql用戶
[root@node1 mysql]
# groupadd -g 3306 mysql
[root@node1 mysql]
# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[root@node1 mysql]
# id mysql
uid=3306(mysql) gid=3306(mysql) 組=3306(mysql)
[root@node2 ~]
# vim /etc/my.cnf
#增加下面幾行
[mysqld]
relay-log = relay-log
relay-log-index = relay-log.index
server-
id
= 10
(3).改動(dòng)mysql安裝目錄所有者與所屬組
(4).為mysql提供配置文件并改動(dòng)
[root@node1 mysql]
# chown -R root.mysql /usr/local/mysql/*
[root@node1 mysql]
# ll
總用量 200
drwxr-xr-x 2 root mysql 4096 8月 24 17:58 bin
-rw-r--r-- 1 root mysql 17987 7月 15 20:01 COPYING
drwxr-xr-x 3 root mysql 4096 8月 24 17:58 data
drwxr-xr-x 2 root mysql 4096 8月 24 17:58 docs
drwxr-xr-x 3 root mysql 4096 8月 24 17:58 include
-rw-r--r-- 1 root mysql 134493 7月 15 20:01 INSTALL-BINARY
drwxr-xr-x 3 root mysql 4096 8月 24 17:58 lib
drwxr-xr-x 4 root mysql 4096 8月 24 17:58
man
drwxr-xr-x 10 root mysql 4096 8月 24 17:58 mysql-
test
-rw-r--r-- 1 root mysql 2496 7月 15 20:01 README
drwxr-xr-x 2 root mysql 4096 8月 24 17:58 scripts
drwxr-xr-x 27 root mysql 4096 8月 24 17:58 share
drwxr-xr-x 4 root mysql 4096 8月 24 17:58 sql-bench
drwxr-xr-x 3 root mysql 4096 8月 24 17:58 support-files
[root@node1 ~]
# service mysqld restart
Shutting down MySQL....... SUCCESS!
Starting MySQL............. SUCCESS!
[root@node1 mysql]
# cp support-files/my-large.cnf /etc/my.cnf
[root@node1 mysql]
# vim /etc/my.cnf
[mysqld]
#增加一行
datadir =
/mydata/data
(5).為mysql提供啟動(dòng)劇本
[root@node1 mysql]
# cp support-files/mysql.server /etc/init.d/mysqld
[root@node1 mysql]
# chmod +x /etc/init.d/mysqld
mysql> grant replication slave on *.* to
'repluser'
@
'192.168.1.%'
identified by
'replpass'
;
Query OK, 0 rows affected (0.61 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.41 sec)
(6).初始化mysql數(shù)據(jù)庫
(7).啟動(dòng)并測(cè)試
[root@node1 ~]
# mkdir -pv /mydata/data
mkdir
: 已創(chuàng)建目錄
"/mydata"
mkdir
: 已創(chuàng)建目錄
"/mydata/data"
[root@node1 ~]
# chown -R mysql.mysql /mydata/data/
[root@node1 ~]
# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
Installing MySQL system tables...
/usr/local/mysql/bin/mysqld
: error
while
loading shared libraries: libaio.so.1: cannot
open
shared object
file
: No such
file
or directory
Installation of system tables failed! Examine the logs
in
/mydata/data/
for
more
information.
You can try to start the mysqld daemon with:
shell>
/usr/local/mysql/bin/mysqld
--skip-grant &
and use the
command
line tool
/usr/local/mysql/bin/mysql
to connect to the mysql database and
look
at the grant tables:
shell>
/usr/local/mysql/bin/mysql
-u root mysql
mysql> show tables
Try
'mysqld --help'
if
you have problems with paths. Using --log
gives you a log
in
/mydata/data/
that may be helpful.
Please consult the MySQL manual section
'Problems running mysql_install_db'
, and the manual section that
describes problems on your OS. Another information
source
are the
MySQL email archives available at http:
//lists
.mysql.com/.
Please check all of the above before mailing us! And remember,
if
you
do
mail us, you MUST use the
/usr/local/mysql/scripts/mysqlbug
script!
[root@node1 ~]
# cd /mydata/data/
[root@node1 data]
# ll
總用量 8
drwx------ 2 mysql root 4096 8月 24 18:21 mysql
drwx------ 2 mysql root 4096 8月 24 18:21
test
[root@node1 ~]
# vim /etc/my.cnf
#增加下面幾行
[mysqld]
log-bin=master-bin
log-bin-index=master-bin.index
server-
id
= 1
innodb_file_per_table = 1
啟動(dòng)報(bào)錯(cuò)
[root@node1 data]
# service mysqld start
Starting MySQL. ERROR! The server quit without updating PID
file
(
/mydata/data/node1
.
test
.com.pid).
查看一下差錯(cuò)日志
[root@node1 data]
# vim node1.test.com.err
130824 18:21:44 mysqld_safe Starting mysqld daemon with databases from
/mydata/data
/usr/local/mysql/bin/mysqld
: error
while
loading shared libraries: libaio.so.1: cannot
open
shared object
file
: No such
file
or directory
130824 18:21:44 mysqld_safe mysqld from pid
file
/mydata/data/node1
.
test
.com.pid ended
注,從差錯(cuò)日志中我們看到差一個(gè)庫文件libaio,我們用yum安裝一下即可.
[root@node1 data]
# yum install -y libaio
重新初始化mysql
[root@node1 data]
# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
啟動(dòng)mysql
[root@node1 data]
# service mysqld start
Starting MySQL... SUCCESS!
環(huán)境變量配置
[root@node1 data]
# vim /etc/profile.d/mysql.sh
export
PATH=$PATH:
/usr/local/mysql/bin
[root@node1 data]
# source /etc/profile
測(cè)試一下
[root@node1 data]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 1
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and
/or
its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and
/or
its
affiliates. Other names may be trademarks of their respective
owners.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
|
test
|
+--------------------+
4 rows
in
set
(0.03 sec)
mysql>
好了,到這里master的mysql配置完成,下面我們進(jìn)行slave的mysql配置.
slave:
啟動(dòng)mysql
[root@node2 data]
# service mysqld start
Starting MySQL... SUCCESS!
情況變量配置
[root@node2 data]
# vim /etc/profile.d/mysql.sh
export
PATH=$PATH:
/usr/local/mysql/bin
[root@node1 data]
# source /etc/profile
測(cè)試一下
[root@node2 data]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 1
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and
/or
its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and
/or
its
affiliates. Other names may be trademarks of their respective
owners.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
|
test
|
+--------------------+
4 rows
in
set
(0.06 sec)
mysql>
(1).解壓并鏈接
(2).新建mysql用戶
[root@node2 ~]
# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/
[root@node2 ~]
# cd /usr/local/
[root@node2
local
]
# ln -sv /usr/local/mysql-5.5.33-linux2.6-x86_64 mysql
"mysql"
-> "/usr/local/mysql-5.5.33-linux2.6-x86_64"
[root@node2 local]
# cd mysql
[root@node2 mysql]
# ll
總用量 200
drwxr-xr-x 2 root root 4096 8月 24 18:41 bin
-rw-r--r-- 1 7161 wheel 17987 7月 15 20:01 COPYING
drwxr-xr-x 3 root root 4096 8月 24 18:41 data
drwxr-xr-x 2 root root 4096 8月 24 18:41 docs
drwxr-xr-x 3 root root 4096 8月 24 18:41 include
-rw-r--r-- 1 7161 wheel 134493 7月 15 20:01 INSTALL-BINARY
drwxr-xr-x 3 root root 4096 8月 24 18:41 lib
drwxr-xr-x 4 root root 4096 8月 24 18:41
man
drwxr-xr-x 10 root root 4096 8月 24 18:41 mysql-
test
-rw-r--r-- 1 7161 wheel 2496 7月 15 20:01 README
drwxr-xr-x 2 root root 4096 8月 24 18:41 scripts
drwxr-xr-x 27 root root 4096 8月 24 18:41 share
drwxr-xr-x 4 root root 4096 8月 24 18:41 sql-bench
drwxr-xr-x 3 root root 4096 8月 24 18:41 support-files
先安裝libaio庫文件
[root@node2 mysql]
# yum install -y libaio
[root@node2 mysql]
# mkdir -pv /mydata/data
mkdir
: 已創(chuàng)立目錄
"/mydata"
mkdir
: 已創(chuàng)立目錄
"/mydata/data"
[root@node2 mysql]
# cd /mydata/data
[root@node2 data]
# chown -R mysql.mysql /mydata/data/
[root@node2 data]
# /usr/local/mysql/scripts/mysql_install_db --datadir=/mydata/data/ --basedir=/usr/local/mysql --user=mysql
[root@node2 data]
# ll
總用量 1084
drwx------ 2 mysql root 4096 8月 24 18:49 mysql
-rw-rw---- 1 mysql mysql 27698 8月 24 18:49 mysql-bin.000001
-rw-rw---- 1 mysql mysql 1061358 8月 24 18:49 mysql-bin.000002
-rw-rw---- 1 mysql mysql 38 8月 24 18:49 mysql-bin.index
drwx------ 2 mysql mysql 4096 8月 24 18:49 performance_schema
drwx------ 2 mysql root 4096 8月 24 18:49
test
[root@node2 mysql]
# groupadd -g 3306 mysql
[root@node2 mysql]
# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[root@node2 mysql]
# id mysql
uid=3306(mysql) gid=3306(mysql) 組=3306(mysql)
(3).修改mysql安裝目錄所有者與所屬組
[root@node2 mysql]
# chown -R root.mysql /usr/local/mysql/*
[root@node2 mysql]
# ll
總用量 200
drwxr-xr-x 2 root mysql 4096 8月 24 18:41 bin
-rw-r--r-- 1 root mysql 17987 7月 15 20:01 COPYING
drwxr-xr-x 3 root mysql 4096 8月 24 18:41 data
drwxr-xr-x 2 root mysql 4096 8月 24 18:41 docs
drwxr-xr-x 3 root mysql 4096 8月 24 18:41 include
-rw-r--r-- 1 root mysql 134493 7月 15 20:01 INSTALL-BINARY
drwxr-xr-x 3 root mysql 4096 8月 24 18:41 lib
drwxr-xr-x 4 root mysql 4096 8月 24 18:41
man
drwxr-xr-x 10 root mysql 4096 8月 24 18:41 mysql-
test
-rw-r--r-- 1 root mysql 2496 7月 15 20:01 README
drwxr-xr-x 2 root mysql 4096 8月 24 18:41 scripts
drwxr-xr-x 27 root mysql 4096 8月 24 18:41 share
drwxr-xr-x 4 root mysql 4096 8月 24 18:41 sql-bench
drwxr-xr-x 3 root mysql 4096 8月 24 18:41 support-files
[root@node2 mysql]
# cp support-files/mysql.server /etc/init.d/mysqld
[root@node2 mysql]
# chmod +x /etc/init.d/mysqld
(4).為mysql提供配置文件并改動(dòng)
(5).為mysql提供啟動(dòng)劇本
[root@node2 mysql]
# cp support-files/my-large.cnf /etc/my.cnf
[root@node2 mysql]
# vim /etc/my.cnf
[mysqld]
datadir =
/mydata/data
(6).初始化mysql數(shù)據(jù)庫
(7).啟動(dòng)并測(cè)試
好了,slave的mysql也設(shè)置裝備擺設(shè)完成了,下面我們來設(shè)置裝備擺設(shè)主從復(fù)制.
7.設(shè)置裝備擺設(shè)master
(1).改動(dòng)配置文件
(2).授權(quán)復(fù)制用戶
(3).重啟一下mysql服務(wù)
8.設(shè)置裝備擺設(shè)slave
(1).改動(dòng)配置文件
(2).重啟mysql服務(wù)
(3).銜接主服務(wù)器并復(fù)制
9.主從復(fù)制測(cè)試
master:
slave:
好了,到這里異步的主從復(fù)制到這里設(shè)置裝備擺設(shè)完成.下面我們來說一下什么是半同步復(fù)制(或說是同步也行).
七、Mysql 主從復(fù)制(半同步)
1.半同步復(fù)制
在說明半同步復(fù)制之前我們先來了解一下,什么是同步復(fù)制?同步復(fù)制:同步復(fù)制可以定義為數(shù)據(jù)在同一時(shí)刻被提交到一臺(tái)或多臺(tái)機(jī)器,通常這是通過眾所周知的“兩階段提交”做到的.雖然這確實(shí)給你在多系統(tǒng)中堅(jiān)持一致性,但也由于增加了額外的消息交換而造成性能下降.使用MyISAM或者InnoDB存儲(chǔ)引擎的MySQL本身并不支持同步復(fù)制,然而有些技術(shù),例如分布式復(fù)制塊設(shè)備(簡稱DRBD),可以在下層的文件系統(tǒng)提供同步復(fù)制,允許第二個(gè)MySQL服務(wù)器在主服務(wù)器丟失的情況下接管(使用第二服務(wù)器的復(fù)本).了解了同步復(fù)制我們正下面來說一下,什么是半同步復(fù)制?
MYSQL 5.5開始,支持半自動(dòng)復(fù)制.之前版本的MySQL Replication都是異步(asynchronous)的,主庫在執(zhí)行完一些事務(wù)后,是不會(huì)管備庫的進(jìn)度的.如果備庫不幸落后,而更不幸的是主庫此時(shí)又出現(xiàn)Crash(例如宕機(jī)),這時(shí)備庫中的數(shù)據(jù)就是不完整的.簡而言之,在主庫發(fā)生故障的時(shí)候,我們無法使用備庫來繼續(xù)提供數(shù)據(jù)一致的服務(wù)了.Semisynchronous Replication(半同步復(fù)制)則必定程度上保證提交的事務(wù)已經(jīng)傳給了至少一個(gè)備庫.Semi synchronous中,僅僅保證事務(wù)的已經(jīng)傳遞到備庫上,但是并不確保已經(jīng)在備庫上執(zhí)行完成了.
此外,還有一種情況會(huì)導(dǎo)致主備數(shù)據(jù)不一致.在某個(gè)session中,主庫上提交一個(gè)事務(wù)后,會(huì)等待事務(wù)傳遞給至少一個(gè)備庫,如果在這個(gè)等待過程中主庫Crash,那么也可能備庫和主庫不一致,這是很致命的.如果主備網(wǎng)絡(luò)故障或者備庫掛了,主庫在事務(wù)提交后等待10秒(rpl_semi_sync_master_timeout的默認(rèn)值)后,就會(huì)繼續(xù).這時(shí),主庫就會(huì)變回本來的異步狀態(tài).
MySQL在加載并開啟Semi-sync插件后,每一個(gè)事務(wù)需期待備庫接收日志后才返回給客戶端.如果做的是小事務(wù),兩臺(tái)主機(jī)的延遲又較小,則Semi-sync可以實(shí)現(xiàn)在性能很小損失的情況下的零數(shù)據(jù)丟失.
2.異步與半同步異同
默認(rèn)情況下MySQL的復(fù)制是異步的,Master上所有的更新操作寫入Binlog之后并不確保所有的更新都被復(fù)制到Slave之上.異步操作雖然效率高,但是在Master/Slave出現(xiàn)問題的時(shí)候,存在很高數(shù)據(jù)分歧步的風(fēng)險(xiǎn),甚至可能丟失數(shù)據(jù).
MySQL5.5引入半同步復(fù)制功效的目的是為了保證在master出問題的時(shí)候,至少有一臺(tái)Slave的數(shù)據(jù)是完整的.在超時(shí)的情況下也可以臨時(shí)轉(zhuǎn)入異步復(fù)制,保障業(yè)務(wù)的正常使用,直到一臺(tái)salve追趕上之后,繼續(xù)切換到半同步模式.
3.具體設(shè)置裝備擺設(shè)
注,mysql5.5半同步插件是由谷歌提供,具體地位/usr/local/mysql/lib/plugin/下,一個(gè)是master用的semisync_master.so,一個(gè)是slave用的semisync_slave.so,下面我們就來具體配置一下.
master:
(1).安裝插件
(2).改動(dòng)配置文件
(3).從新啟動(dòng)服務(wù)
slave:
(1).安裝插件
(2).改動(dòng)配置文件
(3).從新啟動(dòng)服務(wù)
4.查看一下狀況
master:
slave:
5.測(cè)試一下
master:
注,大家可以看到創(chuàng)建一個(gè)表的插入一個(gè)數(shù)據(jù)的時(shí)間都很長,闡明半同步配置完成.
6.模擬一下故障
slave:
注,年夜家可以看到主服務(wù)器會(huì)卡1s,我們超時(shí)時(shí)間設(shè)置的為1s.
7.查看一下狀況
好了,到這里我們就配置完成了半同步復(fù)制.希望年夜家有所收獲.下面我們來簡單說一下mysql復(fù)制的工具.
八、Mysql 復(fù)制對(duì)象
1.percona-toolkit簡介
percona-toolkit是一組高級(jí)命令行工具的集合,用來執(zhí)行各種通過手工執(zhí)行非常復(fù)雜和麻煩的mysql和系統(tǒng)任務(wù),這些任務(wù)包含:
反省master和slave數(shù)據(jù)的一致性
有效地對(duì)記載進(jìn)行歸檔
查找反復(fù)的索引
對(duì)服務(wù)器信息進(jìn)行匯總
闡發(fā)來自日志和tcpdump的查詢
當(dāng)系統(tǒng)出問題的時(shí)候收集緊張的系統(tǒng)信息
MySQL半同步復(fù)制的實(shí)現(xiàn)是建立在MySQL異步復(fù)制的基礎(chǔ)上的.MySQL支持兩種略有分歧的半同步復(fù)制:AFTER_SYNC
和AFTER_COMMIT
(受rpl_semi_sync_master_wait_wait_point控制).
開啟半同步復(fù)制時(shí),Master在返回之前會(huì)期待Slave的響應(yīng)或超時(shí).當(dāng)Slave超時(shí)時(shí),半同步復(fù)制退化成異步復(fù)制.這也是MySQL半同步復(fù)制存在的一個(gè)問題.本文不討論Salve超時(shí)的情形(不討論異步復(fù)制).
半同步復(fù)制AFTER_SYNC模式的根本流程
AFTER_SYNC
模式是MySQL 5.7才支撐的半同步復(fù)制方式,也是MySQL5.7默認(rèn)的半同步復(fù)制方式:
Prepare the transaction in the storage engine(s).
Write the transaction to the binlog, flush the binlog to disk.
Wait for at least one slave to acknowledge the reception for the binlog events for the transaction.
Commit the transaction to the storage engine(s).
半同步復(fù)制AFTER_COMMIT模式的根本流程
MySQL 5.5和5.6的半同步復(fù)制只支撐AFTER_COMMIT
:
Prepare the transaction in the storage engine(s).
Write the transaction to the binlog, flush the binlog to disk.
Commit the transaction to the storage engine(s).
Wait for at least one slave to acknowledge the reception for the binlog events for the transaction.
AFTER_SYNC和AFTER_COMMIT兩種方式的小結(jié)
AFTER_SYNC
: 日記復(fù)制到Slave之后,Master再commit.
所有在master上commit的事務(wù)都曾經(jīng)復(fù)制到slave.
所有已經(jīng)復(fù)制到slave的事務(wù)在master不一定commit了(好比,master將日志復(fù)制到slave之后,在commit之前宕機(jī)了)
AFTER_COMMIT
:Master commit之后再將日記復(fù)制到Slave.
所有master上commit的事務(wù)不一定復(fù)制到slave.(好比,master commit之后,還沒來得及將日志復(fù)制到slave就宕機(jī)了)
所有已經(jīng)復(fù)制到slave的事務(wù)在master上必定commit了.
很明顯,AFTER_COMMIT
在master宕機(jī)的環(huán)境下,無法保證數(shù)據(jù)的一致性(master commit之后,還沒來得及將日志復(fù)制到slave就宕機(jī)了).本文接下來只討論AFTER_SYNC模式.
MySQL5.7.3開始支持配置半同步復(fù)制期待Slave應(yīng)答的個(gè)數(shù):rpl_semi_sync_master_wait_slave_count .
AFTER_SYNC模式下的異常情況闡發(fā)
非常情況1:master宕機(jī)后,主備切換.
master執(zhí)行事務(wù)T,在將事務(wù)T的binlog刷到硬盤之前,master產(chǎn)生宕機(jī).slave升級(jí)為master.master重啟后,crash recovery會(huì)對(duì)事務(wù)T進(jìn)行回滾.主備數(shù)據(jù)一致.
master執(zhí)行事務(wù)T,在將事務(wù)T的binlog刷到硬盤之后,收到slave的ACK之前,master產(chǎn)生宕機(jī)(存在pendinglog).slave升級(jí)為master.
2.1 slave還沒有收到事務(wù)T的binlog,master重啟后,crash recovery會(huì)直接提交pendinglog.主備數(shù)據(jù)紛歧致.
2.2 slave已經(jīng)收到事務(wù)T的binlog.主備數(shù)據(jù)同等.
非常情況2:master宕機(jī)后,不切換主機(jī).只需考慮非常情況1中的2.1.
master重啟后,直接提交pendinglog,此時(shí),主備數(shù)據(jù)紛歧致:
slave連接上master,通過異步復(fù)制的方式得到事務(wù)T的binlog.主備數(shù)據(jù)一致.
slave還沒來得及復(fù)制事務(wù)T的binlog,如果master又發(fā)生宕機(jī),磁盤損壞.主備數(shù)據(jù)紛歧致,事務(wù)T的數(shù)據(jù)丟失.
異常情況處置
從上面異常情況的簡單分析我們得知,半同步復(fù)制必要處理master宕機(jī)后重啟存在pendinglog(slave沒有應(yīng)答的binlog)的特殊情況.
針對(duì)master宕機(jī)后,不進(jìn)行主備切換的情形:
在crash recovery之后,master比及slave的連接和復(fù)制,直到至少有一個(gè)slave復(fù)制了所有已提交的事務(wù)的binlog.(SHOW MASTER STATUS on master and SELECT master_pos_wait() on slave
).
針對(duì)master宕機(jī)后,進(jìn)行主備切換的情形:
舊master重啟后,在crash recovery時(shí),對(duì)pendinglog進(jìn)行回滾.(人工截?cái)鄊aster的binlog未復(fù)制的部門?)
思慮
為什么master重啟之后,crash recovery的過程中,是直接commit pendinglog,而不是重試哀求slave的應(yīng)答呢?
MySQL的異步復(fù)制和半同步復(fù)制都是由slave觸發(fā)的,slave主動(dòng)去銜接master同步binlog.
沒有產(chǎn)生主備切換,機(jī)器重啟后無法知道哪臺(tái)機(jī)器是slave.
如果產(chǎn)生主備切換,它已經(jīng)不是master了,則不會(huì)再有slave連上來.如果繼續(xù)等待,則無法正常運(yùn)行.
總結(jié)
MySQL半同步復(fù)制存在以下問題:
當(dāng)Slave超不時(shí),會(huì)退化成異步復(fù)制.
當(dāng)Master宕機(jī)時(shí),數(shù)據(jù)一致性無法保證,必要人工處理.
復(fù)制是串行的.
正因?yàn)镸ySQL在主備數(shù)據(jù)一致性存在著這些問題,影響了互聯(lián)網(wǎng)業(yè)務(wù)7*24的高可用服務(wù),因此各大公司紛紛祭出本身的“補(bǔ)丁”:騰訊的TDSQL、微信的PhxSQL、阿里的AliSQL、網(wǎng)易的InnoSQL.
MySQL官方曾經(jīng)在MySQL5.7推出新的復(fù)制模式——MySQL Group Replication.
歡迎參與《MySQL半同步復(fù)制》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/7084.html