站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter聽6.聽Replication in MySQL - MySQL 5.1参考手册中文版

绗6绔狅細MySQL涓殑澶嶅埗

鏈珷鎻忚堪浜MySQL鎻愪緵鐨勫悇绉嶅鍒剁壒鎬с傚紩鍏ヤ簡澶嶅埗姒傚康锛屾樉绀哄浣曡缃鍒舵湇鍔″櫒鍜屾湇鍔′互鎸囧鐩稿簲鐨勫鍒堕夐」銆傝繕鎻愪緵浜FAQ(浠ュ強绛旀) 鍒楄〃锛屼互鍙婅В鍐冲鍒堕棶棰樼殑鎺掗敊寤鸿銆

鍏充簬澶嶅埗鐩稿叧鐨SQL璇彞鐨勮娉曟弿杩帮紝鍙傝13.6鑺傦紝鈥滃鍒惰鍙モ

鎴戜滑寤鸿浣犵粡甯歌闂垜浠殑缃戝潃http://www.mysql.com锛屽苟妫鏌ュ鏈珷鐨勪慨鏀广傚鍒跺湪涓嶆柇鍦板緱鍒版敼杩涳紝鎴戜滑鐢ㄦ渶鏂扮殑淇℃伅瀹氭湡鏇存柊鏈墜鍐屻

6.1. 澶嶅埗浠嬬粛

MySQL鏀寔鍗曞悜銆佸紓姝ュ鍒讹紝澶嶅埗杩囩▼涓竴涓湇鍔″櫒鍏呭綋涓绘湇鍔″櫒锛岃屼竴涓垨澶氫釜鍏跺畠鏈嶅姟鍣ㄥ厖褰撲粠鏈嶅姟鍣ㄣ(杩欎笌鍚屾澶嶅埗鍙互杩涜瀵规瘮锛鍚屾澶嶅埗鏄MySQL绨囩殑涓涓壒寰佲斿弬瑙绗17绔狅細MySQL绨涓绘湇鍔″櫒灏嗘洿鏂板啓鍏ヤ簩杩涘埗鏃ュ織鏂囦欢锛屽苟缁存姢鏂囦欢鐨勪竴涓储寮曚互璺熻釜鏃ュ織寰幆銆傝繖浜涙棩蹇楀彲浠ヨ褰曞彂閫佸埌浠庢湇鍔″櫒鐨勬洿鏂般傚綋涓涓粠鏈嶅姟鍣ㄨ繛鎺ヤ富鏈嶅姟鍣ㄦ椂锛屽畠閫氱煡涓绘湇鍔″櫒浠庢湇鍔″櫒鍦ㄦ棩蹇椾腑璇诲彇鐨勬渶鍚庝竴娆℃垚鍔熸洿鏂扮殑浣嶇疆銆備粠鏈嶅姟鍣ㄦ帴鏀朵粠閭f椂璧峰彂鐢熺殑浠讳綍鏇存柊锛岀劧鍚庡皝閿佸苟绛夊緟涓绘湇鍔″櫒閫氱煡鏂扮殑鏇存柊銆

濡傛灉浣犳兂瑕佽缃摼寮忓鍒舵湇鍔″櫒锛屼粠鏈嶅姟鍣ㄦ湰韬篃鍙互鍏呭綋涓绘湇鍔″櫒銆

璇锋敞鎰忓綋浣犺繘琛屽鍒舵椂锛屾墍鏈夊澶嶅埗涓殑琛ㄧ殑鏇存柊蹇呴』鍦ㄤ富鏈嶅姟鍣ㄤ笂杩涜銆傚惁鍒欙紝浣犲繀椤昏灏忓績锛屼互閬垮厤鐢ㄦ埛瀵逛富鏈嶅姟鍣ㄤ笂鐨勮〃杩涜鐨勬洿鏂颁笌瀵逛粠鏈嶅姟鍣ㄤ笂鐨勮〃鎵杩涜鐨勬洿鏂颁箣闂寸殑鍐茬獊銆

鍗曞悜澶嶅埗鏈夊埄浜庡仴澹с侀熷害鍜岀郴缁熺鐞嗭細

         涓绘湇鍔″櫒/浠庢湇鍔″櫒璁剧疆澧炲姞浜嗗仴澹с備富鏈嶅姟鍣ㄥ嚭鐜伴棶棰樻椂锛屼綘鍙互鍒囨崲鍒颁粠鏈嶅姟鍣ㄤ綔涓哄浠姐

         閫氳繃鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涔嬮棿鍒囧垎澶勭悊瀹㈡埛鏌ヨ鐨勮礋鑽凤紝鍙互寰楀埌鏇村ソ鐨勫鎴峰搷搴旀椂闂淬SELECT鏌ヨ鍙互鍙戦佸埌浠庢湇鍔″櫒浠ラ檷浣庝富鏈嶅姟鍣ㄧ殑鏌ヨ澶勭悊璐熻嵎銆備絾淇敼鏁版嵁鐨勮鍙ヤ粛鐒跺簲鍙戦佸埌涓绘湇鍔″櫒锛屼互渚夸富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒淇濇寔鍚屾銆傚鏋滈潪鏇存柊鏌ヨ涓轰富锛岃璐熻浇鍧囪 绛栫暐寰堟湁鏁堬紝浣嗕竴鑸槸鏇存柊鏌ヨ銆

         浣跨敤澶嶅埗鐨勫彟涓涓ソ澶勬槸鍙互浣跨敤涓涓粠鏈嶅姟鍣ㄦ墽琛屽浠斤紝鑰屼笉浼氬共鎵颁富鏈嶅姟鍣ㄣ傚湪澶囦唤杩囩▼涓富鏈嶅姟鍣ㄥ彲浠ョ户缁鐞嗘洿鏂般傚弬瑙5.9.1鑺傦紝鈥滄暟鎹簱澶囦唤鈥

6.2. 澶嶅埗瀹炴柦姒傝堪

MySQL澶嶅埗鍩轰簬涓绘湇鍔″櫒鍦ㄤ簩杩涘埗鏃ュ織涓窡韪墍鏈夊鏁版嵁搴撶殑鏇存敼(鏇存柊銆佸垹闄ょ瓑绛)銆傚洜姝わ紝瑕佽繘琛屽鍒讹紝蹇呴』鍦ㄤ富鏈嶅姟鍣ㄤ笂鍚敤浜岃繘鍒舵棩蹇椼傚弬瑙5.11.3鑺傦紝鈥滀簩杩涘埗鏃ュ織鈥

姣忎釜浠庢湇鍔″櫒浠庝富鏈嶅姟鍣ㄦ帴鏀朵富鏈嶅姟鍣ㄥ凡缁忚褰曞埌鍏朵簩杩涘埗鏃ュ織鐨勪繚瀛樼殑鏇存柊锛屼互渚夸粠鏈嶅姟鍣ㄥ彲浠ュ鍏舵暟鎹嫹璐濇墽琛岀浉鍚岀殑鏇存柊銆

璁よ瘑鍒颁簩杩涘埗鏃ュ織鍙槸涓涓粠鍚敤浜岃繘鍒舵棩蹇楃殑鍥哄畾鏃堕棿鐐瑰紑濮嬬殑璁板綍闈炲父閲嶈銆備换浣曡缃殑浠庢湇鍔″櫒闇瑕佷富鏈嶅姟鍣ㄤ笂鐨鍦ㄤ富鏈嶅姟鍣ㄤ笂鍚敤浜岃繘鍒舵棩蹇楁椂鐨鏁版嵁搴撴嫹璐濄傚鏋滃惎鍔ㄤ粠鏈嶅姟鍣ㄦ椂锛屽叾鏁版嵁搴撲笌涓绘湇鍔″櫒涓婄殑鍚姩浜岃繘鍒舵棩蹇楁椂鐨勭姸鎬佷笉鐩稿悓锛屼粠鏈嶅姟鍣ㄥ緢鍙兘澶辫触銆

灏嗕富鏈嶅姟鍣ㄧ殑鏁版嵁鎷疯礉鍒颁粠鏈嶅姟鍣ㄧ殑涓涓斿緞鏄娇鐢LOAD DATA FROM MASTER璇彞銆傝娉ㄦ剰LOAD DATA FROM MASTER鐩墠鍙湪鎵鏈夎〃浣跨敤MyISAM瀛樺偍寮曟搸鐨勪富鏈嶅姟鍣ㄤ笂宸ヤ綔銆傚苟涓旓紝璇ヨ鍙ュ皢鑾峰緱鍏ㄥ眬璇婚攣瀹氾紝鍥犳褰撹〃姝e鍒跺埌浠庢湇鍔″櫒涓婃椂锛屼笉鍙兘鍦ㄤ富鏈嶅姟鍣ㄤ笂杩涜鏇存柊銆傚綋鎴戜滑鎵ц琛ㄧ殑鏃犻攣鐑浠芥椂锛屽垯涓嶅啀闇瑕佸叏灞璇婚攣瀹氥

鐢变簬杩欎簺闄愬埗锛屾垜浠缓璁彧鏈変富鏈嶅姟鍣ㄤ笂鐨勬暟鎹泦鐩稿杈冨皬锛屾垨鑰呬富鏈嶅姟鍣ㄤ笂寤惰繜璇婚攣瀹氬凡缁忚鎺ュ彈锛屾墠鍙互浣跨敤LOAD DATA FROM MASTER銆傝LOAD DATA FROM MASTER鐨勫疄闄呴熷害闅忕郴缁熺殑涓嶅悓鑰屼笉鍚岋紝瀵逛簬鎵ц鏃堕棿锛屾渶濂界殑瑙勫垯鏄瘡1MB鐨勬暟鎹敤1绉掗挓銆傝繖鏄竴涓矖鐣ョ殑浼拌锛屼絾浣犱細鍙戠幇濡傛灉涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄧ殑鎬ц兘涓婄瓑浠蜂簬700MHz Pentium CPU锛岄氳繃100Mbps鐨勭綉缁滆繘琛岃繛鎺ワ紝鍒欒浼拌鐩稿綋鍑嗙‘銆

浠庢湇鍔″櫒璁剧疆涓哄鍒朵富鏈嶅姟鍣ㄧ殑鏁版嵁鍚庯紝瀹冭繛鎺ヤ富鏈嶅姟鍣ㄥ苟绛夊緟鏇存柊杩囩▼銆傚鏋滀富鏈嶅姟鍣ㄥけ璐ワ紝鎴栬呬粠鏈嶅姟鍣ㄥけ鍘讳笌涓绘湇鍔″櫒涔嬮棿鐨勮繛鎺ワ紝浠庢湇鍔″櫒淇濇寔瀹氭湡灏濊瘯杩炴帴锛岀洿鍒板畠鑳藉缁х画甯у惉鏇存柊銆傜敱--master-connect-retry閫夐」鎺у埗閲嶈瘯闂撮殧銆 榛樿涓60绉掋

姣忎釜浠庢湇鍔″櫒璺熻釜澶嶅埗鏃堕棿銆備富鏈嶅姟鍣ㄤ笉鐭ラ亾鏈夊灏戜釜浠庢湇鍔″櫒鎴栧湪鏌愪竴鏃跺埢鏈夊摢浜涜鏇存柊浜嗐

6.3. 澶嶅埗瀹炴柦缁嗚妭

MySQL浣跨敤3涓嚎绋嬫潵鎵ц澶嶅埗鍔熻兘(鍏朵腑1涓湪涓绘湇鍔″櫒涓婏紝鍙︿袱涓湪浠庢湇鍔″櫒涓娿傚綋鍙戝嚭START SLAVE鏃讹紝浠庢湇鍔″櫒鍒涘缓涓涓I/O绾跨▼锛屼互杩炴帴涓绘湇鍔″櫒骞惰瀹冨彂閫佽褰曞湪鍏朵簩杩涘埗鏃ュ織涓殑璇彞銆備富鏈嶅姟鍣ㄥ垱寤轰竴涓嚎绋嬪皢浜岃繘鍒舵棩蹇椾腑鐨勫唴瀹瑰彂閫佸埌浠庢湇鍔″櫒銆傝绾跨▼鍙互璇嗗埆涓轰富鏈嶅姟鍣ㄤ笂SHOW PROCESSLIST鐨勮緭鍑轰腑鐨Binlog Dump绾跨▼銆備粠鏈嶅姟鍣I/O绾跨▼璇诲彇涓绘湇鍔″櫒Binlog Dump绾跨▼鍙戦佺殑鍐呭骞跺皢璇ユ暟鎹嫹璐濆埌浠庢湇鍔″櫒鏁版嵁鐩綍涓殑鏈湴鏂囦欢涓紝鍗涓户鏃ュ織銆傜3涓嚎绋嬫槸SQL绾跨▼锛屾槸浠庢湇鍔″櫒鍒涘缓鐢ㄤ簬璇诲彇涓户鏃ュ織骞舵墽琛屾棩蹇椾腑鍖呭惈鐨勬洿鏂般

鍦ㄥ墠闈㈢殑鎻忚堪涓紝姣忎釜浠庢湇鍔″櫒鏈3涓嚎绋嬨傛湁澶氫釜浠庢湇鍔″櫒鐨勪富鏈嶅姟鍣ㄥ垱寤轰负姣忎釜褰撳墠杩炴帴鐨勪粠鏈嶅姟鍣ㄥ垱寤轰竴涓嚎绋嬶紱姣忎釜浠庢湇鍔″櫒鏈夎嚜宸辩殑I/OSQL绾跨▼銆

杩欐牱璇诲彇鍜屾墽琛岃鍙ヨ鍒嗘垚涓や釜鐙珛鐨勪换鍔°傚鏋滆鍙ユ墽琛岃緝鎱㈠垯璇彞璇诲彇浠诲姟娌℃湁鎱笅鏉ャ備緥濡傦紝濡傛灉浠庢湇鍔″櫒鏈変竴娈垫椂闂存病鏈夎繍琛屼簡锛屽綋浠庢湇鍔″櫒鍚姩鏃讹紝鍏I/O绾跨▼鍙互寰堝揩鍦颁粠涓绘湇鍔″櫒绱㈠彇鎵鏈変簩杩涘埗鏃ュ織鍐呭锛屽嵆浣SQL绾跨▼杩滆繙婊炲悗銆傚鏋滀粠鏈嶅姟鍣ㄥ湪SQL绾跨▼鎵ц瀹屾墍鏈夌储鍙栫殑璇彞鍓嶅仠姝紝I/O 绾跨▼鑷冲皯宸茬粡绱㈠彇浜嗘墍鏈夊唴瀹癸紝浠ヤ究璇彞鐨勫畨鍏ㄦ嫹璐濅繚瀛樺埌鏈湴浠庢湇鍔″櫒鐨勪腑缁ф棩蹇椾腑锛屼緵浠庢湇鍔″櫒涓嬫鍚姩鏃舵墽琛屻傝繖鏍峰厑璁告竻绌轰富鏈嶅姟鍣ㄤ笂鐨勪簩杩涘埗鏃ュ織锛屽洜涓轰笉鍐嶉渶瑕佺瓑鍊欎粠鏈嶅姟鍣ㄦ潵绱㈠彇鍏跺唴瀹广

SHOW PROCESSLIST璇彞鍙互鎻愪緵鍦ㄤ富鏈嶅姟鍣ㄤ笂鍜屼粠鏈嶅姟鍣ㄤ笂鍙戠敓鐨勫叧浜庡鍒剁殑淇℃伅銆

涓嬮潰鐨勪緥瀛愯鏄庝簡杩3涓嚎绋嬪湪SHOW PROCESSLIST涓殑鏄剧ず銆

鍦ㄤ富鏈嶅姟鍣ㄤ笂锛SHOW PROCESSLIST鐨勮緭鍑虹湅涓婂幓搴斾负锛

mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
     Id: 2
   User: root
   Host: localhost:32931
     db: NULL
Command: Binlog Dump
   Time: 94
  State: Has sent all binlog to slave; waiting for binlog to
         be updated
   Info: NULL

杩欏効锛岀嚎绋2鏄竴涓繛鎺ヤ粠鏈嶅姟鍣ㄧ殑澶嶅埗绾跨▼銆傝淇℃伅琛ㄧず鎵鏈変富瑕佹洿鏂板凡缁忚鍙戦佸埌浠庢湇鍔″櫒锛屼富鏈嶅姟鍣ㄦ绛夊緟鏇村鐨勬洿鏂板嚭鐜般

鍦ㄤ粠鏈嶅姟鍣ㄤ笂锛SHOW PROCESSLIST鐨勮緭鍑虹湅涓婂幓搴斾负锛

mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
     Id: 10
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 11
  State: Waiting for master to send event
   Info: NULL
*************************** 2. row ***************************
     Id: 11
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 11
  State: Has read all relay log; waiting for the slave I/O
         thread to update it
   Info: NULL

璇ヤ俊鎭〃绀虹嚎绋10鏄悓涓绘湇鍔″櫒閫氫俊鐨I/O绾跨▼锛岀嚎绋11鏄鐞嗕繚瀛樺湪涓户鏃ュ織涓殑鏇存柊鐨SQL绾跨▼銆SHOW PROCESSLIST杩愯鏃讹紝涓や釜绾跨▼鍧囩┖闂诧紝绛夊緟鍏跺畠鏇存柊銆

璇锋敞鎰Time鍒楃殑鍊煎彲浠ユ樉绀轰粠鏈嶅姟鍣ㄦ瘮涓绘湇鍔″櫒婊炲悗澶氶暱鏃堕棿銆傚弬瑙6.9鑺傦紝鈥滃鍒禙AQ鈥

6.3.1. 澶嶅埗涓荤嚎绋嬬姸鎬

涓嬮潰鍒楀嚭浜嗕富鏈嶅姟鍣ㄧ殑Binlog Dump绾跨▼鐨State鍒楃殑鏈甯歌鐨勭姸鎬併傚鏋滀綘娌℃湁鍦ㄤ富鏈嶅姟鍣ㄤ笂鐪嬭浠讳綍Binlog Dump绾跨▼锛岃繖璇存槑澶嶅埗娌℃湁鍦ㄨ繍琛屸斿嵆锛岀洰鍓嶆病鏈夎繛鎺ヤ换浣曚粠鏈嶅姟鍣ㄣ

         Sending binlog event to slave

浜岃繘鍒舵棩蹇楃敱鍚勭浜嬩欢缁勬垚锛屼竴涓簨浠堕氬父涓轰竴涓洿鏂板姞涓浜涘叾瀹冧俊鎭傜嚎绋嬪凡缁忎粠浜岃繘鍒舵棩蹇楄鍙栦簡涓涓簨浠跺苟涓旀灏嗗畠鍙戦佸埌浠庢湇鍔″櫒銆

         Finished reading one binlog; switching to next binlog

绾跨▼宸茬粡璇诲畬浜岃繘鍒舵棩蹇楁枃浠跺苟涓旀鎵撳紑涓嬩竴涓鍙戦佸埌浠庢湇鍔″櫒鐨勬棩蹇楁枃浠躲

         Has sent all binlog to slave; waiting for binlog to be updated

绾跨▼宸茬粡浠庝簩杩涘埗鏃ュ織璇诲彇鎵鏈変富瑕佺殑鏇存柊骞跺凡缁忓彂閫佸埌浜嗕粠鏈嶅姟鍣ㄣ傜嚎绋嬬幇鍦ㄦ绌洪棽锛岀瓑寰呯敱涓绘湇鍔″櫒涓婃柊鐨勬洿鏂板鑷寸殑鍑虹幇鍦ㄤ簩杩涘埗鏃ュ織涓殑鏂颁簨浠躲

         Waiting to finalize termination

绾跨▼鍋滄鏃跺彂鐢熺殑涓涓緢绠鍗曠殑鐘舵併

6.3.2. 澶嶅埗浠嶪/O绾跨▼鐘舵

涓嬮潰鍒楀嚭浜嗕粠鏈嶅姟鍣ㄧ殑I/O绾跨▼鐨State鍒楃殑鏈甯歌鐨勭姸鎬併傝鐘舵佷篃鍑虹幇鍦Slave_IO_State鍒楋紝鐢SHOW SLAVE STATUS鏄剧ず銆傝繖璇存槑浣犲彲浠ュ彧閫氳繃璇ヨ鍙ヤ粩缁嗘祻瑙堟墍鍙戠敓鐨勪簨鎯呫

         Connecting to master

绾跨▼姝h瘯鍥捐繛鎺ヤ富鏈嶅姟鍣ㄣ

         Checking master version

寤虹珛鍚屼富鏈嶅姟鍣ㄤ箣闂寸殑杩炴帴鍚庣珛鍗充复鏃跺嚭鐜扮殑鐘舵併

         Registering slave on master

寤虹珛鍚屼富鏈嶅姟鍣ㄤ箣闂寸殑杩炴帴鍚庣珛鍗充复鏃跺嚭鐜扮殑鐘舵併

         Requesting binlog dump

寤虹珛鍚屼富鏈嶅姟鍣ㄤ箣闂寸殑杩炴帴鍚庣珛鍗充复鏃跺嚭鐜扮殑鐘舵併傜嚎绋嬪悜涓绘湇鍔″櫒鍙戦佷竴鏉¤姹傦紝绱㈠彇浠庤姹傜殑浜岃繘鍒舵棩蹇楁枃浠跺悕鍜屼綅缃紑濮嬬殑浜岃繘鍒舵棩蹇楃殑鍐呭銆

         Waiting to reconnect after a failed binlog dump request

濡傛灉浜岃繘鍒舵棩蹇楄浆鍌ㄨ姹傚け璐(鐢变簬娌℃湁杩炴帴)锛岀嚎绋嬭繘鍏ョ潯鐪犵姸鎬侊紝鐒跺悗瀹氭湡灏濊瘯閲嶆柊杩炴帴銆傚彲浠ヤ娇鐢--master-connect-retry閫夐」鎸囧畾閲嶈瘯涔嬮棿鐨勯棿闅斻

         Reconnecting after a failed binlog dump request

绾跨▼姝e皾璇曢噸鏂拌繛鎺ヤ富鏈嶅姟鍣ㄣ

         Waiting for master to send event

绾跨▼宸茬粡杩炴帴涓婁富鏈嶅姟鍣紝姝g瓑寰呬簩杩涘埗鏃ュ織浜嬩欢鍒拌揪銆傚鏋滀富鏈嶅姟鍣ㄦ绌洪棽锛屼細鎸佺画杈冮暱鐨勬椂闂淬傚鏋滅瓑寰呮寔缁slave_read_timeout绉掞紝鍒欏彂鐢熻秴鏃躲傛鏃讹紝绾跨▼璁や负杩炴帴琚腑鏂苟浼佸浘閲嶆柊杩炴帴銆

         Queueing master event to the relay log

绾跨▼宸茬粡璇诲彇涓涓簨浠讹紝姝e皢瀹冨鍒跺埌涓户鏃ュ織渚SQL绾跨▼鏉ュ鐞嗐

         Waiting to reconnect after a failed master event read

璇诲彇鏃(鐢变簬娌℃湁杩炴帴)鍑虹幇閿欒銆傜嚎绋嬩紒鍥鹃噸鏂拌繛鎺ュ墠灏嗙潯鐪master-connect-retry绉掋

         Reconnecting after a failed master event read

绾跨▼姝e皾璇曢噸鏂拌繛鎺ヤ富鏈嶅姟鍣ㄣ傚綋杩炴帴閲嶆柊寤虹珛鍚庯紝鐘舵佸彉涓Waiting for master to send event

         Waiting for the slave SQL thread to free enough relay log space

姝d娇鐢ㄤ竴涓潪闆relay_log_space_limit鍊硷紝涓户鏃ュ織宸茬粡澧為暱鍒板叾缁勫悎澶у皬瓒呰繃璇ュ笺I/O绾跨▼姝g瓑寰呯洿鍒SQL绾跨▼澶勭悊涓户鏃ュ織鍐呭骞跺垹闄ら儴鍒嗕腑缁ф棩蹇楁枃浠舵潵閲婃斁瓒冲鐨勭┖闂淬

         Waiting for slave mutex on exit

绾跨▼鍋滄鏃跺彂鐢熺殑涓涓緢绠鍗曠殑鐘舵併

6.3.3. 澶嶅埗浠嶴QL绾跨▼鐘舵

涓嬮潰鍒楀嚭浜嗕粠鏈嶅姟鍣ㄧ殑SQL绾跨▼鐨State鍒楃殑鏈甯歌鐨勭姸鎬併

         Reading event from the relay log

绾跨▼宸茬粡浠庝腑缁ф棩蹇楄鍙栦竴涓簨浠讹紝鍙互瀵逛簨浠惰繘琛屽鐞嗕簡銆

         Has read all relay log; waiting for the slave I/O thread to update it

绾跨▼宸茬粡澶勭悊浜嗕腑缁ф棩蹇楁枃浠朵腑鐨勬墍鏈変簨浠讹紝鐜板湪姝g瓑寰I/O绾跨▼灏嗘柊浜嬩欢鍐欏叆涓户鏃ュ織銆

         Waiting for slave mutex on exit

绾跨▼鍋滄鏃跺彂鐢熺殑涓涓緢绠鍗曠殑鐘舵併

I/O绾跨▼鐨State鍒椾篃鍙互鏄剧ず璇彞鐨勬枃鏈傝繖璇存槑绾跨▼宸茬粡浠庝腑缁ф棩蹇楄鍙栦簡涓涓簨浠讹紝浠庝腑鎻愬彇浜嗚鍙ワ紝骞朵笖姝e湪鎵ц璇彞銆

6.3.4. 澶嶅埗浼犻掑拰鐘舵佹枃浠

榛樿鎯呭喌锛屼腑缁ф棩蹇椾娇鐢host_name-relay-bin.nnnnnn褰㈠紡鐨勬枃浠跺悕锛屽叾涓host_name鏄粠鏈嶅姟鍣ㄤ富鏈哄悕锛nnnnnn鏄簭鍒楀彿銆傜敤杩炵画搴忓垪鍙锋潵鍒涘缓杩炵画涓户鏃ュ織鏂囦欢锛屼粠000001寮濮浠庢湇鍔″櫒璺熻釜绱㈠紩鏂囦欢涓洰鍓嶆浣跨敤鐨勪腑缁ф棩蹇椼 榛樿涓户鏃ュ織绱㈠紩鏂囦欢鍚嶄负host_name-relay-bin.index銆傞粯璁ゆ儏鍐碉紝鍦ㄤ粠鏈嶅姟鍣ㄧ殑鏁版嵁鐩綍涓垱寤鸿繖浜涙枃浠躲傚彲浠ョ敤--relay-log--relay-log-index鏈嶅姟鍣ㄩ夐」瑕嗙洊 榛樿鏂囦欢鍚嶃傚弬瑙6.8鑺傦紝鈥滃鍒跺惎鍔ㄩ夐」鈥

涓户鏃ュ織涓庝簩杩涘埗鏃ュ織鐨勬牸寮忕浉鍚岋紝骞朵笖鍙互鐢mysqlbinlog璇诲彇銆SQL绾跨▼鎵ц瀹屼腑缁ф棩蹇椾腑鐨勬墍鏈変簨浠跺苟涓斾笉鍐嶉渶瑕佷箣鍚庯紝绔嬪嵆鑷姩鍒犻櫎瀹冦傛病鏈夌洿鎺ョ殑鍒犻櫎涓户鏃ュ織鐨勬満鍒讹紝鍥犱负SQL绾跨▼鍙互璐熻矗瀹屾垚銆傜劧鑰岋紝FLUSH LOGS鍙互寰幆涓户鏃ュ織锛屽綋SQL绾跨▼鍒犻櫎鏃ュ織鏃朵細鏈夊奖鍝嶃

鍦ㄤ笅闈㈢殑鏉′欢涓嬪垱寤烘柊鐨勪腑缁ф棩蹇楋細

         姣忔I/O绾跨▼鍚姩鏃跺垱寤轰竴涓柊鐨勪腑缁ф棩蹇椼

         褰撴棩蹇楄鍒锋柊鏃讹紱渚嬪锛岀敤FLUSH LOGSmysqladmin flush-logs

         褰撳綋鍓嶇殑涓户鏃ュ織鏂囦欢鍙樺緱澶ぇ鏃躲傗澶ぇ鈥濆惈涔夌殑纭畾鏂规硶锛

o        max_relay_log_size锛屽鏋max_relay_log_size > 0

o        max_binlog_size锛屽鏋max_relay_log_size = 0

浠庡睘澶嶅埗鏈嶅姟鍣ㄥ湪鏁版嵁鐩綍涓彟澶栧垱寤轰袱涓皬鏂囦欢銆傝繖浜鐘舵佹枃浠榛樿鍚嶄负涓master.inforelay-log.info銆傚畠浠寘鍚SHOW SLAVE STATUS璇彞鐨勮緭鍑烘墍鏄剧ず鐨勪俊鎭(鍏充簬璇ヨ鍙ョ殑鎻忚堪鍙傝13.6.2鑺傦紝鈥滅敤浜庢帶鍒朵粠鏈嶅姟鍣ㄧ殑SQL璇彞鈥)銆傜姸鎬佹枃浠朵繚瀛樺湪纭洏涓婏紝浠庢湇鍔″櫒鍏抽棴鏃朵笉浼氫涪澶便備笅娆′粠鏈嶅姟鍣ㄥ惎鍔ㄦ椂锛岃鍙栬繖浜涙枃浠朵互纭畾瀹冨凡缁忎粠涓绘湇鍔″櫒璇诲彇浜嗗灏戜簩杩涘埗鏃ュ織锛屼互鍙婂鐞嗚嚜宸辩殑涓户鏃ュ織鐨勭▼搴︺

I/O绾跨▼鏇存柊master.info鏂囦欢銆傛枃浠朵腑鐨勮鍜SHOW SLAVE STATUS鏄剧ず鐨勫垪鐨勫搴斿叧绯讳负锛

鎻忚堪

1

鏂囦欢涓殑琛屽彿

2

Master_Log_File

3

Read_Master_Log_Pos

4

Master_Host

5

Master_User

6

瀵嗙爜(涓嶇敱SHOW SLAVE STATUS鏄剧ず)

7

Master_Port

8

Connect_Retry

9

Master_SSL_Allowed

10

Master_SSL_CA_File

11

Master_SSL_CA_Path

12

Master_SSL_Cert

13

Master_SSL_Cipher

14

Master_SSL_Key

SQL绾跨▼鏇存柊relay-log.info鏂囦欢銆傛枃浠朵腑鐨勮鍜SHOW SLAVE STATUS鏄剧ず鐨勫垪鐨勫搴斿叧绯讳负锛

鎻忚堪

1

Relay_Log_File

2

Relay_Log_Pos

3

Relay_Master_Log_File

4

Exec_Master_Log_Pos

褰撳浠戒粠鏈嶅姟鍣ㄧ殑鏁版嵁鏃讹紝浣犺繕搴斿浠借繖涓や釜灏忔枃浠朵互鍙婁腑缁ф棩蹇楁枃浠躲傚畠浠敤鏉ュ湪鎭㈠浠庢湇鍔″櫒鐨勬暟鎹悗缁х画杩涜澶嶅埗銆傚鏋滀涪澶变簡涓户鏃ュ織浣嗕粛鐒舵湁relay-log.info鏂囦欢锛屼綘鍙互閫氳繃妫鏌ヨ鏂囦欢鏉ョ‘瀹SQL绾跨▼宸茬粡鎵ц鐨勪富鏈嶅姟鍣ㄤ腑浜岃繘鍒舵棩蹇楃殑绋嬪害銆傜劧鍚庡彲浠ョ敤Master_Log_FileMaster_LOG_POS閫夐」鎵цCHANGE MASTER TO鏉ュ憡璇変粠鏈嶅姟鍣ㄩ噸鏂颁粠璇ョ偣璇诲彇浜岃繘鍒舵棩蹇椼傚綋鐒讹紝瑕佹眰浜岃繘鍒舵棩蹇椾粛鐒跺湪涓绘湇鍔″櫒涓娿

濡傛灉浠庢湇鍔″櫒姝e鍒LOAD DATA INFILE璇彞锛屼綘搴斾篃澶囦唤璇ョ洰褰曞唴浠庢湇鍔″櫒鐢ㄤ簬璇ョ洰鐨勭殑浠讳綍SQL_LOAD-*鏂囦欢銆備粠鏈嶅姟鍣ㄩ渶瑕佽繖浜涙枃浠剁户缁鍒朵换浣曚腑鏂殑LOAD DATA INFILE鎿嶄綔銆傜敤--slave-load-tmpdir閫夐」鏉ユ寚瀹氱洰褰曠殑浣嶇疆銆傚鏋滄湭鎸囧畾锛 榛樿鍊间负tmpdir鍙橀噺鐨勫笺

6.4. 濡備綍璁剧疆澶嶅埗

杩欓噷绠鍗曟弿杩颁簡濡備綍涓轰綘褰撳墠鐨MySQL鏈嶅姟鍣ㄨ缃畬鏁寸殑澶嶅埗銆傚亣璁句綘鎯宠澶嶅埗涓绘湇鍔″櫒涓婄殑鎵鏈夋暟鎹簱锛屽苟涓旇繕娌℃湁閰嶇疆鐨勫鍒躲備綘闇瑕佸叧闂富鏈嶅姟鍣ㄦ潵瀹屾垚涓嬮潰鎵鍒楃殑姝ラ銆

涓嬮潰鐨勭▼搴忛拡瀵硅缃竴涓粠鏈嶅姟鍣紝浣犲彲浠ョ敤鏉ヨ缃涓粠鏈嶅姟鍣ㄣ

铏界劧璇ユ柟娉曟槸璁剧疆浠庢湇鍔″櫒鐨勬渶鐩存帴鐨勯斿緞锛屽畠骞朵笉鏄敮涓鐨勪竴涓備緥濡傦紝濡傛灉浣犳湁涓涓富鏈嶅姟鍣ㄧ殑鏁版嵁蹇収锛屽苟涓斾富鏈嶅姟鍣ㄥ凡缁忚缃簡鏈嶅姟鍣ID锛屽惎鐢ㄤ簡浜岃繘鍒舵棩蹇楋紝涓嶉渶瑕佸叧闂富鏈嶅姟鍣ㄦ垨鍋滄瀵瑰畠鐨勬洿鏂颁篃鍙互璁剧疆浠庢湇鍔″櫒銆傝鎯呰鍙傝6.9鑺傦紝鈥滃鍒禙AQ鈥

濡傛灉鎯宠绠$悊MySQL澶嶅埗璁剧疆锛屾垜浠缓璁綘閫氳鏈珷锛屽苟灏濊瘯13.6.1鑺傦紝鈥滅敤浜庢帶鍒朵富鏈嶅姟鍣ㄧ殑SQL璇彞鈥13.6.2鑺傦紝鈥滅敤浜庢帶鍒朵粠鏈嶅姟鍣ㄧ殑SQL璇彞鈥涓殑鎵鏈夎鍙ャ傝繕搴旂啛鎮6.8鑺傦紝鈥滃鍒跺惎鍔ㄩ夐」鈥涓弿杩扮殑澶嶅埗鍚姩閫夐」銆

娉ㄩ噴锛璇ョ▼搴忓拰鍚庨潰绔犺妭鎵绀虹殑澶嶅埗SQL璇彞闇瑕SUPER鏉冮檺銆

1.    纭繚鍦ㄦ湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂瀹夎鐨MySQL鐗堟湰涓6.5鑺傦紝鈥滀笉鍚孧ySQL鐗堟湰涔嬮棿鐨勫鍒跺吋瀹规р鎵绀虹殑琛ㄥ吋瀹广傜悊鎯虫儏鍐碉紝搴斿湪涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂浣跨敤鏈杩戠増鏈殑MySQL

璇峰厛璇佸疄闂涓嶆槸鍑虹幇鍦ㄦ渶鏂扮殑MySQL鐗堟湰涓啀閫氭姤bug

2.    鍦ㄤ富鏈嶅姟鍣ㄤ笂涓烘湇鍔″櫒璁剧疆涓涓繛鎺ヨ处鎴枫傝璐︽埛蹇呴』鎺堜簣REPLICATION SLAVE鏉冮檺銆傚鏋滆处鎴蜂粎鐢ㄤ簬澶嶅埗(鎺ㄨ崘杩欐牱鍋)锛屽垯涓嶉渶瑕佸啀鎺堜簣浠讳綍鍏跺畠鏉冮檺銆(鍏充簬璁剧疆鐢ㄦ埛 璐︽埛鍜屾潈闄愮殑淇℃伅锛屽弬瑙5.8鑺傦紝鈥淢ySQL鐢ㄦ埛璐︽埛绠$悊鈥

鍋囧畾浣犵殑鍩熶负mydomain.com,鎯宠鍒涘缓鐢ㄦ埛鍚嶄负repl鐨勪竴涓处鎴凤紝浠庢湇鍔″櫒鍙互浣跨敤璇ヨ处鎴蜂粠浣犵殑鍩熷唴鐨勪换浣曚富鏈轰娇鐢ㄥ瘑鐮slavepass鏉ヨ闂富鏈嶅姟鍣ㄣ傝鍒涘缓璇 璐︽埛锛屽彲浣跨敤GRANT璇彞锛

mysql> GRANT REPLICATION SLAVE ON *.*
    -> TO 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';

濡傛灉浣犺鍒掍粠浠庡睘鏈嶅姟鍣ㄤ富鏈轰娇鐢LOAD TABLE FROM MASTERLOAD DATA FROM MASTER璇彞锛屼綘闇瑕佹巿浜堣璐︽埛鍏跺畠鏉冮檺锛

         鎺堜簣璐︽埛SUPERRELOAD鍏ㄥ眬鏉冮檺銆

         涓烘墍鏈夋兂瑕佽杞界殑琛ㄦ巿浜SELECT鏉冮檺銆備换浣曡 璐︽埛涓嶈兘SELECT鐨勪富鏈嶅姟鍣ㄤ笂鐨勮〃琚LOAD DATA FROM MASTER蹇界暐鎺夈

3.    鎵цFLUSH TABLES WITH READ LOCK璇彞娓呯┖鎵鏈夎〃鍜屽潡鍐欏叆璇彞锛

4.            mysql> FLUSH TABLES WITH READ LOCK

瀵逛簬InnoDB琛紝璇锋敞鎰忥細FLUSH TABLES WITH READ LOCK杩橀攣瀹COMMIT鎿嶄綔銆傚綋鑾峰緱鍏ㄥ眬璇婚攣瀹氬悗锛屽彲浠ュ紑濮InnoDB琛ㄧ殑鏂囦欢绯荤粺蹇収銆傚揩鐓т笉鑳戒繚璇佸唴閮(InnoDB瀛樺偍寮曟搸鍐呴儴)涓鑷存(鍥犱负InnoDB缂撳瓨娌℃湁鍒锋柊)锛屼絾骞朵笉闇瑕佸叧蹇冭闂锛屽洜涓InnoDB鍙互鍦ㄥ惎鍔ㄦ椂瑙e喅璇ラ棶棰樺苟缁欏嚭涓鑷寸殑缁撴灉銆傝繖璇存槑InnoDB鍦ㄥ惎鍔ㄥ揩鐓ф椂鍙互杩涜宕╂簝鎭㈠锛岃屼笉浼氱牬鍧忋傜劧鑰岋紝褰撲繚璇佷竴鑷寸殑InnoDB琛ㄥ揩鐓ф椂锛岃繕娌℃湁閫斿緞鏉ュ仠姝MySQL鏈嶅姟鍣ㄣ

璁╁鎴风▼搴忎繚鎸佽繍琛岋紝鍙戝嚭FLUSH TABLES璇彞璁╄閿佸畾淇濇寔鏈夋晥銆(濡傛灉閫鍑哄鎴风▼搴忥紝閿佽閲婃斁锛鐒跺悗瀵逛富鏈嶅姟鍣ㄤ笂鐨勬暟鎹繘琛屽揩鐓с

鍒涘缓蹇収鏈绠鍗曠殑閫斿緞鏄娇鐢ㄥ綊妗g▼搴忓涓绘湇鍔″櫒涓婄殑鏁版嵁鐩綍涓殑鏁版嵁搴撹繘琛屼簩杩涘埗澶囦唤銆備緥濡傦紝鍦Unix涓娇鐢tar锛屾垨鑰呭湪Windows涓娇鐢PowerArchiverWinRARWinZip鎴栬呯被浼肩殑杞欢銆傝浣跨敤tar鏉ュ垱寤哄寘鎷墍鏈夋暟鎹簱鐨勫綊妗f枃浠讹紝杩涘叆涓绘湇鍔″櫒鐨勬暟鎹洰褰曪紝鐒跺悗鎵ц鍛戒护锛

shell> tar -cvf /tmp/mysql-snapshot.tar .

濡傛灉浣犳兂璁╁綊妗e彧鍖呮嫭this_db鏁版嵁搴擄紝搴斾娇鐢ㄥ懡浠わ細

shell> tar -cvf /tmp/mysql-snapshot.tar ./this_db

鐒跺悗灏嗗綊妗f枃浠跺鍒跺埌浠庢湇鍔″櫒涓绘満鐨/tmp鐩綍銆傚湪璇ユ満鍣ㄤ笂锛岃繘鍏ヤ粠鏈嶅姟鍣ㄧ殑鏁版嵁鐩綍锛屽苟浣跨敤涓嬭堪鍛戒护瑙e帇缂╁綊妗f枃浠讹細

shell> tar -xvf /tmp/mysql-snapshot.tar

濡傛灉浠庢湇鍔″櫒鐨勭敤鎴疯处鎴蜂笌涓绘湇鍔″櫒鐨勪笉鍚岋紝浣犲彲鑳戒笉鎯冲鍒mysql鏁版嵁搴撱傚湪杩欑鎯呭喌涓嬶紝搴斾粠褰掓。涓帓闄よ鏁版嵁搴撱備綘涔熶笉闇瑕佸湪褰掓。涓寘鎷换浣曟棩蹇楁枃浠舵垨鑰master.inforelay-log.info鏂囦欢銆

FLUSH TABLES WITH READ LOCK鎵缃閿佸畾鏈夋晥鏃讹紝璇诲彇涓绘湇鍔″櫒涓婂綋鍓嶇殑浜岃繘鍒舵棩蹇楀悕鍜屽亸绉婚噺鍊硷細

mysql > SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73       | test         | manual,mysql     |
+---------------+----------+--------------+------------------+

File鍒楁樉绀烘棩蹇楀悕锛岃Position鏄剧ず鍋忕Щ閲忋傚湪璇ヤ緥瀛愪腑锛屼簩杩涘埗鏃ュ織鍊间负mysql-bin.003锛屽亸绉婚噺涓73銆傝褰曡鍊笺備互鍚庤缃粠鏈嶅姟鍣ㄦ椂闇瑕佷娇鐢ㄨ繖浜涘笺傚畠浠〃绀哄鍒跺潗鏍囷紝浠庢湇鍔″櫒搴斾粠璇ョ偣寮濮嬩粠涓绘湇鍔″櫒涓婅繘琛屾柊鐨勬洿鏂般

鍙栧緱蹇収骞惰褰曟棩蹇楀悕鍜屽亸绉婚噺鍚庯紝鍙互鍦ㄤ富鏈嶅姟鍣ㄤ笂閲嶆柊鍚敤鍐欐椿鍔細

mysql> UNLOCK TABLES

濡傛灉浣犳浣跨敤InnoDB琛紝鐞嗘兂鎯呭喌搴斾娇鐢InnoDB Hot Backup宸ュ叿锛屼娇鐢ㄨ宸ュ叿鍙互鑾峰緱涓鑷寸殑蹇収鑰屼笉闇瑕佸湪涓绘湇鍔″櫒涓婅繘琛岄攣瀹氾紝骞朵笖鍙互瀵瑰簲浠庢湇鍔″櫒涓婁娇鐢ㄧ殑蹇収鏉ヨ褰曟棩蹇楀悕鍜屽亸绉婚噺銆Hot Backup鏄竴涓檮鍔犵殑闈炲厤璐(鍟嗕笟)宸ュ叿锛屾病鏈夊寘鍚湪鏍囧噯 MySQL鍒嗗彂涓傝缁嗕俊鎭弬瑙http://www.innodb.com/manual.phpInnoDB Hot Backup涓婚〉銆

涓嶄娇鐢Hot Backup宸ュ叿锛屾渶蹇嵎鐨勯斿緞鏄娇鐢InnoDB琛ㄧ殑浜岃繘鍒跺揩鐓ф潵鍏抽棴涓绘湇鍔″櫒骞跺鍒InnoDB鏁版嵁鏂囦欢銆佹棩蹇楁枃浠跺拰琛ㄥ畾涔夋枃浠(.frm鏂囦欢)銆傝璁板綍褰撳墠鐨勬棩蹇楁枃浠跺悕鍜屽亸绉婚噺锛屽叧闂湇鍔″櫒涔嬪墠搴斿彂鍑轰笅闈㈢殑璇彞锛

mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;

鐒跺悗璁板綍鍓嶉潰鎵绀虹殑SHOW MASTER STATUS鐨勮緭鍑轰腑鏄剧ず鐨勬棩蹇楀悕鍜屽亸绉婚噺銆傝褰曟棩蹇楀悕鍜屽亸绉婚噺鍚庯紝瑙i攣琛ㄥ叧闂湇鍔″櫒浠ョ‘淇  鏈嶅姟鍣ㄥ叧闂椂鐨勫揩鐓т笌褰撳墠鐨勬棩蹇楁枃浠跺拰鍋忕Щ閲忕浉瀵瑰簲锛

shell> mysqladmin -u root shutdown

閫傚悎MyISAMInnoDB琛ㄧ殑鍙︿竴涓柟娉曟槸瀵逛富鏈嶅姟鍣ㄤ笂鐨SQL杩涜杞偍鑰屼笉鏄鍓嶉潰璁ㄨ鐨勪簩杩涘埗澶嶅埗杩涜杞偍銆備负浜嗗疄鐜帮紝鍙互鍦ㄤ富鏈嶅姟鍣ㄤ笂浣跨敤mysqldump --master-data锛屼互鍚庡皢SQL杞偍鏂囦欢瑁呭叆浠庢湇鍔″櫒銆備絾鏄紝杩欐牱姣斾簩杩涘埗澶嶅埗瑕佹參涓浜涖

濡傛灉涓绘湇鍔″櫒杩愯鏃舵病鏈夊惎鐢--logs-binSHOW MASTER STATUSmysqldump --master-data鏄剧ず鐨勬棩蹇楀悕鍜屼綅缃间负绌恒傚湪杩欑鎯呭喌涓嬶紝褰撲互鍚庢寚瀹氫粠鏈嶅姟鍣ㄧ殑鏃ュ織鏂囦欢鍜屼綅缃椂闇瑕佷娇鐢ㄧ殑鍊间负绌哄瓧绗︿覆('')4.

5.    纭繚涓绘湇鍔″櫒涓绘満涓my.cnf鏂囦欢鐨[mysqld]閮ㄥ垎鍖呮嫭涓涓log-bin閫夐」銆傝閮ㄥ垎杩樺簲鏈変竴涓server-id=Master_id閫夐」锛屽叾涓master_id蹇呴』涓12321涔嬮棿鐨勪竴涓鏁存暟鍊笺備緥濡傦細

6.            [mysqld]
7.            log-bin=mysql-bin
8.            server-id=1

濡傛灉娌℃湁鎻愪緵閭d簺閫夐」锛屽簲娣诲姞瀹冧滑骞堕噸鍚湇鍔″櫒銆

9.    鍋滄鐢ㄤ簬浠庢湇鍔″櫒鐨勬湇鍔″櫒骞跺湪鍏my.cnf鏂囦欢涓坊鍔犱笅闈㈢殑琛岋細

10.        [mysqld]
11.        server-id=slave_id

slave_id鍊煎悓Master_id鍊间竴鏍凤紝蹇呴』涓12321涔嬮棿鐨勪竴涓鏁存暟鍊笺傚苟涓旓紝浠庢湇鍔″櫒鐨ID蹇呴』涓庝富鏈嶅姟鍣ㄧ殑ID涓嶇浉鍚屻備緥濡傦細

[mysqld]
server-id=2

濡傛灉璁剧疆澶氫釜浠庢湇鍔″櫒锛屾瘡涓粠鏈嶅姟鍣ㄥ繀椤绘湁涓涓敮涓鐨server-id鍊硷紝蹇呴』涓庝富鏈嶅姟鍣ㄧ殑浠ュ強鍏跺畠浠庢湇鍔″櫒鐨勪笉鐩稿悓銆傚彲浠ヨ涓server-id鍊肩被浼间簬IP鍦板潃锛氳繖浜ID鍊艰兘鍞竴璇嗗埆澶嶅埗鏈嶅姟鍣ㄧ兢闆嗕腑鐨勬瘡涓湇鍔″櫒瀹炰緥銆

濡傛灉涓嶆寚瀹氫竴涓server-id鍊硷紝濡傛灉娌℃湁瀹氫箟master-host锛屽垯灏嗗畠璁剧疆涓1锛涘惁鍒欒缃负2銆傝娉ㄦ剰濡傛灉server-id澶暱锛屼富鏈嶅姟鍣 鎷掔粷鎵鏈夋潵鑷粠鏈嶅姟鍣ㄧ殑杩炴帴锛屽苟涓斾粠鏈嶅姟鍣ㄦ嫆缁濊繛鎺ュ埌涓绘湇鍔″櫒銆傝繖鏍凤紝鐪佺暐server-id鍙傚悎鐢ㄤ簩杩涘埗鏃ュ織澶囦唤銆

12.濡傛灉瀵逛富鏈嶅姟鍣ㄧ殑鏁版嵁杩涜浜岃繘鍒跺浠斤紝鍚姩浠庢湇鍔″櫒涔嬪墠灏嗗畠澶嶅埗鍒颁粠鏈嶅姟鍣ㄧ殑鏁版嵁鐩綍涓傜‘淇濆杩欎簺鏂囦欢鍜岀洰褰曠殑鏉冮檺姝g‘銆傛湇鍔″櫒 MySQL杩愯鐨勭敤鎴峰繀椤昏兘澶熻鍐欐枃浠讹紝濡傚悓鍦ㄤ富鏈嶅姟鍣ㄤ笂涓鏍枫

濡傛灉浣跨敤mysqldum澶囦唤锛屽厛鍚姩浠庢湇鍔″櫒(鐪嬩笅涓姝)

13.鍚姩浠庢湇鍔″櫒銆傚鏋滃墠闈㈠凡缁忓鍒朵簡锛岀敤--skip-slave-start閫夐」鍚姩浠庢湇鍔″櫒锛屼互渚垮畠涓嶇珛鍗冲皾璇曡繛鎺ヤ富鏈嶅姟鍣ㄣ備綘涔熷彲鑳芥兂瑕佺敤--logs-warnings閫夐」鍚姩浠庢湇鍔″櫒(榛樿璁剧疆鍚敤)锛屼互渚垮湪閿欒鏃ュ織涓樉绀烘洿澶氱殑闂鐩稿叧鐨勪俊鎭(渚嬪锛岀綉缁滄垨杩炴帴闂)銆傛斁寮冪殑杩炴帴灏嗚鍏ラ敊璇棩蹇楋紝闄ら潪鍏跺煎ぇ浜1

14.濡傛灉浣跨敤mysqldump澶囦唤涓绘湇鍔″櫒鐨勬暟鎹紝灏嗚浆鍌ㄦ枃浠惰杞藉埌浠庢湇鍔″櫒锛

15.        shell> mysql -u root -p < dump_file.sql
16.        鍦ㄤ粠鏈嶅姟鍣ㄤ笂鎵ц涓嬮潰鐨勮鍙ワ紝鐢ㄤ綘鐨勭郴缁熺殑瀹為檯鍊兼浛鎹㈤夐」鍊硷細
17.        mysql> CHANGE MASTER TO
18.            ->     MASTER_HOST='master_host_name',
19.            ->     MASTER_USER='replication_user_name',
20.            ->     MASTER_PASSWORD='replication_password',
21.            ->     MASTER_LOG_FILE='recorded_log_file_name',
22.            ->     MASTER_LOG_POS=recorded_log_position;

涓嬮潰鐨勮〃鏄剧ず浜嗗瓧绗︿覆閫夐」鐨勬渶澶ч暱搴︼細

Master_Host

60

Master_USER

16

Master_PASSWORD

32

Master_Log_File

255

23.鍚姩浠庢湇鍔″櫒绾跨▼锛

24.        mysql> START SLAVE

鎵ц杩欎簺绋嬪簭鍚庯紝浠庢湇鍔″櫒搴旇繛鎺ヤ富鏈嶅姟鍣紝骞惰ˉ鍏呰嚜浠庡揩鐓т互鏉ュ彂鐢熺殑浠讳綍鏇存柊銆

濡傛灉浣犲繕璁拌缃富鏈嶅姟鍣ㄧ殑server-id鍊硷紝浠庢湇鍔″櫒涓嶈兘杩炴帴涓绘湇鍔″櫒銆

濡傛灉浣犲繕璁拌缃粠鏈嶅姟鍣ㄧ殑server-id鍊硷紝鍦ㄤ粠鏈嶅姟鍣ㄧ殑閿欒鏃ュ織涓細鍑虹幇涓嬮潰鐨勯敊璇細

Warning: You should set server-id to a non-0 value if master_host is set;
we will force server id to 2, but this MySQL server will not act as a slave.

濡傛灉鐢变簬鍏跺畠鍘熷洜涓嶈兘澶嶅埗锛屼粠鏈嶅姟鍣ㄧ殑閿欒鏃ュ織涓篃浼氬嚭鐜伴敊璇秷鎭

浠庢湇鍔″櫒澶嶅埗鏃讹紝浼氬湪鍏舵暟鎹洰褰曚腑鍙戠幇鏂囦欢dmaster.inforelay-log.info銆備粠鏈嶅姟鍣ㄤ娇鐢ㄨ繖涓や釜鏂囦欢璺熻釜宸茬粡澶勭悊浜嗗灏戜富鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇椼備笉瑕佺Щ闄ゆ垨缂栬緫杩欎簺鏂囦欢锛岄櫎闈炰綘纭垏鐭ヤ綘姝e湪鍋氫粈涔堝苟瀹屽叏鐞嗚В鍏舵剰涔夈傚嵆浣胯繖鏍凤紝鏈濂芥槸浣跨敤CHANGE MASTER TO璇彞銆

娉ㄩ噴锛master.info鍐呭浼氳鐩栧懡浠よ鎴in my.cnf涓寚瀹氱殑閮ㄥ垎閫夐」銆傝鎯呭弬瑙6.8鑺傦紝鈥滃鍒跺惎鍔ㄩ夐」鈥

鏈変簡涓涓揩鐓э紝浣犲彲浠ョ敤瀹冩牴鎹垰鍒氭弿杩扮殑浠庢湇鍔″櫒閮ㄥ垎鏉ヨ缃叾瀹冧粠鏈嶅姟鍣ㄣ備綘涓嶉渶瑕佷富鏈嶅姟鍣ㄧ殑鍙︿竴涓揩鐓э紱姣忎釜浠庢湇鍔″櫒鍙互浣跨敤鐩稿悓鐨勫揩鐓с

娉ㄩ噴锛氫负浜嗕繚璇佷簨鍔InnoDB澶嶅埗璁剧疆鐨勬渶澶у彲鑳界殑鑰愬彈鎬у拰涓鑷存э紝搴斿湪涓绘湇鍔″櫒鐨my.cnf鏂囦欢涓娇鐢innodb_flush_log_at_trx_commit=1sync-binlog=1

6.5. 涓嶅悓MySQL鐗堟湰涔嬮棿鐨勫鍒跺吋瀹规

MySQL 5.1涓娇鐢ㄧ殑浜岃繘鍒舵棩蹇楁牸寮忎笌浠ュ墠鐨勭増鏈腑鎵浣跨敤鐨勫ぇ澶т笉鍚岋紝鐗瑰埆鏄湪瀛楃闆嗗鐞嗐LOAD DATA INFILE浠ュ強鏃跺尯鏂归潰銆

娉ㄩ噴锛浣犱笉鑳戒粠浣跨敤鏂颁簩杩涘埗鏃ュ織鏍煎紡鐨勪富鏈嶅姟鍣ㄥ悜浣跨敤鏃т簩杩涘埗鏃ュ織鏍煎紡鐨勪粠鏈嶅姟鍣ㄥ鍒(渚嬪锛屼粠MySQL 5.0MySQL 4.1銆傝繖鏍锋搷浣滃湪澶嶅埗璁剧疆鍗囩骇鏈嶅姟鍣ㄦ椂鍚庢灉涓ラ噸锛屽弬瑙6.6鑺傦紝鈥滃崌绾у鍒惰缃

鎴戜滑鎺ㄨ崘浣跨敤鏈杩戠殑MySQL鐗堟湰锛屽洜涓哄鍒跺姛鑳藉湪涓嶆柇鍦版敼杩涗腑銆傛垜浠繕鎺ㄨ崘涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ娇鐢ㄧ浉鍚岀殑鐗堟湰銆傛垜浠缓璁崌绾т富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒锛岃繍琛alphabeta鐗堟湰鍒版柊鐨(浜у搧)鐗堟湰銆傚湪璁稿鎯呭喌涓嬶紝浠庢柊鐨勪富鏈嶅姟鍣ㄥ悜鏃х殑浠庢湇鍔″櫒澶嶅埗灏嗕細澶辫触銆備竴鑸師鍒欙紝杩愯MySQL 5.1.x鐨勪粠鏈嶅姟鍣ㄥ彲浠ヤ笌鏃х殑涓绘湇鍔″櫒(鍙互杩愯MySQL 3.234.0鎴栬4.1)涓璧蜂娇鐢紝浣嗕笉鑳藉弽杩囨潵銆

鍓嶉潰鐨勪俊鎭傚悎鍗忚绾у鍒跺吋瀹规с傜劧鑰岋紝杩樹細鏈変竴涓害鏉熸潯浠讹紝渚嬪SQL绾у吋瀹规ч棶棰樸備緥濡傦紝 5.1鐗堟湰鐨勪富鏈嶅姟鍣ㄤ笉鑳藉鍒跺埌5.0鐗堟湰鐨勪粠鏈嶅姟鍣紝濡傛灉澶嶅埗璇彞浣跨敤5.1鐗堟湰鐨SQL鐗规ц屼笉鏄5.0鐗堟湰銆傝繖浜涢棶棰樺拰鍏跺畠闂鍧囧湪6.7鑺傦紝鈥滃鍒剁壒鎬у拰宸茬煡闂鈥涓璁恒

6.6. 鍗囩骇澶嶅埗璁剧疆

褰撳湪澶嶅埗璁剧疆涓崌绾ф湇鍔″櫒鏃讹紝鍗囩骇杩囩▼鍙栧喅浜庡綋鍓嶇殑鏈嶅姟鍣ㄧ増鏈拰瑕佸崌绾х殑鏈嶅姟鍣ㄧ増鏈

6.6.1. 灏嗗鍒跺崌绾у埌5.0鐗

璇ヨ妭閫傜敤浜庡皢澶嶅埗浠MySQL 3.234.0鎴栬4.1鍗囩骇鍒5.14.0鏈嶅姟鍣ㄥ簲涓4.0.3鎴栨洿鏂扮増銆

褰撳皢鏃╂湡MySQL鐗堟湰绯诲垪涓绘湇鍔″櫒鍗囩骇鍒5.1鏃讹紝搴斿厛纭繚璇ヤ富鏈嶅姟鍣ㄧ殑鎵鏈変粠鏈嶅姟鍣ㄤ娇鐢ㄤ簡鐩稿悓鐨5.1.x鐗堟湰銆傚鏋滀笉鏄繖鏍凤紝浣犲簲鍏堝崌绾т粠鏈嶅姟鍣ㄣ傚崌绾т粠鏈嶅姟鍣ㄦ椂锛屽簲鍏堝叧闂粠鏈嶅姟鍣紝鍗囩骇鍒扮浉搴5.1.x鐗堟湰锛岀劧鍚庨噸鍚粠鏈嶅姟鍣ㄥ苟閲嶆柊寮濮嬪鍒躲5.1鐗堟湰鐨勪粠鏈嶅姟鍣ㄨ兘澶熻鍙栧崌绾у墠鍐欏叆鐨勬棫鐨勪腑缁ф棩蹇楀苟鎵ц鏃ュ織涓寘鍚殑璇彞銆傚崌绾у悗浠庢湇鍔″櫒鍒涘缓鐨勪腑缁ф棩蹇椾负5.1鏍煎紡銆

浠庢湇鍔″櫒鍗囩骇鍚庯紝鍏抽棴涓绘湇鍔″櫒锛屽皢瀹冨崌绾у埌涓庝粠鏈嶅姟鍣ㄧ浉鍚岀殑5.1.x鐗堟湰骞堕噸鍚畠銆5.1涓绘湇鍔″櫒鑳藉璇诲彇鍗囩骇鍓嶅啓鍏ョ殑鏃х殑浜岃繘鍒舵棩蹇楀苟灏嗗畠浠彂閫佸埌5.1浠庢湇鍔″櫒銆備粠鏈嶅姟鍣ㄥ彲浠ヨ瘑鍒棫鐨勬牸寮忓苟姝g‘澶勭悊瀹冦傚崌绾у悗涓绘湇鍔″櫒鍒涘缓鐨勪簩杩涘埗鏃ュ織閲囩敤5.1鏍煎紡銆傝繖鏍蜂篃鍙互鐢5.1浠庢湇鍔″櫒璇嗗埆銆

鎹㈠彞璇濊锛屽綋鍗囩骇鍒5.1鏃舵病鏈変粈涔堟帾鏂斤紝鍙湁灏嗕富鏈嶅姟鍣ㄥ崌绾у埌5.1涔嬪墠鍏堝皢浠庢湇鍔″櫒鍗囩骇鍒5.1銆傝娉ㄦ剰浠5.1闄嶇骇鍒版棫鐗堟湰涓嶄細濡傛绠鍗曪細蹇呴』纭繚宸茬粡瀹屽叏澶勭悊鎵鏈5.1鐗堟湰鐨勪簩杩涘埗鏃ュ織鎴栦腑缁ф棩蹇楋紝浠ヤ究鍦ㄩ檷绾у墠鍙互绉婚櫎瀹冧滑銆

6.7. 澶嶅埗鐗规у拰宸茬煡闂

涓鑸師鍒欙紝SQL绾у鍒跺吋瀹规ц姹備富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒鍧囨敮鎸佷娇鐢ㄧ殑鐗规с備緥濡傦紝鍦MySQL 5.0.0涓紑濮嬩娇鐢TIMESTAMPADD()鍑芥暟銆傚鏋滃湪涓绘湇鍔″櫒涓婁娇鐢ㄨ鍑芥暟锛屼笉鑳藉鍒跺埌MySQL 5.0.0涔嬪墠鐨勪粠鏈嶅姟鍣ㄣ傚鏋滀綘璁″垝鍦5.1鍜屼互鍓嶇増鏈殑MySQL涔嬮棿杩涜澶嶅埗锛屼綘搴旀煡闃呭搴斾互鍓嶇増鏈郴鍒楃殑MySQL鍙傝冩墜鍐岋紝鏌ヨ璇ョ郴鍒楀鍒剁壒寰佺浉鍏充俊鎭

涓嬮潰鍒楀嚭浜嗗叧浜庢敮鎸佷粈涔堝拰涓嶆敮鎸佷粈涔堢殑璇︾粏淇℃伅銆傚叧浜庡鍒剁殑鍏跺畠InnoDB鍏蜂綋淇℃伅鍙傝15.2.6.5鑺傦紝鈥淚nnoDB鍜孧ySQL澶嶅埗鈥

鍏充簬淇濆瓨鐨勭▼搴忓拰瑙﹀彂鍣ㄧ殑澶嶅埗闂鍦20.4鑺傦紝鈥滃瓨鍌ㄥ瓙绋嬪簭鍜岃Е鍙戠▼搴忕殑浜岃繘鍒舵棩蹇楀姛鑳解涓璁恒

         AUTO_INCREMENTLAST_INSERT_ID()TIMESTAMP鍊兼纭疄鐜板鍒躲

         USER()UUID()LOAD_FILE()鍑芥暟姣棤鏀瑰彉鍦拌锛岃繖鏍蜂笉鑳藉彲闈犲湴鍦ㄤ粠鏈嶅姟鍣ㄤ笂宸ヤ綔銆

         涓嬮潰鐨勯檺鍒跺彧閫傚悎鍩轰簬璇彞鐨勫鍒讹紝鑰屼笉鏄熀浜庤鐨勫鍒躲澶勭悊鐢ㄦ埛绾ч攣瀹氱殑鍑芥暟GET_LOCK()RELEASE_LOCK()IS_FREE_LOCK()IS_USED_LOCK()澶嶅埗鏃朵粠鏈嶅姟鍣ㄤ笉鐭ラ亾鍦ㄤ富鏈嶅姟鍣ㄤ笂鍚屾椂杩涜鐨勭浉鍏虫枃鏈紱鍥犳濡傛灉浠庢湇鍔″櫒涓婄殑鍐呭涓嶅悓锛岃繖浜涘嚱鏁颁笉鐢ㄦ潵鎻掑叆鍒颁富鏈嶅姟鍣ㄧ殑琛ㄤ腑(渚嬪涓嶆墽琛INSERT INTO mytable VALUES(GET_LOCK(...)))

         MySQL 5.1FOREIGN_KEY_CHECKSSQL_MODEUNIQUE_CHECKSSQL_AUTO_IS_NULL鍙橀噺鍧囧鍒躲備絾TABLE_TYPE锛屽嵆STORAGE_ENGINE鍙橀噺 涓嶅鍒讹紝鏈夊埄浜庡湪涓嶅悓鐨勫瓨鍌ㄥ紩鎿庝箣闂磋繘琛屽鍒躲

         鍗充娇涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄦ湁涓嶅悓鐨勫叏灞瀛楃闆嗗彉閲忥紝浠ュ強鍗充娇鏈変笉鍚岀殑鍏ㄥ眬鏃跺尯鍙橀噺浠嶅彲浠ュ鍒躲

         涓嬮潰閫傚悎浣跨敤涓嶅悓瀛楃闆嗙殑MySQL鏈嶅姟鍣ㄤ箣闂寸殑澶嶅埗锛

1.    蹇呴』鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓鎬绘槸浣跨敤鐩稿悓鐨鍏ㄥ眬瀛楃闆嗗拰鏍″瑙勫垯(--default-character-set--default-collation)銆傚惁鍒欙紝浼氬湪浠庢湇鍔″櫒涓婇亣鍒板鍒堕敭鍊奸敊璇紝鍥犱负鍦ㄤ富鏈嶅姟鍣ㄧ殑瀛楃闆嗕腑琚涓烘槸鍞竴鐨勯敭鍊煎湪浠庢湇鍔″櫒鐨勫瓧绗﹂泦涓彲鑳戒笉鏄敮涓鐨勩

2.    濡傛灉涓绘湇鍔″櫒鏃╀簬MySQL 4.1.3锛屽垯浼氳瘽涓殑瀛楃闆嗕笉搴斾笌鍏跺叏灞鍊间笉鍚(鎹㈠彞璇濊锛屼笉瑕佷娇鐢SET NAMESSET CHARACTER SET绛夌瓑)锛屽洜涓轰粠鏈嶅姟鍣ㄤ笉鐭ラ亾璇ュ瓧绗﹂泦鐨勬洿鏀广傚鏋滀富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒鍧囦负4.1.3鎴栨洿鏂扮増锛屽彲浠ラ殢渚垮皢浼氳瘽鐨勫瓧绗﹂泦鍙橀噺璁剧疆涓烘湰鍦板(渚嬪NAMESCHARACTER SETCOLLATION_CLIENTCOLLATION_SERVER)锛屽洜涓鸿繖浜涜瀹氬艰鍐欏叆浜岃繘鍒舵棩蹇楋紝鍥犳浠庢湇鍔″櫒鐭ラ亾銆傜劧鑰岋紝绂佹鏇存敼浼氳瘽涓繖浜涘彉閲忕殑鍏ㄥ眬鍊硷紱濡傚墠闈㈡墍杩帮紝涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄥ繀椤诲叿鏈夊敮涓鐨勫叏灞瀛楃闆嗗笺

3.    濡傛灉鍦ㄤ富鏈嶅姟鍣ㄤ笂鐨勬暟鎹簱鐨勫瓧绗﹂泦涓庡叏灞collation_server鍊间笉鍚岋紝鍒欏簲璁捐CREATE TABLE璇彞锛屼互渚垮畠浠笉闅愬惈渚濊禆鏁版嵁搴撶殑榛樿瀛楃闆(Bug #2326)锛涗竴涓ソ鐨勮В鍐冲姙娉曟槸鍦CREATE TABLE涓槑鏄捐鏄庡瓧绗﹂泦鍜屾牎瀵硅鍒欍

         搴斿湪涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ笂璁剧疆鐩稿悓鐨勭郴缁熸椂鍖恒傚惁鍒欎竴浜涜鍙ワ紝渚嬪浣跨敤NOW()FROM_UNIXTIME()鍑芥暟鐨勮鍙ワ紝灏嗕笉浼氭纭鍒躲傚彲浠ヤ娇鐢ㄨ剼鏈mysqld_safe--timezone=timezone_name閫夐」鎴栭氳繃璁剧疆TZ鐜鍙橀噺璁剧疆MySQL鏈嶅姟鍣ㄨ繍琛岀殑绯荤粺鐨勬椂鍖恒備富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒杩樺簲鏈夌浉鍚岀殑榛樿杩炴帴鏃跺尯璁剧疆锛涘嵆涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄥ簲鏈夌浉鍚岀殑--default-time-zone鍙傛暟鍊笺

         CONVERT_TZ(...,...,@global.time_zone)涓嶈兘姝g‘澶嶅埗銆傚彧鏈変富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒鍧囦负5.0.4鎴栨洿鏂扮増鎵嶈兘姝g‘澶嶅埗CONVERT_TZ(...,...,@session.time_zone)

         浼氳瘽鍙橀噺鍙湁鍦ㄦ洿鏂拌〃鐨勮鍙ヤ腑浣跨敤鏃舵墠鑳芥纭鍒讹紱渚嬪锛SET MAX_JOIN_SIZE=1000INSERT INTO mytable VALUES(@MAX_JOIN_SIZE)涓嶈兘灏嗙浉鍚岀殑鏁版嵁鎻掑叆鍒颁富鏈嶅姟鍣ㄤ笂鍜屼粠鏈嶅姟鍣ㄤ笂銆備笉閫傜敤浜庨氱敤鐨SET TIME_ZONE=...INSERT INTO mytable VALUES(CONVERT_TZ(...,...,@time_zone))

         鍙互灏嗕粠鏈嶅姟鍣ㄤ笂鐨勯潪浜嬪姟琛ㄥ涓轰富鏈嶅姟鍣ㄤ笂鐨勪簨鍔¤〃銆備緥濡傦紝鍙互灏嗕富鏈嶅姟鍣ㄤ笂鐨InnoDB琛ㄥ鍒朵负浠庢湇鍔″櫒涓婄殑MyISAM琛ㄣ傜劧鑰岋紝澶嶅埗杩囩▼涓紝濡傛灉浠庢湇鍔″櫒鍦BEGIN/COMMIT鍧楄繃绋嬩腑鍋滄鍒欎細浜х敓闂锛屽洜涓轰粠鏈嶅姟鍣ㄥ湪BEGIN鍧楀紑濮嬫椂浼氶噸鍚傝闂鍑虹幇鍦TODO涓紝涓嶄箙灏嗕細寰楀埌淇銆

         MySQL 5.1涓彲浠ユ纭鍒跺紩鐢ㄧ敤鎴峰彉閲(@var_name褰㈠紡鐨勫彉閲)鐨勬洿鏂拌鍙ワ紱浣嗗湪4.1浠ュ墠鐨勭増鏈腑鍗翠笉鍙兘銆傝娉ㄦ剰浠MySQL 5.1寮濮嬪鐢ㄦ埛鍙橀噺鍚嶇殑澶у皬鍐欎笉鍐嶆晱鎰燂紱褰撳湪5.1鍜屾棫鐗堟湰涔嬮棿璁剧疆澶嶅埗鏃跺簲鑰冭檻璇ラ棶棰樸

         浠庢湇鍔″櫒鍙互浣跨敤SSL杩炴帴鍒颁富鏈嶅姟鍣ㄣ

         鏈変竴涓叏灞绯荤粺鍙橀噺slave_transaction_retries锛氬鏋滃洜涓烘煇涓InnoDB姝婚攣鎴栬秴杩 InnoDBinnodb_lock_wait_timeoutNDB绨囩殑TransactionDeadlockDetectionTimeoutTransactionInactiveTimeoutREPLICATION SLAVESQL绾跨▼鏈兘鎵ц鏌愪釜浜嬪姟锛屽湪缁欏嚭閿欒鍋滄鍓嶈嚜鍔ㄩ噸璇slave_transaction_retries娆° 榛樿鍊兼槸10銆備粠MySQL 5.0.4寮濮嬶紝鍙互浠SHOW STATUS鐨勮緭鍑轰腑鐪嬪埌閲嶈瘯鎬绘鏁帮紱鍙傝5.3.4鑺傦紝鈥滄湇鍔″櫒鐘舵佸彉閲忊

         濡傛灉鍦ㄤ富鏈嶅姟鍣ㄤ笂鐨CREATE TABLE璇彞涓娇鐢ㄤ簡DATA DIRECTORYINDEX DIRECTORY瀛愬彞锛屽瓙鍙ヤ篃鍙互鍦ㄤ粠鏈嶅姟鍣ㄤ笂浣跨敤銆傚鏋滃湪浠庢湇鍔″櫒涓绘満鏂囦欢绯荤粺涓笉瀛樺湪涓鑷寸殑鐩綍鎴栬櫧鐒跺瓨鍦ㄤ絾涓嶈兘琚粠鏈嶅姟鍣ㄨ闂紝鍒欎細甯︽潵闂銆MySQL 5.1鏀寔涓涓О涓NO_DIR_IN_CREATEsql_mode閫夐」銆傚鏋滀粠鏈嶅姟鍣ㄨ繍琛屾椂灏SQL妯″紡璁剧疆涓哄寘鎷閫夐」锛屽鍒CREATE TABLE璇彞鏃跺皢蹇界暐杩欎簺瀛愬彞銆傜粨鏋滄槸鍦ㄨ〃鐨勬暟鎹簱鐩綍涓垱寤轰簡MyISAM鏁版嵁鍜岀储寮曟枃浠躲

         涓嬮潰鐨勯檺鍒跺彧閫傚悎鍩轰簬璇彞鐨勫鍒讹紝鑰屼笉鏄熀浜庤鐨勫鍒锛氬鏋滃湪鏌ヨ涓暟鎹慨鏀逛笉纭畾锛屼富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓婄殑鏁版嵁鍙互涓嶅悓锛涗篃灏辨槸鐢辨煡璇紭鍖栧櫒纭畾銆(杩欐槸甯哥敤鐨勪絾涓嶆槸寰堝ソ鐨勪範鎯紝鍗充娇涓嶆槸鍦ㄥ鍒朵腑涔熶笉濂斤級鍏充簬璇ラ棶棰樼殑璇︾粏瑙i噴锛屽弬瑙A.8.1鑺傦紝鈥淢ySQL涓殑鎵撳紑浜嬪疁鈥

         READ LOCKFLUSH LOGSFLUSH MASTERFLUSH SLAVEFLUSH TABLES涓嶈鍏ユ棩蹇楋紝鍥犱负濡傛灉澶嶅埗鍒颁粠鏈嶅姟鍣ㄤ細閫犳垚闂銆傚叧浜庤娉曠ず渚嬶紝鍙傝13.5.5.2鑺傦紝鈥淔LUSH璇硶鈥FLUSH TABLESANALYZE TABLEOPTIMIZE TABLEREPAIR TABLE璇彞琚啓鍏ヤ簩杩涘埗鏃ュ織骞朵細澶嶅埗鍒颁粠鏈嶅姟鍣ㄣ備竴鑸儏鍐典笉浼氶犳垚闂锛屽洜涓鸿繖浜涜鍙ヤ笉淇敼琛ㄧ殑鏁版嵁銆備絾鏄湪鏌愪簺鎯呭喌涓嬩細甯︽潵闂銆傚鏋滀綘澶嶅埗mysql鏁版嵁搴撲腑鐨勬巿鏉冭〃骞朵笖涓嶄娇鐢GRANT鐩存帴鏇存柊閭d簺琛紝蹇呴』鍦ㄤ粠鏈嶅姟鍣ㄤ笂鎵цFLUSH PRIVILEGES浣挎柊鐨勬潈闄愮敓鏁堛傚苟涓旓紝濡傛灉浣跨敤FLUSH TABLES閲嶆柊鍛藉悕MERGE琛ㄧ殑MyISAM琛紝蹇呴』鎵嬪姩鍦ㄤ粠鏈嶅姟鍣ㄤ笂鎵цFLUSH TABLES銆傚鏋滀笉鎸囧畾NO_WRITE_TO_BINLOG鎴栧叾鍒悕LOCAL锛屽垯杩欎簺璇彞琚啓鍏ヤ簩杩涘埗鏃ュ織銆

         MySQL鍙敮鎸佷竴涓富鏈嶅姟鍣ㄥ拰澶氫釜浠庢湇鍔″櫒銆傛垜浠鍒掑皢鏉ユ坊鍔犱竴涓姇绁ㄧ畻娉曪紝褰撳墠鐨勪富鏈嶅姟鍣ㄥ嚭鐜伴棶棰樻椂鑷姩鍒囨崲銆傛垜浠繕璁″垝寮曞叆浠g悊杩囩▼閫氳繃鍚戜笉鍚岀殑浠庢湇鍔″櫒鍙戦SELECT鏌ヨ浠ュ府鍔╄繘琛岃礋杞藉潎琛°

         褰撴湇鍔″櫒鍏抽棴銆侀噸鍚椂锛屽叾MEMORY琛ㄥ皢鍙樹负绌恒備富鏈嶅姟鍣ㄦ寜涓嬭堪鏂规硶澶嶅埗璇ョ粨鏋滐細鍚姩鍚庣1娆′富鏈嶅姟鍣ㄤ娇鐢ㄦ瘡涓MEMORY琛紝瀹冮氱煡浠庢湇鍔″櫒闇瑕佸悜琛ㄥ啓鍏DELETE FROM璇彞鏉ユ竻绌轰簩杩涘埗鏃ュ織鐨勮〃銆傝缁嗕俊鎭弬瑙15.4鑺傦紝鈥淢EMORY (HEAP)瀛樺偍寮曟搸鈥

         闄や簡鍏抽棴浠庢湇鍔″櫒(鑰屼笉浠呬粎鏄粠鏈嶅姟鍣ㄧ嚎绋) 涓存椂琛ㄩ兘琚鍒讹紝骞朵笖杩樻病鏈夊湪浠庢湇鍔″櫒涓婃墽琛岀殑鏇存柊鎵浣跨敤鐨勪复鏃惰〃涔熷凡缁忓鍒躲傚鏋滃叧闂粠鏈嶅姟鍣紝浠庢湇鍔″櫒閲嶅惎鍚庢洿鏂伴渶瑕佺殑閭d簺涓存椂琛ㄤ笉鍙啀鐢ㄣ備负浜嗛伩鍏嶈闂锛屼复鏃惰〃鎵撳紑鏃朵笉瑕佸叧闂粠鏈嶅姟鍣ㄣ傝屽簲閬电収涓嬮潰鐨勭▼搴忥細

1.    鎵цSTOP SLAVE璇彞銆

2.    浣跨敤SHOW STATUS妫鏌slave_open_temp_tables鍙橀噺鐨勫笺

3.    濡傛灉鍊间负0锛屼娇鐢mysqladmin shutdown鍛戒护鍏抽棴浠庢湇鍔″櫒銆

4.    濡傛灉鍊间笉涓0锛岀敤START SLAVE閲嶅惎浠庢湇鍔″櫒绾跨▼銆

5.    鍚庨潰鍐嶉噸澶嶈绋嬪簭鐪嬩笅娆$殑杩愭皵鏄惁濂戒竴浜涖

鎴戜滑璁″垝鍦ㄤ笉涔呯殑灏嗘潵淇璇ラ棶棰樸

         鍙互寰堝畨鍏ㄥ湴杩炴帴鐢--logs-slave-updates閫夐」鎸囧畾鐨勫惊鐜富鏈嶅姟鍣/浠庢湇鍔″櫒鍏崇郴涓殑鏈嶅姟鍣ㄣ備絾璇锋敞鎰忚澶氳鍙ュ湪杩欑璁剧疆涓笉鑳芥纭伐浣滐紝闄ら潪浣犵殑瀹㈡埛浠g爜鍏虫敞浜嗘綔鍦ㄧ殑鍦ㄤ笉鍚岀殑鏈嶅姟鍣ㄤ笉鍚岄『搴忕殑鏇存柊涓彲鑳藉彂鐢熺殑杩欑被闂銆

杩欒鏄庝綘鍙互璞¤繖鏍峰垱寤鸿缃細

A -> B -> C -> A

鏈嶅姟鍣ID琚紪鐮佸湪浜岃繘鍒舵棩蹇椾簨浠朵腑锛屽洜姝ゆ湇鍔″櫒A鐭ラ亾浣曟椂鑷繁棣栨鍒涘缓瀹冭鍙栫殑浜嬩欢骞朵笖涓嶆墽琛屼簨浠(闄ら潪鐢--replicate-same-server-id閫夐」鍚姩浜嗘湇鍔″櫒A锛屽彧鍦ㄥ緢灏戞儏鍐典笅鏈夋剰涔)銆傝繖鏍凤紝娌℃湁鏃犻檺寰幆銆傚彧鏈夊琛ㄦ墽琛屾病鏈夊啿绐佺殑鏇存柊鏃惰绫诲惊鐜缃墠鑳藉伐浣溿傛崲鍙ヨ瘽璇达紝濡傛灉鍦AC涓彃鍏ユ暟鎹紝缁濆涓嶅簲鍦A涓彃鍏ラ敭鍊煎彲鑳戒笌鎻掑叆鍒C涓殑琛岀浉鍐茬獊鐨勪竴琛屻傚鏋滄洿鏂扮殑椤哄簭寰堥噸瑕侊紝杩樹笉搴旀洿鏂颁袱涓湇鍔″櫒涓婄浉鍚岀殑琛屻

         濡傛灉浠庢湇鍔″櫒涓婄殑鏌愪釜璇彞浜х敓閿欒锛屽垯浠庢湇鍔″櫒涓婄殑SQL绾跨▼缁堟锛屽苟涓斾粠鏈嶅姟鍣ㄥ悜閿欒鏃ュ織鍐欏叆涓鏉℃秷鎭傛鏃跺簲鎵嬪姩杩炴帴浠庢湇鍔″櫒锛屼慨澶嶈闂(渚嬪锛屼竴涓笉瀛樺湪鐨勮〃)锛岀劧鍚庤繍琛START SLAVE

         鍙互寰堝畨鍏ㄥ湴鍏抽棴涓绘湇鍔″櫒骞跺湪浠ュ悗閲嶅惎銆傚鏋滄煇涓粠鏈嶅姟鍣ㄤ涪澶变笌涓绘湇鍔″櫒鐨勮繛鎺ワ紝浠庢湇鍔″櫒灏濊瘯绔嬪嵆閲嶆柊杩炴帴銆傚鏋滃け璐ワ紝浠庢湇鍔″櫒瀹氭湡閲嶈瘯銆(榛樿璁剧疆鏄瘡60绉掗噸璇曚竴娆°傚彲浠ラ氳繃--master-connect-retry閫夐」鏇存敼锛浠庢湇鍔″櫒涔熻兘澶熷鐞嗙綉缁滆繛鎺ヤ腑鏂備絾鏄紝鍙湁浠庢湇鍔″櫒瓒呰繃slave_net_timeout绉掓病鏈変粠涓绘湇鍔″櫒鏀跺埌鏁版嵁鎵嶉氱煡缃戠粶涓柇銆傚鏋滀腑鏂椂闂寸煭锛屽彲浠ラ檷浣slave_net_timeout銆傚弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

         鍏抽棴浠庢湇鍔″櫒锛堝噣鍏抽棴锛変篃寰堝畨鍏紝鍥犱负瀹冨彲浠ヨ窡韪畠绂诲紑鐨勫湴鐐广備笉绾噣鐨勫叧闂搷浣滀細浜х敓闂锛岀壒鍒槸绯荤粺鍏抽棴鍓嶇‖鐩樼紦瀛樻湭鍒锋柊鍒扮‖鐩樹笂鏃躲傚鏋滄湁涓嶉棿鏂數婧愶紝鍙互澶уぇ鎻愰珮绯荤粺瀹归敊鑳藉姏銆備笉绾噣鐨勫叧闂富鏈嶅姟鍣ㄤ細閫犳垚涓绘湇鍔″櫒涓婄殑琛ㄥ拰浜岃繘鍒舵棩蹇楀唴瀹逛箣闂寸殑涓嶄竴鑷存э紱鍦ㄤ富鏈嶅姟鍣ㄤ笂浣跨敤InnoDB琛ㄥ拰--innodb-safe-binlog閫夐」鍙互閬垮厤璇ラ棶棰樸傚弬瑙5.11.3鑺傦紝鈥滀簩杩涘埗鏃ュ織鈥(娉ㄩ噴锛MySQL 5.1涓笉闇瑕--innodb-safe-binlog锛岀敱浜庡紩鍏ヤ簡XA浜嬪姟鏀寔宸茬粡浣滃簾浜嗭級

         鐢变簬MyISAM琛ㄧ殑闈炰簨鍔″睘鎬э紝鍙互鏈変竴涓鍙ュ彧鏄洿鏂颁竴涓〃骞惰繑鍥為敊璇唬鐮併備緥濡傦紝澶氳鎻掑叆鏃舵湁涓涓瓒呰繃閿肩害鏉燂紝鎴栬呭鏋滈暱鐨勬洿鏂拌鍙ユ洿鏂伴儴鍒嗚鍚庤鏉鎺変簡銆傚鏋滃彂鐢熷湪涓绘湇鍔″櫒涓婏紝闄ら潪閿欒浠g爜鍚堟硶骞朵笖璇彞鎵ц浜х敓鐩稿悓鐨勯敊璇唬鐮侊紝浠庢湇鍔″櫒绾跨▼灏嗛鍑哄苟绛夊緟鏁版嵁搴撶鐞嗗憳鍐冲畾濡備綍鍋氥傚鏋滆閿欒浠g爜楠岃瘉琛屼负涓嶇悊鎯筹紝鍙互鐢--slave-skip-errors閫夐」鎺╃洊(蹇借)閮ㄥ垎鎴栧叏閮ㄩ敊璇

         濡傛灉浠BEGIN/COMMIT绯诲垪鐨勯潪浜嬪姟琛ㄦ洿鏂颁簨鍔¤〃锛屽鏋滄彁浜や簨鍔″墠鏇存柊闈炰簨鍔¤〃锛屽浜岃繘鍒舵棩蹇楃殑鏇存柊鍙兘浼氫笉鍚屾銆傝繖鏄洜涓轰簨鍔℃彁浜ゅ悗鎵嶈鍐欏叆浜岃繘鍒舵棩蹇椼

         浜嬪姟娣峰悎鏇存柊浜嬪姟琛ㄥ拰闈炰簨鍔¤〃鏃讹紝浜岃繘鍒舵棩蹇椾腑璇彞鐨勯『搴忔槸姝g‘鐨勶紝鍗充娇鍦ROLLBACK鏃讹紝鎵鏈夐渶瑕佺殑璇彞涔熶細鍐欏叆浜岃繘鍒舵棩蹇椼備絾鏄鏋滃湪绗1涓繛鎺ョ殑浜嬪姟瀹屾垚鍓嶏紝绗2涓繛鎺ユ洿鏂伴潪浜嬪姟琛紝璇彞璁板叆鏃ュ織鏃朵細鍑虹幇椤哄簭閿欒锛屽洜涓虹2涓繛鎺ョ殑鏇存柊鎵ц瀹屽悗绔嬪嵆鍐欏叆鏃ュ織锛岃屼笉绠$1涓繛鎺ユ墽琛岀殑浜嬪姟鐨勭姸鎬佸浣曘

6.8. 澶嶅埗鍚姩閫夐」

鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涓婏紝鍧囧繀椤讳娇鐢server-id閫夐」涓烘瘡涓湇鍔″櫒寤虹珛鍞竴鐨勫鍒ID銆備綘搴斾负姣忎釜涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ粠12321鐨勮寖鍥存寫涓涓敮涓鐨勬鏁存暟銆備緥濡傦細server-id=3

鐢ㄤ簬涓绘湇鍔″櫒涓婃帶鍒朵簩杩涘埗鏃ュ織鐨勯夐」鐨勭浉鍏虫弿杩拌5.11.3鑺傦紝鈥滀簩杩涘埗鏃ュ織鈥

涓嬭〃鎻忚堪浜嗗彲浠ョ敤浜MySQL 5.1浠庡睘澶嶅埗鏈嶅姟鍣ㄧ殑閫夐」銆備綘鍙互鍦ㄥ懡浠よ涓垨鍦ㄩ夐」鏂囦欢涓寚瀹氳繖浜涢夐」銆

鏌愪簺浠庢湇鍔″櫒澶嶅埗閫夐」鎸夌壒娈婃柟寮忓鐞嗭紝褰撲粠鏈嶅姟鍣ㄥ惎鍔ㄦ椂濡傛灉master.info鏂囦欢瀛樺湪骞朵笖鍖呭惈閫夐」鍊硷紝瀹冧滑灏嗚蹇界暐鎺夈備笅闈㈢殑閫夐」鎸夎繖绉嶆柟寮忓鐞嗭細

         --master-host

         --master-user

         --master-password

         --master-port

         --master-connect-retry

         --master-ssl

         --master-ssl-ca

         --master-ssl-capath

         --master-ssl-cert

         --master-ssl-cipher

         --master-ssl-key

5.1涓殑master.info鏂囦欢鏍煎紡鍖呮嫭瀵瑰簲SSL閫夐」鐨勫笺傚苟涓旓紝鏂囦欢鏍煎紡鍖呮嫭鏂囦欢涓殑琛屽彿锛屽鍚岀1琛屻傚鏋滀綘灏嗘棫鐨勬湇鍔″櫒鍗囩骇鍒版柊鐨勭増鏈紝鏂版湇鍔″櫒鍚姩鏃惰嚜鍔ㄥ皢smaster.info鏂囦欢鍗囩骇鍒版柊鐨勬牸寮忋傜劧鑰岋紝濡傛灉灏嗘柊鏈嶅姟鍣ㄩ檷绾у埌鏃х殑鐗堟湰锛岄娆″惎鍔ㄦ棫鐗堟湰鐨勬湇鍔″櫒涔嬪墠搴斿垹闄ょ1琛屻

濡傛灉浠庢湇鍔″櫒鍚姩鏃master.info鏂囦欢涓嶅瓨鍦紝閫夐」閲囩敤閫夐」鏂囦欢鎴栧懡浠よ涓寚瀹氱殑鍊笺傞娆″皢鏈嶅姟鍣ㄤ綔涓轰粠鏈嶅姟鍣ㄥ惎鍔ㄦ椂锛屾垨鑰呭凡缁忚繍琛RESET SLAVE鐒跺悗宸茬粡鍏抽棴骞堕噸鍚粠鏈嶅姟鍣ㄦ椂浼氬彂鐢熴

濡傛灉浠庢湇鍔″櫒鍚姩鏃master.info鏂囦欢瀛樺湪锛屾湇鍔″櫒蹇界暐閭d簺閫夐」銆備娇鐢master.info鏂囦欢涓彂鐜扮殑鍊笺

濡傛灉浣犱娇鐢ㄤ笌master.info鏂囦欢涓浉瀵瑰簲鐨勫惎鍔ㄩ夐」鐨勪笉鍚岀殑鍊奸噸鍚粠鏈嶅姟鍣紝鍚姩閫夐」鐨勪笉鍚岀殑鍊间笉浼氱敓鏁堬紝鍥犱负鏈嶅姟鍣ㄧ户缁娇鐢master.info鏂囦欢銆傝鎯充娇鐢ㄥ惎鍔ㄩ夐」鐨勪笉鍚岀殑鍊硷紝蹇呴』鍒犻櫎master.info鏂囦欢骞堕噸鍚粠鏈嶅姟鍣紝鎴(鏈濂芥槸)鍦ㄤ粠鏈嶅姟鍣ㄨ繍琛屾椂浣跨敤CHANGE MASTER TO璇彞閲嶆柊璁剧疆鍊笺

鍋囧畾鍦my.cnf鏂囦欢涓寚瀹氳閫夐」锛

[mysqld]
master-host=some_host

1娆′綔涓哄鍒朵粠鏈嶅姟鍣ㄥ惎鍔ㄦ湇鍔″櫒鏃讹紝浠my.cnf鏂囦欢璇诲彇骞朵娇鐢ㄩ夐」銆傛湇鍔″櫒鐒跺悗璁板綍master.info鏂囦欢涓殑鍊笺備笅娆″惎鍔ㄦ湇鍔″櫒鏃讹紝瀹冨彧浠庢湇鍔″櫒鐨master.info鏂囦欢璇诲彇涓绘湇鍔″櫒涓绘満鍊煎苟蹇界暐閫夐」鏂囦欢涓殑鍊笺傚鏋滀綘淇敼my.cnf鏂囦欢涓some_other_host鎸囧畾鍏跺畠涓绘湇鍔″櫒涓绘満锛屾洿鏀逛粛鐒朵笉浼氱敓鏁堛備綘搴斾娇鐢CHANGE MASTER TO

鍥犱负鏈嶅姟鍣ㄧ粰宸叉湁master.info鏂囦欢鐨勪紭鍏堟潈楂樹簬鍒氬垰鎻忚堪鐨勫惎鍔ㄩ夐」锛屽彲浠ラ夋嫨涓嶄娇鐢ㄨ繖浜涘肩殑鍚姩閫夐」锛岃屾槸浣跨敤CHANGE MASTER TO璇彞鏉ユ寚瀹氥傚弬瑙13.6.2.1鑺傦紝鈥淐HANGE MASTER TO璇硶鈥

涓嬮潰鐨勪緥瀛愭樉绀轰簡濡備綍鏇村箍娉涘湴浣跨敤鍚姩閫夐」鏉ラ厤缃粠鏈嶅姟鍣細

[mysqld]
server-id=2
master-host=db-master.mycompany.com
master-port=3306
master-user=pertinax
master-password=freitag
master-connect-retry=60
report-host=db-slave.mycompany.com

涓嬮潰鍒楀嚭浜嗘帶鍒跺鍒剁殑鍚姩閫夐」锛氳澶氶夐」鍙互鍦ㄦ湇鍔″櫒杩愯鏃堕氳繃CHANGE MASTER TO璇彞閲嶆柊杩涜璁剧疆銆傚叾瀹冮夐」锛屼緥濡--replicate-*閫夐」锛屽彧鑳藉湪浠庢湇鍔″櫒鍚姩鏃惰繘琛岃缃傛垜浠鍒掑皢淇璇ラ棶棰樸

         --logs-slave-updates

閫氬父鎯呭喌锛屼粠鏈嶅姟鍣ㄤ粠涓绘湇鍔″櫒鎺ユ敹鍒扮殑鏇存柊涓嶈鍏ュ畠鐨勪簩杩涘埗鏃ュ織銆傝閫夐」鍛婅瘔浠庢湇鍔″櫒灏嗗叾SQL绾跨▼鎵ц鐨勬洿鏂拌鍏ュ埌浠庢湇鍔″櫒鑷繁鐨勪簩杩涘埗鏃ュ織銆備负浜嗕娇璇ラ夐」鐢熸晥锛岃繕蹇呴』鐢--logs-bin閫夐」鍚姩浠庢湇鍔″櫒浠ュ惎鐢ㄤ簩杩涘埗鏃ュ織銆傚鏋滄兂瑕佸簲鐢ㄩ摼寮忓鍒舵湇鍔″櫒锛屽簲浣跨敤--logs-slave-updates銆備緥濡傦紝鍙兘浣犳兂瑕佽繖鏍疯缃細

A -> B -> C

涔熷氨鏄锛A涓轰粠鏈嶅姟鍣B鐨勪富鏈嶅姟鍣紝B涓轰粠鏈嶅姟鍣C鐨勪富鏈嶅姟鍣ㄣ備负浜嗚兘宸ヤ綔锛B蹇呴』鏃负涓绘湇鍔″櫒鍙堜负浠庢湇鍔″櫒銆備綘蹇呴』鐢--logs-bin鍚姩AB浠ュ惎鐢ㄤ簩杩涘埗鏃ュ織锛屽苟涓旂敤--logs-slave-updates閫夐」鍚姩B

         --logs-warnings

璁╀粠鏈嶅姟鍣ㄥ悜閿欒鏃ュ織杈撳嚭鏇磋缁嗙殑鍏充簬鍏舵墽琛屾搷浣滅殑娑堟伅銆備緥濡傦紝閫氱煡浣犵綉缁/杩炴帴澶辫触鍚庡凡缁忔垚鍔熼噸鏂拌繛鎺ワ紝骞堕氱煡浣犳瘡涓粠鏈嶅姟鍣ㄧ嚎绋嬪浣曞惎鍔ㄣ傝閫夐」榛樿鍚敤锛涜鎯崇鐢ㄥ畠锛屼娇鐢--skip-logs-warnings銆傛斁寮冪殑杩炴帴涓嶈鍏ラ敊璇棩蹇楋紝闄ら潪璇ュ煎ぇ浜1

璇锋敞鎰忚閫夐」鐨勬晥鏋滀笉闄愪簬澶嶅埗銆傚彲浠ュ鏈嶅姟鍣ㄧ殑閮ㄥ垎鍔ㄤ綔浜х敓璀﹀憡銆

         --master-connect-retry=seconds

鍦ㄤ富鏈嶅姟鍣ㄥ畷鏈烘垨杩炴帴涓㈠け鐨勬儏鍐典笅锛屼粠鏈嶅姟鍣ㄧ嚎绋嬮噸鏂板皾璇曡繛鎺ヤ富鏈嶅姟鍣ㄤ箣鍓嶇潯鐪犵殑绉掓暟銆傚鏋滀富鏈嶅姟鍣.info鏂囦欢涓殑鍊煎彲浠ヨ鍙栧垯浼樺厛浣跨敤銆傚鏋滄湭璁剧疆锛 榛樿鍊间负60

         --master-host=host

涓诲鍒舵湇鍔″櫒鐨勪富鏈哄悕鎴IP鍦板潃銆傚鏋滄病鏈夌粰鍑鸿閫夐」锛屼粠鏈嶅姟鍣ㄧ嚎绋嬩笉鍚姩銆傚鏋滀富鏈嶅姟鍣.info鏂囦欢涓殑鍊煎彲浠ヨ鍙栧垯浼樺厛浣跨敤銆

         --master-info-file=file_name

浠庢湇鍔″櫒鐢ㄤ簬璁板綍涓绘湇鍔″櫒鐨勭浉鍏充俊鎭娇鐢ㄧ殑鏂囦欢鍚嶃傞粯璁ゅ悕涓烘暟鎹洰褰曚腑鐨mysql.info

         --master-password=password

杩炴帴涓绘湇鍔″櫒鏃朵粠鏈嶅姟鍣ㄧ嚎绋嬬敤浜庨壌瀹氱殑璐︽埛鐨勫瘑鐮併傚鏋滀富鏈嶅姟鍣.info鏂囦欢涓殑鍊煎彲浠ヨ鍙栧垯浼樺厛浣跨敤銆傚鏋滄湭璁剧疆锛屽亣瀹 瀵嗙爜涓虹┖銆

         --master-port=port_number

涓绘湇鍔″櫒姝e抚鍚殑TCP/IP绔彛鍙枫傚鏋滀富鏈嶅姟鍣.info鏂囦欢涓殑鍊煎彲浠ヨ鍙栧垯浼樺厛浣跨敤銆傚鏋滄湭璁剧疆锛屽亣瀹氫娇鐢ㄧ紪璇戣繘鏉ョ殑璁惧畾鍊笺傚鏋滀綘鏈浘鐢configure閫夐」杩涜淇敼锛岃鍊煎簲涓3306

         --master-ssl--master-ssl-ca=file_name--master-ssl-capath=directory_name--master-ssl-cert=file_name--master-ssl-cipher=cipher_list--master-ssl-key=file_name

杩欎簺閫夐」鐢ㄤ簬浣跨敤SSL璁剧疆涓庝富鏈嶅姟鍣ㄧ殑瀹夊叏澶嶅埗杩炴帴銆傚畠浠殑鍚箟涓5.8.7.6鑺傦紝鈥淪SL鍛戒护琛岄夐」鈥涓弿杩扮殑鐩稿簲ssl--ssl-ca--ssl-capath--ssl-cert--ssl-cipher--ssl-key閫夐」鐩稿悓銆傚鏋滀富鏈嶅姟鍣.info鏂囦欢涓殑鍊煎彲浠ヨ鍙栧垯浼樺厛浣跨敤銆

         --master-user=username

杩炴帴涓绘湇鍔″櫒鏃朵粠鏈嶅姟鍣ㄧ嚎绋嬬敤浜庨壌瀹氱殑璐︽埛鐨勭敤鎴峰悕銆傝璐︽埛蹇呴』鍏锋湁REPLICATION SLAVE鏉冮檺銆傚鏋滀富鏈嶅姟鍣.info鏂囦欢涓殑鍊煎彲浠ヨ鍙栧垯浼樺厛浣跨敤銆傚鏋滄湭璁剧疆涓绘湇鍔″櫒鐢ㄦ埛锛屽亣瀹氫娇鐢ㄧ敤鎴test

         --max-relay-logs-size=size

鑷姩寰幆涓户鏃ュ織銆傚弬瑙5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥

         --read-only

璇ラ夐」璁╀粠鏈嶅姟鍣ㄥ彧鍏佽鏉ヨ嚜浠庢湇鍔″櫒绾跨▼鎴栧叿鏈SUPER鏉冮檺鐨勭敤鎴风殑鏇存柊銆傚彲浠ョ‘淇濅粠鏈嶅姟鍣ㄤ笉鎺ュ彈鏉ヨ嚜瀹㈡埛鐨勬洿鏂般

         --relay-log=file_name

涓户鏃ュ織鍚嶃傞粯璁ゅ悕涓host_name-relay-bin.nnnnnn锛屽叾涓host_name鏄粠鏈嶅姟鍣ㄤ富鏈虹殑鍚嶏紝nnnnnn琛ㄧず涓户鏃ュ織鍦ㄧ紪鍙峰簭鍒椾腑鍒涘缓銆傚鏋滀腑缁ф棩蹇楀お澶(骞朵笖浣犱笉鎯抽檷浣max_relay_log_size)锛岄渶瑕佸皢瀹冧滑鏀惧埌鏁版嵁鐩綍涔嬪鐨勫叾瀹冨湴鏂癸紝鎴栬呭鏋滄兂瑕侀氳繃纭洏涔嬮棿鐨勮礋杞藉潎琛℃彁楂橀熷害锛屽彲浠ユ寚瀹氶夐」鍒涘缓涓庝富鏈哄悕鏃犲叧鐨勪腑缁ф棩蹇楀悕銆

         --relay-log-index=file_name

涓户鏃ュ織绱㈠紩鏂囦欢浣跨敤鐨勪綅缃拰鍚嶇О銆傞粯璁ゅ悕涓host_name-relay-bin.index锛屽叾涓host_name涓轰粠鏈嶅姟鍣ㄥ悕銆

         --relay-log-info-file=file_name

浠庢湇鍔″櫒鐢ㄤ簬璁板綍涓户鏃ュ織鐩稿叧淇℃伅鐨勬枃浠跺悕銆傞粯璁ゅ悕涓烘暟鎹洰褰曚腑鐨relay-log.info

         --relay-log-purge={0|1}

绂佺敤鎴栧惎鐢ㄤ笉鍐嶉渶瑕佷腑缁ф棩蹇楁椂鏄惁鑷姩娓呯┖瀹冧滑銆傞粯璁ゅ间负1(鍚敤)銆傝繖鏄竴涓叏灞鍙橀噺锛屽彲浠ョ敤SET GLOBAL Relay_log_purge鍔ㄦ佹洿鏀广

         --relay-log-space-limit=size

闄愬埗鎵鏈変腑缁ф棩蹇楀湪浠庢湇鍔″櫒涓婃墍鍗犵敤绌洪棿鐨勪笂闄(0鍊艰〃绀衡鏃犻檺鍒)銆備粠鏈嶅姟鍣ㄤ富鏈虹‖鐩樼┖闂存湁闄愭椂寰堟湁鐢ㄣ傝揪鍒伴檺鍒跺悗锛I/O绾跨▼鍋滄浠庝富鏈嶅姟鍣ㄨ鍙栦簩杩涘埗鏃ュ織涓殑浜嬩欢锛岀洿鍒SQL绾跨▼琚棴閿佸苟涓斿垹闄や簡閮ㄥ垎鏈娇鐢ㄧ殑涓户鏃ュ織銆傝娉ㄦ剰璇ラ檺鍒跺苟涓嶆槸缁濆鐨勶細鏈夊彲鑳SQL绾跨▼鍒犻櫎涓户鏃ュ織鍓嶉渶瑕佹洿澶氱殑浜嬩欢銆傚湪杩欑鎯呭喌涓嬶紝I/O绾跨▼灏嗚秴杩囬檺鍒讹紝鐩村埌SQL绾跨▼鍙互鍒犻櫎閮ㄥ垎涓户鏃ュ織銆(涓嶈繖鏍峰仛灏嗕細閫犳垚姝婚攣锛--relay-log-space-limit鐨勫间笉鑳藉皬浜--max-relay-logs-size(鎴栧鏋--max-relay-logs-size0锛岄--max-binlog-size)鐨勫肩殑涓ゅ嶃傚湪杩欑鎯呭喌涓嬶紝鏈夊彲鑳I/O绾跨▼绛夊緟閲婃斁绌洪棿锛屽洜涓鸿秴杩囦簡--relay-log-space-limit锛屼絾SQL绾跨▼娌℃湁瑕佹竻绌虹殑涓户鏃ュ織锛屼笉鑳芥弧瓒I/O绾跨▼鐨勯渶姹傘傚己鍒I/O绾跨▼涓存椂蹇借--relay-log-space-limit

         --replicate-do-db=db_name

鍛婅瘔浠庢湇鍔″櫒闄愬埗榛樿鏁版嵁搴(USE鎵閫夋嫨)db_name鐨勮鍙ョ殑澶嶅埗銆傝鎸囧畾澶氫釜鏁版嵁搴擄紝搴斿娆′娇鐢ㄨ閫夐」锛屾瘡涓暟鎹簱浣跨敤涓娆°傝娉ㄦ剰涓嶅鍒惰法鏁版嵁搴撶殑璇彞锛屼緥濡傚綋宸茬粡閫夋嫨浜嗗叾瀹冩暟鎹簱鎴栨病鏈夋暟鎹簱鏃舵墽琛UPDATE some_db.some_table SET foo='bar'銆傚鏋滈渶瑕佽法鏁版嵁搴撹繘琛屾洿鏂帮紝浣跨敤--replicate-wild-do-table=db_name.%銆傝璇诲彇璇ラ夐」鍒楄〃鍚庨潰鐨勬敞鎰忎簨椤广

涓涓笉鑳芥寜鐓ф湡鏈涘伐浣滅殑渚嬪瓙锛氬鏋滅敤--replicate-do-db=sales鍚姩浠庢湇鍔″櫒锛屽苟涓斿湪涓绘湇鍔″櫒涓婃墽琛屼笅闈㈢殑璇彞锛UPDATE璇彞涓嶄細澶嶅埗锛

USE prices;
UPDATE sales.january SET amount=amount+1000;

濡傛灉闇瑕佽法鏁版嵁搴撹繘琛屾洿鏂帮紝搴斾娇鐢--replicate-wild-do-table=db_name.%

鍙鏌ラ粯璁ゆ暟鎹簱鈥濊涓虹殑涓昏鍘熷洜鏄鍙ヨ嚜宸卞緢闅剧煡閬撳畠鏄惁搴旇澶嶅埗(渚嬪锛屽鏋滀綘姝d娇鐢ㄨ法鏁版嵁搴撶殑澶氳〃DELETE璇彞鎴栧琛UPDATE璇彞)銆傚鏋滀笉闇瑕侊紝鍙鏌ラ粯璁ゆ暟鎹簱姣旀鏌ユ墍鏈夋暟鎹簱瑕佸揩寰楀銆

         --replicate-do-table=db_name.tbl_name

鍛婅瘔浠庢湇鍔″櫒绾跨▼闄愬埗瀵规寚瀹氳〃鐨勫鍒躲傝鎸囧畾澶氫釜琛紝搴斿娆′娇鐢ㄨ閫夐」锛屾瘡涓〃浣跨敤涓娆°傚悓--replicate-do-db瀵规瘮锛屽厑璁歌法鏁版嵁搴撴洿鏂般傝璇诲彇璇ラ夐」鍒楄〃鍚庨潰鐨勬敞鎰忎簨椤广

         --replicate-ignore-db=db_name

鍛婅瘔浠庢湇鍔″櫒涓嶈澶嶅埗榛樿鏁版嵁搴(USE鎵閫夋嫨)db_name鐨勮鍙ャ傝鎯冲拷鐣ュ涓暟鎹簱锛屽簲澶氭浣跨敤璇ラ夐」锛屾瘡涓暟鎹簱浣跨敤涓娆°傚鏋滄杩涜璺ㄦ暟鎹簱鏇存柊骞朵笖涓嶆兂澶嶅埗杩欎簺鏇存柊锛屼笉搴斾娇鐢ㄨ閫夐」銆傝璇诲彇璇ラ夐」鍚庨潰鐨勬敞鎰忎簨椤广

涓涓笉鑳芥寜鐓ф湡鏈涘伐浣滅殑渚嬪锛氬鏋滅敤--replicate-ignore-db=sales鍚姩浠庢湇鍔″櫒锛屽苟涓斿湪涓绘湇鍔″櫒涓婃墽琛屼笅闈㈢殑璇彞锛UPDATE璇彞涓嶄細澶嶅埗锛

                USE prices;
                UPDATE sales.january SET amount=amount+1000;

濡傛灉闇瑕佽法鏁版嵁搴撴洿鏂帮紝搴斾娇鐢--replicate-wild-ignore-table=db_name.%

         --replicate-ignore-table=db_name.tbl_name

鍛婅瘔浠庢湇鍔″櫒绾跨▼涓嶈澶嶅埗鏇存柊鎸囧畾琛ㄧ殑浠讳綍璇彞(鍗充娇璇ヨ鍙ュ彲鑳芥洿鏂板叾瀹冪殑琛)銆傝鎯冲拷鐣ュ涓〃锛屽簲澶氭浣跨敤璇ラ夐」锛屾瘡涓〃浣跨敤涓娆°傚悓--replicate-ignore-db瀵规瘮锛岃閫夐」鍙互璺ㄦ暟鎹簱杩涜鏇存柊銆傝璇诲彇璇ラ夐」鍚庨潰鐨勬敞鎰忎簨椤广

         --replicate-wild-do-table=db_name.tbl_name

鍛婅瘔浠庢湇鍔″櫒绾跨▼闄愬埗澶嶅埗鏇存柊鐨勮〃鍖归厤鎸囧畾鐨勬暟鎹簱鍜岃〃鍚嶆ā寮忕殑璇彞銆傛ā寮忓彲浠ュ寘鍚%鈥欏拰鈥_鈥欓氶厤绗︼紝涓LIKE妯″紡鍖归厤鎿嶄綔绗﹀叿鏈夌浉鍚岀殑鍚箟銆傝鎸囧畾澶氫釜琛紝搴斿娆′娇鐢ㄨ閫夐」锛屾瘡涓〃浣跨敤涓娆°傝閫夐」鍙互璺ㄦ暟鎹簱杩涜鏇存柊銆傝璇诲彇璇ラ夐」鍚庨潰鐨勬敞鎰忎簨椤广

渚嬪锛--replicate-wild-do-table=foo%.bar%鍙鍒舵暟鎹簱鍚嶄互foo寮濮嬪拰琛ㄥ悕浠bar寮濮嬬殑琛ㄧ殑鏇存柊銆

濡傛灉琛ㄥ悕妯″紡涓%锛屽彲鍖归厤浠讳綍琛ㄥ悕锛岄夐」涔熼傚悎鏁版嵁搴撶骇璇彞(CREATE DATABASEDROP DATABASEALTER DATABASE)銆備緥濡傦紝濡傛灉浣跨敤--replicate-wild-do-table=foo%.%锛屽鏋滄暟鎹簱鍚嶅尮閰嶆ā寮foo%锛屽垯澶嶅埗鏁版嵁搴撶骇璇彞銆

瑕佹兂鍦ㄦ暟鎹簱鎴栬〃鍚嶆ā寮忎腑鍖呮嫭閫氶厤绗︼紝鐢ㄥ弽鏂滅嚎瀵瑰畠浠繘琛岃浆涔夈備緥濡傦紝瑕佸鍒跺悕涓my_own%db鐨勬暟鎹簱鐨勬墍鏈夎〃锛屼絾涓嶅鍒my1ownAABCdb鏁版嵁搴撶殑琛紝搴旇繖鏍疯浆涔夆_鈥欏拰鈥%鈥欏瓧绗︼細--replicate-wild-do-table=my\_own\%db銆傚鏋滃湪鍛戒护琛屼腑浣跨敤閫夐」锛屽彲鑳介渶瑕佸弻鍙嶆枩绾挎垨灏嗛夐」鍊煎紩璧锋潵锛屽彇鍐充簬鍛戒护瑙i噴绗︺備緥濡傦紝鐢bash澶栧3鍒欓渶瑕佽緭鍏--replicate-wild-do-table=my\\_own\\%db

         --replicate-wild-ignore-table=db_name.tbl_name

鍛婅瘔浠庢湇鍔″櫒绾跨▼涓嶈澶嶅埗琛ㄥ尮閰嶇粰鍑虹殑閫氶厤绗︽ā寮忕殑璇彞銆傝鎯冲拷鐣ュ涓〃锛屽簲澶氭浣跨敤璇ラ夐」锛屾瘡涓〃浣跨敤涓娆°傝閫夐」鍙互璺ㄦ暟鎹簱杩涜鏇存柊銆傝璇诲彇璇ラ夐」鍚庨潰鐨勬敞鎰忎簨椤广

渚嬪锛--replicate-wild-ignore-table=foo%.bar%涓嶅鍒舵暟鎹簱鍚嶄互foo寮濮嬪拰琛ㄥ悕浠bar寮濮嬬殑琛ㄧ殑鏇存柊銆

鍏充簬鍖归厤濡備綍宸ヤ綔鐨勪俊鎭紝鍙傝--replicate-wild-do-table閫夐」鐨勬弿杩般傚湪閫夐」鍊间腑鍖呮嫭閫氶厤绗︾殑瑙勫垯涓--replicate-wild-ignore-table鐩稿悓銆

         --replicate-rewrite-db=from_name->to_name

鍛婅瘔浠庢湇鍔″櫒濡傛灉榛樿鏁版嵁搴(USE鎵閫夋嫨)涓轰富鏈嶅姟鍣ㄤ笂鐨from_name锛屽垯缈昏瘧涓to_name銆傚彧褰卞搷鍚湁琛ㄧ殑璇彞(涓嶆槸绫讳技CREATE DATABASEDROP DATABASEALTER DATABASE鐨勮鍙)锛屽苟涓斿彧鏈from_name涓轰富鏈嶅姟鍣ㄤ笂鐨勯粯璁ゆ暟鎹簱鏃躲傝閫夐」涓嶅彲浠ヨ法鏁版嵁搴撹繘琛屾洿鏂般傝娉ㄦ剰鍦ㄦ祴璇--replicate-*瑙勫垯涔嬪墠缈昏瘧鏁版嵁搴撳悕銆

濡傛灉鍦ㄥ懡浠よ涓娇鐢ㄨ閫夐」锛 鈥>鈥欏瓧绗︿笓鐢ㄤ簬鍛戒护瑙i噴绗︼紝搴斿皢閫夐」鍊煎紩璧锋潵銆備緥濡傦細

shell> mysqld --replicate-rewrite-db="olddb->newdb"

         --replicate-same-server-id

灏嗙敤浜庝粠鏈嶅姟鍣ㄤ笂銆傞氬父鍙互榛樿璁剧疆涓0浠ラ槻姝㈠惊鐜鍒朵腑鐨勬棤闄愬惊鐜傚鏋滆缃负1锛岃浠庢湇鍔″櫒涓嶈烦杩囨湁鑷繁鐨勬湇鍔″櫒id鐨勪簨浠讹紱閫氬父鍙湪鏈夊緢灏戦厤缃殑鎯呭喌涓嬫湁鐢ㄣ傚鏋滀娇鐢--logs-slave-updates涓嶈兘璁剧疆涓1銆傝娉ㄦ剰榛樿鎯呭喌涓嬪鏋滄湁浠庢湇鍔″櫒鐨id锛屾湇鍔″櫒I/O绾跨▼涓嶅皢浜岃繘鍒舵棩蹇椾簨浠跺啓鍏ヤ腑缁ф棩蹇(璇ヤ紭鍖栧彲浠ュ府鍔╄妭鐪佺‖鐩樼殑浣跨敤)銆傚洜姝ゅ鏋滄兂瑕佷娇鐢--replicate-same-server-id锛岃浠庢湇鍔″櫒璇诲彇鑷繁鐨SQL绾跨▼鎵ц鐨勪簨浠跺墠锛屼竴瀹氳鐢ㄨ閫夐」鍚姩銆

         --report-host=slave_name

浠庢湇鍔″櫒娉ㄥ唽杩囩▼涓姤鍛婄粰涓绘湇鍔″櫒鐨勪富鏈哄悕鎴IP鍦板潃銆傝鍊煎嚭鐜板湪涓绘湇鍔″櫒涓SHOW SLAVE HOSTS鐨勮緭鍑轰腑銆傚鏋滀笉鎯宠浠庢湇鍔″櫒鑷繁鍦ㄤ富鏈嶅姟鍣ㄤ笂娉ㄥ唽锛屽垯涓嶈缃鍊笺傝娉ㄦ剰浠庢湇鍔″櫒杩炴帴鍚庯紝涓绘湇鍔″櫒浠呬粎浠TCP/IP濂楁帴瀛楄鍙栦粠鏈嶅姟鍣ㄧ殑IP鍙锋槸涓嶅鐨勩傜敱浜 NAT鍜屽叾瀹冭矾鐢遍棶棰橈紝IP鍙兘涓嶅悎娉曪紝涓嶈兘浠庝富鏈嶅姟鍣ㄦ垨鍏跺畠涓绘満杩炴帴浠庢湇鍔″櫒銆

         --report-port=slave_port

杩炴帴浠庢湇鍔″櫒鐨TCP/IP绔彛鍙凤紝浠庢湇鍔″櫒娉ㄥ唽杩囩▼涓姤鍛婄粰涓绘湇鍔″櫒銆傚彧鏈変粠鏈嶅姟鍣ㄥ抚鍚潪榛樿绔彛鎴栧鏋滄湁涓涓壒娈婇毀閬撲緵涓绘湇鍔″櫒鎴栧叾瀹冨鎴疯繛鎺ヤ粠鏈嶅姟鍣ㄦ椂鎵嶈缃畠銆傚鏋滀綘涓嶇‘瀹氾紝涓嶈缃閫夐」銆

         --skip-slave-start

鍛婅瘔浠庢湇鍔″櫒褰撴湇鍔″櫒鍚姩鏃朵笉鍚姩浠庢湇鍔″櫒绾跨▼銆備娇鐢START SLAVE璇彞鍦ㄤ互鍚庡惎鍔ㄧ嚎绋嬨

         --slave_compressed_protocol={0|1}

濡傛灉璇ラ夐」璁剧疆涓 1锛屽鏋滀粠鏈嶅姟鍣ㄥ拰涓绘湇鍔″櫒鍧囨敮鎸侊紝浣跨敤鍘嬬缉浠庢湇鍔″櫒/涓绘湇鍔″櫒鍗忚銆

         --slave-load-tmpdir=file_name

浠庢湇鍔″櫒鍒涘缓涓存椂鏂囦欢鐨勭洰褰曞悕銆傝閫夐」榛樿绛変簬tmpdir绯荤粺鍙橀噺鐨勫笺傚綋浠庢湇鍔″櫒SQL绾跨▼澶嶅埗LOAD DATA INFILE璇彞鏃讹紝浠庝腑缁ф棩蹇楀皢寰呰杞界殑鏂囦欢鎻愬彇鍒颁复鏃舵枃浠讹紝鐒跺悗灏嗚繖浜涙枃浠惰鍏ュ埌琛ㄤ腑銆傚鏋滆杞藉埌涓绘湇鍔″櫒涓婄殑鏂囦欢寰堝ぇ锛屼粠鏈嶅姟鍣ㄤ笂鐨勪复鏃舵枃浠朵篃寰堝ぇ銆傚洜姝わ紝寤鸿浣跨敤璇ラ夐」鍛婅瘔浠庢湇鍔″櫒灏嗕复鏃舵枃浠舵斁鍒版枃浠剁郴缁熶腑鏈夊ぇ閲忓彲鐢ㄧ┖闂寸殑鐩綍涓嬨傚湪杩欑鎯呭喌涓嬶紝涔熷彲浠ヤ娇鐢--relay-log閫夐」灏嗕腑缁ф棩蹇楁斁鍒拌鏂囦欢绯荤粺涓紝鍥犱负涓户鏃ュ織涔熷緢澶с--slave-load-tmpdir搴旀寚鍚戝熀浜庣‖鐩樼殑鏂囦欢绯荤粺锛岃岄潪鍩轰簬鍐呭瓨鐨勬枃浠剁郴缁燂細浠庢湇鍔″櫒闇瑕佺敤涓存椂鏂囦欢鍦ㄦ満鍣ㄩ噸鍚椂鐢ㄤ簬澶嶅埗LOAD DATA INFILE銆傜郴缁熷惎鍔ㄨ繃绋嬩腑鎿嶄綔绯荤粺涔熶笉鑳芥竻闄よ鐩綍銆

         --slave-net-timeout=seconds

鏀惧純璇讳箣鍓嶄粠涓绘湇鍔″櫒绛夊欐洿澶氭暟鎹殑绉掓暟锛岃冭檻鍒拌繛鎺ヤ腑鏂拰灏濊瘯閲嶆柊杩炴帴銆傝秴鏃跺悗绔嬪嵆寮濮嬬1娆¢噸璇曘傜敱--master-connect-retry閫夐」鎺у埗閲嶈瘯涔嬮棿鐨勯棿闅斻

         --slave-skip-errors=[err_code1,err_code2,... | all]

閫氬父鎯呭喌锛屽綋鍑虹幇閿欒鏃跺鍒跺仠姝紝杩欐牱缁欎綘涓涓満浼氭墜鍔ㄨВ鍐虫暟鎹腑鐨勪笉涓鑷存ч棶棰樸傝閫夐」鍛婅瘔浠庢湇鍔″櫒SQL绾跨▼褰撹鍙ヨ繑鍥炰换浣曢夐」鍊间腑鎵鍒楃殑閿欒鏃剁户缁鍒躲

濡傛灉浣犱笉鑳藉畬鍏ㄧ悊瑙d负浠涔堝彂鐢熼敊璇紝鍒欎笉瑕佷娇鐢ㄨ閫夐」銆傚鏋滃鍒惰缃拰瀹㈡埛绋嬪簭涓病鏈bug锛屽苟涓MySQL鑷韩涔熸病鏈bug锛屽簲涓嶄細鍙戠敓鍋滄澶嶅埗鐨勯敊璇傛互鐢ㄨ閫夐」浼氫娇浠庢湇鍔″櫒涓庝富鏈嶅姟鍣ㄤ笉鑳戒繚瀛樺悓姝ワ紝骞朵笖浣犳壘涓嶅埌鍘熷洜銆

瀵逛簬閿欒浠g爜锛屼綘搴斾娇鐢ㄤ粠鏈嶅姟鍣ㄩ敊璇棩蹇椾腑閿欒娑堟伅鎻愪緵鐨勭紪鍙峰拰SHOW SLAVE STATUS鐨勮緭鍑恒傛湇鍔″櫒閿欒浠g爜鍒椾簬闄勫綍B锛閿欒浠g爜鍜屾秷鎭

浣犱篃鍙互(浣嗕笉搴)浣跨敤涓嶆帹鑽愮殑all鍊煎拷鐣ユ墍鏈夐敊璇秷鎭紝涓嶈冭檻鎵鍙戠敓鐨勯敊璇傛棤闇鑰岃█锛屽鏋滀娇鐢ㄨ鍊硷紝鎴戜滑涓嶈兘淇濊瘉鏁版嵁鐨勫畬鏁存с傚湪杩欑鎯呭喌涓嬶紝濡傛灉浠庢湇鍔″櫒鐨勬暟鎹笌涓绘湇鍔″櫒涓婄殑涓嶇浉杩戣涓嶈鎶辨(鎴栫紪鍐bug鎶ュ憡)宸茬粡璀﹀憡浣犱簡

渚嬪锛

--slave-skip-errors=1062,1053
--slave-skip-errors=all

浠庢湇鍔″櫒鎸変笅闈㈣瘎浼--replicate-*瑙勫垯锛岀‘瀹氭槸鍚︽墽琛屾垨蹇借璇彞锛

1.    鏄惁鏈--replicate-do-db--replicate-ignore-db瑙勫垯锛

         锛氭祴璇--binlog-do-db--binlog-ignore-db(鍙傝5.11.3鑺傦紝鈥滀簩杩涘埗鏃ュ織鈥)銆傛祴璇曠粨鏋滄槸浠涔堬紵

o        蹇借璇彞锛氬拷瑙嗗苟閫鍑恒

o        璁稿彲璇彞锛氫笉绔嬪嵆鎵ц璇彞銆傛帹杩熷喅绛栵紱缁х画涓嬩竴姝ャ

         娌℃湁锛氱户缁笅涓姝ャ

2.    鎴戜滑鐩墠姝f墽琛屼繚瀛樼殑绋嬪簭鎴栧嚱鏁板悧锛

         锛氭墽琛屾煡璇㈠苟閫鍑恒

         锛氱户缁笅涓姝ャ

3.    鏄惁鏈--replicate-*-table瑙勫垯锛

         娌℃湁锛氭墽琛屾煡璇㈠苟閫鍑恒

         锛氱户缁笅涓姝ュ苟寮濮嬫寜鎵绀洪『搴忚瘎浼拌〃瑙勫垯(棣栧厛鏄潪閫氶厤瑙勫垯锛岀劧鍚庢槸閫氶厤瑙勫垯)銆傚彧鏈夊緟鏇存柊鐨勮〃鏍规嵁杩欎簺瑙勫垯杩涜姣旇緝(INSERT INTO sales SELECT * FROM prices:鍙湁sales鏍规嵁杩欎簺瑙勫垯杩涜姣旇緝)銆傚鏋滆鏇存柊鍑犱釜琛(澶氳〃璇彞)锛岀1涓尮閰嶇殑琛(鍖归厤鈥do鈥濇垨鈥ignore)鑾疯耽銆備篃灏辨槸璇达紝鏍规嵁杩欎簺瑙勫垯姣旇緝绗1涓〃銆傜劧鍚庯紝濡傛灉涓嶈兘杩涜鍐崇瓥锛屾牴鎹繖浜涜鍒欐瘮杈冪2涓〃绛夌瓑銆

4.    鏄惁鏈--replicate-do-table瑙勫垯锛

         锛氳〃鍖归厤鍚楋紵

o        锛氭墽琛屾煡璇㈠苟閫鍑恒

o        锛氱户缁笅涓姝ャ

         娌℃湁锛氱户缁笅涓姝ャ

5.    鏄惁鏈--replicate-ignore-table瑙勫垯锛

         锛氳〃鍖归厤鍚楋紵

o        锛氬拷瑙嗘煡璇㈠苟閫鍑恒

o        锛氱户缁笅涓姝ャ

         娌℃湁锛氱户缁笅涓姝ャ

6.    鏄惁鏈--replicate-wild-do-table瑙勫垯锛

         锛氳〃鍖归厤鍚楋紵

o        锛氭墽琛屾煡璇㈠苟閫鍑恒

o        锛氱户缁笅涓姝ャ

         娌℃湁锛氱户缁笅涓姝ャ

7.    鏄惁鏈--replicate-wild-ignore-table瑙勫垯锛

         锛氳〃鍖归厤鍚楋紵

o        锛氬拷瑙嗘煡璇㈠苟閫鍑恒

o        锛氱户缁笅涓姝ャ

         娌℃湁锛氱户缁笅涓姝ャ

8.    娌℃湁鍖归厤鐨--replicate-*-table瑙勫垯銆傝鏍规嵁杩欎簺瑙勫垯娴嬭瘯鍏跺畠琛ㄥ悧锛

         锛氭墽琛屽惊鐜

         锛氭垜浠幇鍦ㄥ凡缁忔祴璇曚簡鎵鏈夊緟鏇存柊鐨勮〃锛岀粨鏋滀笉鑳藉尮閰嶄换浣曡鍒欍傛槸鍚︽湁--replicate-do-table--replicate-wild-do-table瑙勫垯锛

o        锛氭湁鈥do鈥濊鍒欎絾涓嶅尮閰嶃傚拷瑙嗘煡璇㈠苟閫鍑恒

o        娌℃湁锛氭墽琛屾煡璇㈠苟閫鍑恒

6.9. 澶嶅埗FAQ

Q锛氬鏋滀富鏈嶅姟鍣ㄦ鍦ㄨ繍琛屽苟涓斾笉鎯冲仠姝富鏈嶅姟鍣紝鎬庢牱閰嶇疆涓涓粠鏈嶅姟鍣紵

A锛氭湁澶氱鏂规硶銆傚鏋滀綘鍦ㄦ煇鏃堕棿鐐瑰仛杩囦富鏈嶅姟鍣ㄥ浠藉苟涓旇褰曚簡鐩稿簲蹇収鐨勪簩杩涘埗鏃ュ織鍚嶅拰鍋忕Щ閲(閫氳繃SHOW MASTER STATUS鍛戒护鐨勮緭鍑)锛岄噰鐢ㄤ笅闈㈢殑姝ラ锛

1.    纭繚浠庢湇鍔″櫒鍒嗛厤浜嗕竴涓敮涓鐨勬湇鍔″櫒ID鍙枫

2.    鍦ㄤ粠鏈嶅姟鍣ㄤ笂鎵ц涓嬮潰鐨勮鍙ワ紝涓烘瘡涓夐」濉叆閫傚綋鐨勫硷細

            mysql> CHANGE MASTER TO

                ->     MASTER_HOST='master_host_name',
                ->     MASTER_USER='master_user_name',
                ->     MASTER_PASSWORD='master_pass',
                ->     MASTER_LOG_FILE='recorded_log_file_name',
              ->     MASTER_LOG_POS=recorded_log_position;

3.    鍦ㄤ粠鏈嶅姟鍣ㄤ笂鎵цSTART SLAVE璇彞銆

濡傛灉浣犳病鏈夊浠戒富鏈嶅姟鍣紝杩欓噷鏄竴涓垱寤哄浠界殑蹇熺▼搴忋傛墍鏈夋楠ら兘搴旇鍦ㄤ富鏈嶅姟鍣ㄤ富鏈轰笂鎵ц銆

1.    鍙戝嚭璇ヨ鍙ワ細

     mysql> FLUSH TABLES WITH READ LOCK

2.    浠嶇劧鍔犻攣鏃讹紝鎵ц璇ュ懡浠わ紙鎴栧畠鐨勫彉浣擄級锛

     shell> tar zcf /tmp/backup.tar.gz /var/lib/mysql

3.    鍙戝嚭璇ヨ鍙ュ苟涓旂‘淇濊褰曚簡浠ュ悗鐢ㄥ埌鐨勮緭鍑猴細

     mysql>SHOW MASTER STATUS

4.    閲婃斁閿侊細

     mysql> UNLOCK TABLES

涓涓彲閫夋嫨鐨勬柟娉曟槸锛岃浆鍌ㄤ富鏈嶅姟鍣ㄧ殑SQL鏉ヤ唬鏇垮墠闈㈡楠や腑鐨勪簩杩涘埗澶嶅埗銆傝杩欐牱鍋氾紝浣犲彲浠ュ湪涓绘湇鍔″櫒涓婁娇鐢mysqldump --master-data浠ュ悗瑁呰浇SQL杞偍鍒板埌浣犵殑浠庢湇鍔″櫒銆傜劧鑰岋紝杩欐瘮杩涜浜岃繘鍒跺鍒堕熷害鎱€

涓嶇浣犱娇鐢ㄨ繖涓ょ鏂规硶涓殑閭d竴涓紝褰撲綘鏈変竴涓揩鐓у拰璁板綍浜嗘棩蹇楀悕涓庡亸绉婚噺鏃锛屽悗鏉ユ牴鎹鏄庢搷浣溿浣犲彲浠ヤ娇鐢ㄧ浉鍚岀殑蹇収寤虹珛澶氫釜浠庢湇鍔″櫒銆備竴鏃︿綘鎷ユ湁涓绘湇鍔″櫒鐨勪竴涓揩鐓э紝鍙互绛夊緟鍒涘缓涓涓粠鏈嶅姟鍣紝鍙涓绘湇鍔″櫒鐨勪簩杩涘埗鏃ュ織瀹屾暣銆備袱涓兘澶熺瓑寰呯殑鏃堕棿瀹為檯鐨勯檺鍒舵槸鎸囧湪涓绘湇鍔″櫒涓婁繚瀛樹簩杩涘埗鏃ュ織鐨勫彲鐢ㄧ‖鐩樼┖闂村拰浠庢湇鍔″櫒鍚屾鎵鐢ㄧ殑鏃堕棿銆

浣犱篃鍙互浣跨敤LOAD DATA FROM MASTER銆傝繖鏄竴涓柟渚跨殑璇彞锛屽畠浼犺緭涓涓揩鐓у埌浠庢湇鍔″櫒骞朵笖绔嬪嵆璋冩暣鏃ュ織鍚嶅拰鍋忕Щ閲忋傚皢鏉ワ紝LOAD DATA FROM MASTER灏嗘垚涓哄垱寤轰粠鏈嶅姟鍣ㄧ殑鎺ㄨ崘鏂规硶銆傜劧鑰岄渶瑕佹敞鎰忥紝瀹冨彧宸ヤ綔鍦MyISAM 琛ㄤ笂骞朵笖鍙兘闀挎椂闂存寔鏈夎閿佸畾銆傚畠骞朵笉璞℃垜浠笇鏈涚殑閭f牱楂樻晥鐜囧湴鎵ц銆傚鏋滀綘鏈夊ぇ琛紝鎵цFLUSH TABLES WITH READ LOCK璇彞鍚庯紝杩欐椂棣栭夋柟娉曚粛鐒舵槸鍦ㄤ富鏈嶅姟鍣ㄤ笂鍒朵綔浜岃繘鍒跺揩鐓с

Q锛氫粠鏈嶅姟鍣ㄩ渶瑕佸缁堣繛鎺ュ埌涓绘湇鍔″櫒鍚楋紵

A锛氫笉锛屼笉闇瑕併備粠鏈嶅姟鍣ㄥ彲浠ュ畷鏈烘垨鏂紑杩炴帴鍑犱釜灏忔椂鐢氳嚦鍑犲ぉ锛岄噸鏂拌繛鎺ュ悗鑾峰緱鏇存柊淇℃伅銆備緥濡傦紝浣犲彲浠ュ湪閫氳繃鎷ㄥ彿鐨勯摼鎺ヤ笂璁剧疆涓绘湇鍔″櫒/浠庢湇鍔″櫒鍏崇郴锛屽叾涓彧鏄伓灏旂煭鏃堕棿鍐呰繘琛岃繛鎺ャ傝繖鎰忓懗鐫锛屽湪浠讳綍缁欏畾鏃堕棿锛屼粠鏈嶅姟鍣ㄤ笉鑳戒繚璇佷笌涓绘湇鍔″櫒鍚屾闄ら潪浣犳墽琛屾煇浜涚壒娈婄殑鏂规硶銆傚皢鏉ワ紝鎴戜滑灏嗕娇鐢ㄩ夐」鏉ラ樆濉炰富鏈嶅姟鍣ㄧ洿鍒版湁涓涓粠鏈嶅姟鍣ㄥ悓姝ャ

Q锛氭垜鎬庢牱鐭ラ亾浠庢湇鍔″櫒涓庝富鏈嶅姟鍣ㄧ殑鏈鏂版瘮杈? 鎹㈠彞璇濊锛屾垜鎬庢牱鐭ラ亾浠庢湇鍔″櫒澶嶅埗鐨勬渶鍚庝竴涓煡璇㈢殑鏃ユ湡锛

A锛氫綘鍙互鏌ョ湅SHOW SLAVE STATUS璇彞鐨Seconds_Behind_Master鍒楃殑缁撴灉銆傚弬瑙6.3鑺傦紝鈥滃鍒跺疄鏂界粏鑺傗

褰撲粠鏈嶅姟鍣SQL绾跨▼鎵ц浠庝富鏈嶅姟鍣ㄨ鍙栫殑浜嬩欢鏃讹紝瀹冩牴鎹簨浠舵椂闂存埑淇敼鑷繁鐨勬椂闂达紙杩欐槸TIMESTAMP鑳藉寰堝ソ澶嶅埗鐨勫師鍥狅級銆傚湪SHOW PROCESSLIST璇彞杈撳嚭鐨Time鍒楀唴锛屼负浠庢湇鍔″櫒SQL绾跨▼鏄剧ず鐨勭鏁版槸鏈鍚庝竴涓鍒朵簨浠剁殑鏃堕棿鎴冲拰浠庢湇鍔″櫒涓绘満鐨勫疄闄呮椂闂翠箣闂寸浉宸殑绉掓暟銆備綘鍙互浣跨敤瀹冩潵纭畾鏈鍚庝竴涓鍒朵簨浠剁殑鏃ユ湡銆傛敞鎰忥紝濡傛灉浣犵殑浠庢湇鍔″櫒涓庝富鏈嶅姟鍣ㄨ繛鎺ユ柇寮涓涓皬鏃讹紝鐒跺悗閲嶆柊杩炴帴锛屽湪SHOW PROCESSLIST缁撴灉涓紝浣犲彲浠ョ珛鍗崇湅鍒颁粠鏈嶅姟鍣SQL绾跨▼鐨Time鍊间负3600銆傝繖鍙兘鏄洜涓轰粠鏈嶅姟鍣ㄦ墽琛岀殑璇彞鏄竴涓竴灏忔椂涔嬪墠鐨勩

Q锛氭垜鎬庢牱寮哄埗涓绘湇鍔″櫒闃诲鏇存柊鐩村埌浠庢湇鍔″櫒鍚屾锛

A锛氫娇鐢ㄤ笅闈㈢殑姝ラ锛

1.    鍦ㄤ富鏈嶅姟鍣ㄤ笂锛屾墽琛岃繖浜涜鍙ワ細

     mysql> FLUSH TABLES WITH READ LOCK;

     mysql> SHOW MASTER STATUS;

 

璁板綍SHOW璇彞鐨勮緭鍑虹殑鏃ュ織鍚嶅拰鍋忕Щ閲忋傝繖浜涙槸澶嶅埗鍧愭爣銆

2.    鍦ㄤ粠鏈嶅姟鍣ㄤ笂锛屽彂鍑轰笅闈㈢殑璇彞锛屽叾涓Master_POS_WAIT()鍑芥暟鐨勫弬閲忔槸鍓嶉潰姝ラ涓殑寰楀埌鐨勫鍒跺潗鏍囧硷細

     mysql> SELECT MASTER_POS_WAIT('log_name', log_offset);

SELECT璇彞闃诲鐩村埌浠庢湇鍔″櫒杈惧埌鎸囧畾鐨勬棩蹇楁枃浠跺拰鍋忕Щ閲忋傛鏃讹紝浠庢湇鍔″櫒涓庝富鏈嶅姟鍣ㄥ悓姝ワ紝璇彞杩斿洖銆

3.    鍦ㄤ富鏈嶅姟鍣ㄤ笂锛屽彂鍑轰笅闈㈢殑璇彞鍏佽涓绘湇鍔″櫒閲嶆柊寮濮嬪鐞嗘洿鏂帮細

     mysql> UNLOCK TABLES

Q锛氬綋璁剧疆鍙屽悜澶嶅埗鏃舵垜搴旇鐭ラ亾鍙戝嚭閭d簺璇彞锛

AMySQL澶嶅埗鐩墠涓嶆敮鎸佷富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涔嬮棿鐨勪换浣曢攣瀹氬崗璁潵淇濊瘉鍒嗗竷寮(璺ㄦ湇鍔″櫒)鏇存柊鐨勫師瀛愭с傛崲鍙ヨ瘽璇达紝杩欐牱鍋氭槸鍙兘鐨勶細瀹㈡埛A鏍规嵁鍗忎綔-涓绘湇鍔″櫒1鏇存柊锛屽悓鏃讹紝鍦ㄥ畠浼犵粰鍗忎綔-涓绘湇鍔″櫒2涔嬪墠锛屽鎴B鑳藉鏍规嵁鍗忎綔-涓绘湇鍔″櫒2鏇存柊锛岃繖鏍峰鎴A鐨勬洿鏂颁笌瀹冨湪鍗忎綔-涓绘湇鍔″櫒1鐨勬洿鏂颁笉鍚屻傝繖鏍凤紝褰撳鎴A鏍规嵁鍗忎綔-涓绘湇鍔″櫒2鏇存柊鏃讹紝瀹冧骇鐢熺殑琛ㄤ笌鍦ㄥ崗浣-涓绘湇鍔″櫒1涓婄殑涓鍚岋紝鍗充娇鎵鏈夋牴鎹崗浣-涓绘湇鍔″櫒2鐨勬洿鏂板凡缁忎紶杩囨潵銆傝繖鎰忓懗鐫锛屽湪鍙屽悜澶嶅埗鍏崇郴涓紝浣犱笉搴旇鎶婁袱涓湇鍔″櫒涓茶繛鍦ㄤ竴璧凤紝闄ら潪浣犵‘淇′换浣曢『搴忕殑鏇存柊鏄畨鍏ㄧ殑锛屾垨鑰呴櫎闈炰綘鍦ㄥ鎴风浠g爜涓敞鎰忔庢牱閬垮厤鏇存柊椤哄簭閿欒銆

浣犺繕蹇呴』璁よ瘑鍒颁粠鏇存柊瑙掑害锛屽弻鍚戝鍒跺疄闄呬笂骞朵笉鑳芥樉钁楀湴鎻愰珮鎬ц兘锛堟垨鑰呮牴鏈笉鑳芥彁楂樻ц兘锛夈備袱涓湇鍔″櫒閮介渶瑕佸仛鐩稿悓鏁伴噺鐨勬洿鏂帮紝濡傚悓鍦ㄤ竴涓湇鍔″櫒鍋氱殑閭f牱銆傚敮涓鐨勫樊鍒槸閿佺珵浜夎灏戯紝杩欏洜涓烘簮浜庡彟涓涓湇鍔″櫒鐨勬洿鏂板湪涓涓粠绾跨▼涓簭鍒楀寲銆傚嵆浣胯繖涓泭澶勫彲鑳借缃戠粶寤惰繜鎶垫秷銆

Q锛氭庢牱閫氳繃澶嶅埗鏉ユ彁楂樼郴缁熺殑鎬ц兘锛

A锛氫綘搴斿皢涓涓湇鍔″櫒璁剧疆涓轰富鏈嶅姟鍣ㄥ苟涓斿皢鎵鏈夊啓鎸囧悜璇ユ湇鍔″櫒銆傜劧鍚庢牴鎹绠楅厤缃敖鍙兘澶氱殑浠庢湇鍔″櫒浠ュ強鏍堢┖闂达紝骞朵笖鍦ㄤ富鏈嶅姟鍣ㄥ拰浠庢湇鍔″櫒涔嬮棿鍒嗗彂璇诲彇鎿嶄綔銆備綘涔熷彲浠ョ敤--skip-innodb--skip-bdb--low-priority-updates浠ュ強--delay-key-write=ALL閫夐」鍚姩浠庢湇鍔″櫒锛屼互渚垮湪浠庢湇鍔″櫒绔彁楂橀熷害銆傚湪杩欑鎯呭喌涓嬶紝涓轰簡鎻愰珮閫熷害锛屼粠鏈嶅姟鍣ㄤ娇鐢ㄩ潪浜嬪姟MyISAM琛ㄦ潵浠f浛InnoDBBDB琛ㄣ

Q锛氫负浜嗕娇鐢ㄩ珮鎬ц兘鐨勫鍒讹紝鎴戝簲璇ュ湪鑷繁鐨勫簲鐢ㄧ▼搴忎腑鎬庢牱鍑嗗瀹㈡埛绔唬鐮侊紵

A锛氬鏋滀綘鐨勪唬鐮佷腑鏁版嵁搴撹闂儴鍒嗗凡缁忔纭湴妯″潡鍖栵紝搴旇鑳藉骞虫粦鍜屽鏄撳湴杞崲涓哄湪澶嶅埗姝ラ涓繍琛岀殑浠g爜銆備粎闇瑕佹洿鏀规暟鎹簱璁块棶鎵ц閮ㄥ垎锛屼互渚垮彂閫佹墍鏈夌殑鍐欐搷浣滃埌涓绘湇鍔″櫒锛屼互鍙婂彂閫佽鎿嶄綔鍒颁富鏈嶅姟鍣ㄦ垨鏌愪釜浠庢湇鍔″櫒銆傚鏋滀綘鐨勪唬鐮佹病鏈夎繖涓骇鍒紝璁剧疆涓涓鍒剁郴缁熶互渚挎竻闄ゃ傚簲鍏堥氳繃涓嬮潰鐨勫嚱鏁板垱寤轰竴涓寘瑁呭簱鎴栨ā鍧楋細

         safe_writer_connect()

         safe_reader_connect()

         safe_reader_statement()

         safe_writer_statement()

姣忎釜鍑芥暟鍚嶇殑safe_鎰忓懗鐫鍑芥暟姣旇緝灏忓績鍦板鐞嗘墍鏈夐敊璇備綘鍙互浣跨敤涓嶅悓鍚嶇殑鍑芥暟銆傞噸瑕佹槸瀵逛簬璇昏繛鎺ャ佸啓杩炴帴銆佽鍜屽啓鏈変竴涓粺涓鐨勬帴鍙c

鐒跺悗锛屼綘搴旇杞崲瀹㈡埛绔唬鐮佷娇鐢ㄥ寘瑁呭簱銆傚垰寮濮嬭繖鍙兘鏄棝鑻﹀拰鎭愭厡鐨勮繃绋嬶紝浣嗕粠闀胯繙鏉ョ湅鏄煎緱鐨勩備娇鐢ㄥ垰鎵嶈璁虹殑鏂规硶鐨勬墍鏈夊簲鐢ㄧ▼搴忛兘鑳藉鍒╃敤涓绘湇鍔″櫒/浠庢湇鍔″櫒閰嶇疆鐨勪紭瓒婃э紝鍗充娇鏄惈鏈夊涓粠鏈嶅姟鍣ㄧ殑閰嶇疆銆備唬鐮侀潪甯稿鏄撶淮鎶わ紝骞朵笖娣诲姞鎺掗敊閫夐」涔熷緢瀹规槗銆備綘浠呴渶瑕佷慨鏀逛竴涓や釜鍑芥暟锛涗緥濡傦紝璁板綍姣忎釜璇彞鎵ц鐨勬椂闂达紝鎴栬呬綘鐨勪笂鍗冧釜璇彞涓摢涓鍙ュ彂鐢熶簡閿欒銆

濡傛灉浣犲凡缁忕紪鍐欎簡璁稿浠g爜锛屼綘鍙兘鎯充娇鐢replace宸ュ叿鑷姩杩涜杞崲锛岃宸ュ叿闅忔爣鍑MySQL涓璧峰彂甯冿紝鎴栧彲浠ヨ嚜宸辩紪鍐欒浆鎹㈣剼鏈傜悊鎯虫儏鍐碉紝浣犵殑浠g爜浣跨敤涓鑷寸殑绋嬪簭杞崲椋庢牸銆傚惁鍒欙紝鍙兘鏈濂介噸鏂扮紪鍐欎唬鐮侊紝鎴栬呰嚦灏戞墜宸ュ鍏惰繘琛岃鍒欏寲浠ヤ娇鐢ㄤ竴鑷寸殑椋庢牸銆

QMySQL澶嶅埗鑳藉浣曟椂鍜屽澶х▼搴︽彁楂樼郴缁熸ц兘锛

AMySQL澶嶅埗瀵逛簬棰戠箒璇诲拰棰戠箒鍐欑殑绯荤粺鍏锋湁鏈澶уソ澶勩傜悊璁轰笂锛岄氳繃浣跨敤鍗曚釜涓绘湇鍔″櫒/澶氫粠鏈嶅姟鍣ㄨ缃紝鍙互閫氳繃娣诲姞鏇村鐨勪粠鏈嶅姟鍣ㄦ潵鎵╁厖绯荤粺锛岀洿鍒扮敤瀹岀綉缁滃甫瀹斤紝鎴栬呬綘鐨勬洿鏂拌礋杞藉凡缁忓闀垮埌涓绘湇鍔″櫒涓嶈兘澶勭悊鐨勭偣銆

鍦ㄨ幏寰楃殑鏀剁泭寮濮嬪悆骞充箣鍓嶏紝涓轰簡纭畾鍙互鏈夊灏戜粠鏈嶅姟鍣紝浠ュ強鍙互灏嗕綘鐨勭珯鐐圭殑鎬ц兘鎻愰珮澶氬皯锛岄渶瑕佺煡閬撴煡璇㈡ā寮忥紝骞朵笖瑕侀氳繃鍩哄噯娴嬭瘯骞舵牴鎹粡楠岀‘瀹氫竴涓吀鍨嬬殑涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄤ腑鐨勮鍙栵紙姣忕閽熻鍙栭噺锛屾垨鑰max_reads锛夊悶鍚愰噺鍜屽啓锛max_writes锛夊悶鍚愰噺鐨勫叧绯汇傞氳繃涓涓亣璁剧殑甯︽湁澶嶅埗鐨勭郴缁燂紝鏈緥缁欏嚭浜嗕竴涓潪甯哥畝鍗曠殑璁$畻缁撴灉銆

鍋囪绯荤粺璐熻浇鍖呮嫭10%鐨勫啓鍜90%鐨勮鍙栵紝骞朵笖鎴戜滑閫氳繃鍩哄噯娴嬭瘯纭畾max_reads1200 2 max_writes銆傛崲鍙ヨ瘽璇达紝濡傛灉娌℃湁鍐欐搷浣滐紝绯荤粺姣忕鍙互杩涜1,200娆¤鍙栨搷浣滐紝骞冲潎鍐欐搷浣滄槸骞冲潎璇绘搷浣滄墍鐢ㄦ椂闂寸殑涓ゅ嶏紝骞朵笖鍏崇郴鏄嚎鎬х殑銆傛垜浠亣瀹氫富鏈嶅姟鍣ㄥ拰姣忎釜浠庢湇鍔″櫒鍏锋湁鐩稿悓鐨勬ц兘锛屽苟涓旀垜浠湁涓涓富鏈嶅姟鍣ㄥ拰N涓粠鏈嶅姟鍣ㄣ傞偅涔堬紝瀵逛簬姣忎釜鏈嶅姟鍣紙涓绘湇鍔″櫒鎴栦粠鏈嶅姟鍣級锛屾垜浠湁锛

reads = 1200 2 writes

reads = 9 writes / (N + 1) (璇诲彇鏄垎绂荤殑, 浣嗘槸鍐欏叆鎵鏈夋湇鍔″櫒)

9 writes / (N + 1) + 2 writes = 1200

writes = 1200 / (2 + 9/(N+1))

鏈鍚庣殑绛夊紡琛ㄦ槑浜N涓粠鏈嶅姟鍣ㄧ殑鏈澶у啓鎿嶄綔鏁帮紝鍋囪鏈澶у彲鑳界殑璇诲彇閫熺巼鏄瘡鍒嗛挓1,200娆★紝璇绘搷浣滀笌鍐欐搷浣滅殑姣旂巼鏄9

濡備笂鍒嗘瀽鍙互寰楀埌涓嬮潰鐨勭粨璁猴細

         濡傛灉N = 0锛堣繖琛ㄦ槑娌℃湁澶嶅埗锛夛紝绯荤粺姣忕鍙互澶勭悊澶х害1200/11 = 109涓啓鎿嶄綔銆

         濡傛灉N = 1锛屾瘡绉掑緱鍒184涓啓鎿嶄綔銆

         濡傛灉N = 8锛屾瘡绉掑緱鍒400涓啓鎿嶄綔銆

         濡傛灉N = 17锛屾瘡绉掑緱鍒480涓啓鎿嶄綔銆

         鏈鍚庯紝褰 N 瓒嬩簬鏃犵┓澶э紙浠ュ強鎴戜滑棰勭畻鐨勮礋鏃犵┓澶э級鏃讹紝鍙互寰楀埌闈炲父鎺ヨ繎姣忕600涓啓鎿嶄綔锛岀郴缁熷悶鍚愰噺澧炲姞灏嗚繎5.5鍊嶃傜劧鑰岋紝濡傛灉鍙敤8涓湇鍔″櫒锛屽鍔犳帴杩4鍊嶃

璇锋敞鎰忥紝杩欎簺璁$畻鍋囪缃戠粶甯﹀鏃犵┓澶у苟蹇界暐鎺変簡鍏跺畠涓浜涘洜绱狅紝閭d簺鍥犵礌鍙兘瀵圭郴缁熶骇鐢熼噸瑕佺殑褰卞搷銆傚湪璁稿鎯呭喌涓嬶紝涓嶈兘鎵ц涓庡垰鎵嶇被浼肩殑璁$畻锛屽嵆濡傛灉娣诲姞N鍙板鍒朵粠鏈嶅姟鍣紝搴旇鍑嗙‘棰勬姤绯荤粺灏嗗彂鐢熷摢浜涘奖鍝嶃傚洖绛斾笅闈㈢殑闂搴旇兘澶熷府鍔╀綘纭畾澶嶅埗鏄惁鍜屽湪澶氬ぇ绋嬪害涓婅兘澶熸彁楂樼郴缁熺殑鎬ц兘锛

         绯荤粺涓婄殑璇诲彇/鍐欐瘮渚嬫槸浠涔?

         濡傛灉鍑忓皯璇诲彇鎿嶄綔锛屼竴涓湇鍔″櫒鍙互澶氬鐞嗗灏戝啓璐熻浇锛

         缃戠粶甯﹀鍙弧瓒冲灏戜粠鏈嶅姟鍣ㄧ殑闇姹?

Q锛氬浣曚娇鐢ㄥ鍒舵潵鎻愪緵鍐椾綑/楂樺彲鐢ㄦ?

A锛氬埄鐢ㄧ洰鍓嶇殑鍙敤鐗规э紝蹇呴』璁剧疆涓涓富鏈嶅姟鍣ㄥ拰涓涓粠鏈嶅姟鍣紙鎴栧涓粠鏈嶅姟鍣級锛屼互鍙婂啓涓涓剼鏈潵鐩戣涓绘湇鍔″櫒鏄惁鍚姩銆傚鏋滀富鏈嶅姟鍣ㄥけ璐ワ紝閫氱煡搴旂敤绋嬪簭鍜屼粠鏈嶅姟鍣ㄥ垏鎹富鏈嶅姟鍣ㄣ備笅闈㈡槸涓浜涘缓璁細

         鍛婄煡浠庢湇鍔″櫒鏇存敼鍏朵富鏈嶅姟鍣紝浣跨敤CHANGE MASTER TO璇彞銆

         閫氱煡搴旂敤绋嬪簭涓绘湇鍔″櫒浣嶇疆鐨勪竴涓緢濂界殑鏂规硶鏄涓绘湇鍔″櫒鎻愪緵鍔ㄦDNS鍏ュ彛銆傜敤bind鍙互浣跨敤nsupdate鍔ㄦ佹洿鏂DNS

         搴旇鐢--logs-bin閫夐」鑰屼笉鐢 --logs-slave-updates閫夐」杩愯浠庢湇鍔″櫒銆傝繖鏍凤紝涓鏃︿綘鍦ㄥ叾瀹冧粠鏈嶅姟鍣ㄤ笂鍙戝嚭STOP SLAVE; RESET MASTER, 浠ュ強CHANGE MASTER TO璇彞璇ヤ粠鏈嶅姟鍣ㄥ彲浠ュ垏鎹负涓绘湇鍔″櫒銆備緥濡傦紝鍋囪鏈変笅闈㈢殑璁剧疆锛

                       WC
                        \
                         v
                 WC----> M
                       / | \
                      /  |  \
                     v   v   v
                    S1   S2  S3

M浠h〃涓绘湇鍔″櫒锛S浠h〃浠庢湇鍔″櫒锛WC浠h〃鍙戝嚭鏁版嵁搴撳啓鍜岃鍙栨搷浣滅殑瀹㈡埛锛涘彧鍙戝嚭鏁版嵁搴撹鍙栨搷浣滅殑瀹㈡埛娌℃湁缁欏嚭锛屽洜涓哄畠浠笉闇瑕佸垏鎹€S1S2浠ュ強S3鏄粠鏈嶅姟鍣紝鐢--logs-bin閫夐」鑰屾病鏈夌敤--logs-slave-updates杩愯銆傚洜涓轰粠鏈嶅姟鍣ㄦ敹鍒扮殑涓绘湇鍔″櫒鐨勬洿鏂版病鏈夎褰曞湪浜岃繘鍒舵棩蹇椾腑锛岄櫎闈炴寚瀹 --logs-slave-updates閫夐」锛屾瘡涓粠鏈嶅姟鍣ㄤ笂鐨勪簩杩涘埗鏃ュ織鏄┖鐨勩傚鏋滃洜涓烘煇浜涘師鍥M 鍙樺緱涓嶅彲鐢紝浣犲彲浠ラ夊彇涓涓粠鏈嶅姟鍣ㄥ彉涓烘柊鐨勪富鏈嶅姟鍣ㄣ備緥濡傦紝濡傛灉浣犻夊彇浜S1锛屾墍鏈WC搴旇閲嶆柊鎸囧悜S1S2锛屽苟涓S3鐒跺悗搴斾粠S1澶嶅埗

纭繚鎵鏈変粠鏈嶅姟鍣ㄥ凡缁忓鐞嗕簡涓户鏃ュ織涓殑鎵鏈夎鍙ャ傚湪姣忎釜浠庢湇鍔″櫒涓婏紝鍙戝嚭STOP SLAVE IO_THREAD璇彞锛岀劧鍚庢鏌SHOW PROCESSLIST璇彞鐨勮緭鍑猴紝鐩村埌浣犵湅鍒Has read all relay log銆傚綋鎵鏈変粠鏈嶅姟鍣ㄩ兘鎵ц瀹岃繖浜涳紝瀹冧滑鍙互琚噸鏂伴厤缃负涓涓柊鐨勮缃傚湪琚彁鍗囦负涓绘湇鍔″櫒鐨勪粠鏈嶅姟鍣S1涓婏紝鍙戝嚭STOP SLAVERESET MASTER璇彞銆

鍦ㄥ叾瀹冧粠鏈嶅姟鍣S2S3锛屼娇鐢STOP SLAVECHANGE MASTER TO MASTER_HOST='S1'锛堝叾涓'S1'琛ㄧずS1瀹為檯鐨勪富鏈哄悕锛夈備负CHANGE MASTER娣诲姞鍏充簬浠S2S3濡備綍杩炴帴鍒S1鎵鏈変俊鎭紙userpasswordport锛夈傚湪CHANGE MASTER鍛戒护涓紝涓嶉渶瑕佹寚瀹氫粠鍏惰鍙栫殑S1鐨勪簩杩涘埗鏃ュ織鍚嶆垨浜岃繘鍒舵棩蹇椾綅缃細鎴戜滑鐭ラ亾瀹冩槸绗1涓簩杩涘埗鏃ュ織锛屼綅缃槸4锛岃繖鏄CHANGE MASTER鍛戒护鐨勯粯璁ゅ笺傛渶鍚庯紝鍦S2S3浣跨敤START SLAVE 鍛戒护銆

鐒跺悗锛屾寚绀烘墍鏈WC 鎶婂畠浠殑璇彞鎸囧悜S1姝ゅ悗锛WC鍙戝嚭鐨勬墍鏈夊彂閫佸埌S1鏇存柊璇彞琚啓鍏S1浜岃繘鍒舵棩蹇楋紝S1鍒欏寘鍚M姝绘帀涔嬪悗鐨勫彂閫佸埌 S1鐨勬瘡涓涓洿鏂拌鍙ャ

缁撴灉鏄笅闈㈢殑閰嶇疆锛

       WC
      /
      |
 WC   |  M(unavailable)
  \   |
   \  |
    v v
     S1<--S2  S3
      ^       |
      +-------+

M閲嶆柊鍚姩鍚庯紝浣犲繀椤诲湪M鍙戝嚭鐩稿悓鐨CHANGE MASTER璇彞锛屼笌鍦S2S3涓婂彂鍑虹殑璇彞涓鏍凤紝浠ヤ究M鍙樹负S1浠庢湇鍔″櫒骞朵笖鎭㈠鍦ㄥ畠瀹曟満鍚庝涪澶辩殑鎵鏈WC鍐欐搷浣溿傝鎶 M 鍐嶆浣滀负涓绘湇鍔″櫒锛堜緥濡傦紝鍥犱负瀹冩槸鍔熻兘鏈寮虹殑鏈哄櫒锛夛紝浣跨敤鍓嶉潰鐨勬楠わ紝濂藉儚S1涓嶅彲鐢ㄥ苟涓M鍙樹负涓涓柊鐨勪富鏈嶅姟鍣ㄤ竴鏍枫傚湪杩欎釜杩囩▼涓紝鍦S1S2浠ュ強S3浣滀负M浠庢湇鍔″櫒涔嬪墠锛屼笉瑕佸繕璁板湪M杩愯RESET MASTER銆傚惁鍒欙紝瀹冧滑鍙兘鎷惧彇M鍙樺緱涓嶅彲鐢ㄤ箣鍓嶇殑鏃WC鍐欐搷浣溿

鎴戜滑鐩墠姝e湪MySQL闆嗘垚鑷姩涓绘湇鍔″櫒閫夋嫨绯荤粺锛屼絾鍦ㄥ噯澶囧ソ涔嬪墠锛屼綘蹇呴』鍒涘缓鑷繁鐨勭洃鎺у伐鍏枫

6.10. 澶嶅埗鏁呴殰璇婃柇涓庢帓闄

濡傛灉浣犻伒浠庝簡涓婅堪璇存槑锛屽鍒惰缃粛鐒朵笉宸ヤ綔锛岄鍏堟鏌ヤ笅闈㈠悇椤癸細

         妫鏌ラ敊璇棩蹇楃殑娑堟伅銆傝澶氱敤鎴烽亣鍒伴棶棰樺悗娌℃湁鍙婃椂鍦拌繖鏍峰仛鑰屾氮璐逛簡鏃堕棿銆

         涓绘湇鍔″櫒璁板綍鍒颁簡浜岃繘鍒舵棩蹇楋紵鐢SHOW MASTER STATUS妫鏌ャ傚鏋滃凡缁忚褰曪紝Position搴斾负闈為浂銆傚鏋滄病鏈夎褰曪紝纭姝g敤log-binserver-id閫夐」杩愯涓绘湇鍔″櫒銆

         鏄惁浠庢湇鍔″櫒鍦ㄨ繍琛岋紵浣跨敤SHOWSHOW SLAVE STATUS妫鏌ユ槸鍚slave_IO_Runningslave_SQL_Running鐨勫煎潎涓Yes銆傚鏋滀笉鏄紝楠岃瘉褰撳惎鍔ㄤ粠鏈嶅姟鍣ㄦ椂浣跨敤鐨勯夐」銆

         濡傛灉浠庢湇鍔″櫒姝e湪杩愯锛屽缓绔嬩簡涓庝富鏈嶅姟鍣ㄧ殑杩炴帴鍚楋紵浣跨敤SHOW PROCESSLIST锛屾壘鍑I/OSQL绾跨▼骞舵鏌ュ畠浠殑State鍒楃湅瀹冧滑濡備綍鏄剧ず銆傚弬瑙6.3鑺傦紝鈥滃鍒跺疄鏂界粏鑺傗銆傚鏋I/O绾跨▼鐘舵佷负Connecting to master锛岄獙璇佷富鏈嶅姟鍣ㄤ笂澶嶅埗鐢ㄦ埛鐨勬潈闄愩佷富鏈嶅姟鍣ㄤ富鏈哄悕銆DNS璁剧疆锛屾槸鍚︿富鏈嶅姟鍣ㄧ湡姝e湪杩愯锛屼互鍙婃槸鍚﹀彲浠ヤ粠浠庡睘鏈嶅姟鍣ㄨ闂

         濡傛灉浠庢湇鍔″櫒浠ュ墠鍦ㄨ繍琛屼絾鏄幇鍦ㄥ凡缁忓仠姝紝鍘熷洜閫氬父鏄湪涓绘湇鍔″櫒涓婃垚鍔熺殑閮ㄥ垎璇彞鍦ㄤ粠鏈嶅姟鍣ㄤ笂澶辫触浜嗐傚鏋滀綘姝g‘蹇収浜嗕富鏈嶅姟鍣紝骞朵笖浠庢潵娌℃湁涓嶉氳繃鏈嶅姟鍣ㄧ嚎绋嬩慨鏀逛粠鏈嶅姟鍣ㄤ笂鐨勬暟鎹紝杩欑鐜拌薄涓嶅簲鍙戠敓銆傚鏋滃彂鐢燂紝搴斾负涓涓bug鎴栦綘閬囧埌浜嗕竴涓6.7鑺傦紝鈥滃鍒剁壒鎬у拰宸茬煡闂鈥 鎻忚堪鐨勫凡鐭ョ殑澶嶅埗闄愬埗銆傚鏋滄槸涓涓bug锛屽弬瑙6.11鑺傦紝鈥滈氭姤澶嶅埗缂洪櫡鈥鏌ラ槄濡備綍閫氭姤鐨勮鏄庛

         濡傛灉鏌愪釜鍦ㄤ富鏈嶅姟鍣ㄤ笂鎴愬姛鐨勮鍙ユ嫆缁濆湪浠庢湇鍔″櫒涓婅繍琛岋紝骞朵笖涓嶈兘鎵ц瀹屽叏鐨勬暟鎹簱閲嶆柊鍚屾(鍗冲垹闄や粠鏈嶅姟鍣ㄧ殑鏁版嵁搴撳苟浠庝富鏈嶅姟鍣ㄥ鍒舵柊鐨勫揩鐓)锛屽皾璇曪細

1.    纭畾鏄惁浠庢湇鍔″櫒鐨勮〃涓庝富鏈嶅姟鍣ㄧ殑涓嶅悓銆傚敖鍔涗簡瑙e彂鐢熺殑鍘熷洜銆傜劧鍚庤浠庢湇鍔″櫒鐨勮〃涓庝富鏈嶅姟鍣ㄧ殑涓鏍峰苟杩愯START SLAVE

2.    濡傛灉鍓嶉潰鐨勬楠や笉宸ヤ綔鎴栦笉閫傚悎锛屽敖鍔涗簡瑙f墜鍔ㄦ洿鏂版槸鍚﹀畨鍏(濡傛灉闇瑕)锛岀劧鍚庡拷瑙嗘潵鑷富鏈嶅姟鍣ㄧ殑涓嬩竴涓鍙ャ

3.    濡傛灉浣犵‘瀹氬彲浠ヨ烦杩囨潵鑷富鏈嶅姟鍣ㄧ殑涓嬩竴涓鍙ワ紝鎵ц涓嬮潰鐨勮鍙ワ細

4.                  mysql> SET GLOBAL SQL_slave_SKIP_COUNTER = n
5.                  mysql> START SLAVE

濡傛灉鏉ヨ嚜涓绘湇鍔″櫒鐨勪笅涓涓鍙ヤ笉浣跨敤AUTO_INCREMENTLAST_INSERT_ID()n 鍊煎簲涓1銆傚惁鍒欙紝鍊煎簲涓2銆備娇鐢AUTO_INCREMENTLAST_INSERT_ID()鐨勮鍙ヤ娇鐢ㄥ2鐨勫師鍥犳槸瀹冧滑浠庝富鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇椾腑鍙栦袱涓簨浠躲

6.    濡傛灉浣犵‘淇濅粠鏈嶅姟鍣ㄥ惎鍔ㄦ椂瀹屽ソ鍦颁笌涓绘湇鍔″櫒鍚屾锛屽苟涓旀病鏈夋洿鏂颁粠鏈嶅姟鍣ㄧ嚎绋嬩箣澶栫殑琛紝鍒欏ぇ姒傝寮傛槸鐢变簬bug銆傚鏋滀綘姝h繍琛屾渶杩戠殑鐗堟湰锛岃閫氭姤璇ラ棶棰樸傚鏋滀綘姝h繍琛屾棫鐗堟湰MySQL锛屽敖鍔涘崌绾у埌鏈鏂扮殑浜у搧鐗堟湰銆

6.11. 閫氭姤澶嶅埗缂洪櫡

濡傛灉浣犵‘瀹氭病鏈夌敤鎴烽敊璇紝浣嗗鍒朵粛鐒朵笉宸ヤ綔鎴栦笉绋冲畾锛屽垯鏄悜鎴戜滑鍙戦bug閫氭姤鐨勬椂鍊欎簡銆傛垜浠渶瑕佸敖鍙兘浠庝綘閭e効鑾峰緱鏇村鐨勪俊鎭凡璺熻釜bug銆傝鑺变竴浜涙椂闂村拰鍔姏缂栧啓涓浠藉ソ鐨bug閫氭姤銆

濡傛灉浣犳湁涓涓噸澶嶇殑娴嬭瘯妗堜緥鏉ヨ鏄bug锛岃鎶婂畠杈撳叆鎴戜滑鐨bug鏁版嵁搴擄紝浣嶇疆涓http://bugs.mysql.com/銆傚鏋滀綘鏈変竴涓phantom鈥濋棶棰(涓嶈兘鎸夌収鏈熸湜杩涜澶嶅埗)锛屽垯浣跨敤涓嬮潰鐨勭▼搴忥細

1.    纭鏈寘鎷敤鎴烽敊璇備緥濡傦紝濡傛灉浣犱笉鐢ㄤ粠鏈嶅姟鍣ㄧ嚎绋嬫潵鏇存柊浠庢湇鍔″櫒锛屾暟鎹皢涓嶅悓姝ワ紝骞朵笖浼氶亣鍒板敮涓鐨勯敭鍊艰繚鑳屾洿鏂般傚湪杩欑鎯呭喌涓嬶紝浠庢湇鍔″櫒绾跨▼鍋滄骞剁瓑寰呬綘鎵嬪姩娓呯悊琛ㄤ娇瀹冧滑鍚屾銆杩欎笉鏄鍒堕棶棰樸傝繖鏄竴涓閮ㄦ帴鍙i棶棰橀犳垚澶嶅埗澶辫触銆

2.    --logs-slave-updates--logs-bin閫夐」杩愯浠庢湇鍔″櫒銆傝繖浜涢夐」浣夸粠鏈嶅姟鍣ㄥ皢浠庝富鏈嶅姟鍣ㄦ帴鏀剁殑鏇存柊璁板叆鑷繁鐨勪簩杩涘埗鏃ュ織銆

3.    閲嶆柊璁剧疆澶嶅埗鐘舵佷箣鍓嶄繚瀛樻墍鏈夌殑璇佹嵁銆傚鏋滄垜浠病鏈変俊鎭垨鍙湁绮楃暐鐨勪俊鎭紝鍒欓毦浠ユ垨涓嶅彲鑳借窡韪棶棰樸傚簲鎼滈泦鐨勮瘉鎹负锛

         鎵鏈変富鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇

         鎵鏈変粠鏈嶅姟鍣ㄧ殑浜岃繘鍒舵棩蹇

         浣犲彂鐜伴棶棰樻椂涓绘湇鍔″櫒鐨SHOW MASTER STATUS鐨勮緭鍑

         浣犲彂鐜伴棶棰樻椂涓绘湇鍔″櫒鐨SHOW SLAVE STATUS鐨勮緭鍑

         涓绘湇鍔″櫒鍜屼粠鏈嶅姟鍣ㄧ殑閿欒鏃ュ織

4.    浣跨敤mysqlbinlog妫鏌ヤ簩杩涘埗鏃ュ織銆備笅闈㈠懡浠ゅ簲鏈夊姪浜庡彂鐜版湁闂鐨勬煡璇紝渚嬪锛

5.            shell> mysqlbinlog -j pos_from_slave_status \
6.                       /path/to/log_from_slave_status | head

鎼滈泦浜嗛棶棰樼殑璇佹嵁鍚庯紝棣栧厛浣滀负涓涓祴璇曟渚嬮殧绂诲紑銆傜劧鍚庡皢闂杈撳叆鎴戜滑鐨bug鏁版嵁搴擄紝浣嶇疆涓http://bugs.mysql.com/锛屽簲鎻愪緵灏藉彲鑳藉鐨勪俊鎭

6.12. 澶氭湇鍔″櫒澶嶅埗涓殑Auto-Increment

褰撳皢澶氫釜鏈嶅姟鍣ㄩ厤缃负澶嶅埗涓绘湇鍔″櫒鏃讹紝浣跨敤auto_increment鏃跺簲閲囧彇鐗规畩姝ラ浠ラ槻姝㈤敭鍊煎啿绐侊紝鍚﹀垯鎻掑叆琛屾椂澶氫釜涓绘湇鍔″櫒浼氳瘯鍥句娇鐢ㄧ浉鍚岀殑auto_increment鍊笺

鏈嶅姟鍣ㄥ彉閲auto_increment_incrementauto_increment_offset鍙互甯姪鍗忚皟澶氫富鏈嶅姟鍣ㄥ鍒跺拰AUTO_INCREMENT鍒椼傛瘡涓彉閲忔湁涓涓粯璁ょ殑(骞朵笖鏄渶灏忕殑)1锛屾渶澶у间负65,535

灏嗚繖浜涘彉閲忚缃负闈炲啿绐佺殑鍊硷紝褰撳湪鍚屼竴涓〃涓绘彃鍏ユ柊琛屾椂锛屽涓绘湇鍔″櫒閰嶇疆涓荤殑鏈嶅姟鍣ㄥ皢涓嶄細涓AUTO_INCREMENT鍊煎啿绐併

杩欎袱涓彉閲忚繖鏍峰奖鍝AUTO_INCREMENT鍒楋細

         auto_increment_increment鎺у埗鍒楀煎鍔犵殑闂撮殧銆備緥濡傦細

                mysql> SHOW VARIABLES LIKE 'auto_inc%';
                +--------------------------+-------+
                | Variable_name            | Value |
                +--------------------------+-------+
                | auto_increment_increment | 1     |
                | auto_increment_offset    | 1     |
                +--------------------------+-------+
                2 rows in set (0.00 sec)
                 
                mysql> CREATE TABLE autoinc1 (col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
                Query OK, 0 rows affected (0.04 sec)
                 
                mysql> SET @auto_increment_increment=10;
                Query OK, 0 rows affected (0.00 sec)
                 
                mysql> SHOW VARIABLES LIKE 'auto_inc%';
                +--------------------------+-------+
                | Variable_name            | Value |
                +--------------------------+-------+
                | auto_increment_increment | 10    |
                | auto_increment_offset    | 1     |
                +--------------------------+-------+
                2 rows in set (0.01 sec)
                 
                mysql> INSERT INTO autoinc1 VALUES (NULL), (NULL), (NULL), (NULL);
                Query OK, 4 rows affected (0.00 sec)
                Records: 4  Duplicates: 0  Warnings: 0
                 
                mysql> SELECT col FROM autoinc1;
                +-----+
                | col |
                +-----+
                |   1 |
                |  11 |
                |  21 |
                |  31 |
                +-----+
                4 rows in set (0.00 sec)

(杩欓噷娉ㄦ槑濡備綍浣跨敤SHOW VARIABLES浠ヨ幏寰楄繖浜涘彉閲忕殑褰撳墠鍊硷級

         auto_increment_offset纭畾AUTO_INCREMENT鍒楀肩殑璧风偣銆傚奖鍝嶅埌鍦ㄥ鍒惰缃富鍙互鏈夊灏戜富鏈嶅姟鍣(渚嬪灏嗚鍊艰缃负10琛ㄧず璁剧疆鍙互鏀寔10涓湇鍔″櫒)

鑰冭檻涓嬮潰鐨勫懡浠わ紝鍋囧畾鍦ㄥ墠闈㈡墍绀虹ず渚嬩腑鐨勭浉鍚岀殑浼氳瘽涓墽琛岃繖浜涘懡浠わ細

mysql> SET @auto_increment_offset=5;
Query OK, 0 rows affected (0.00 sec)
 
mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 5     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> CREATE TABLE autoinc2 (col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.06 sec)
 
mysql> INSERT INTO autoinc2 VALUES (NULL), (NULL), (NULL), (NULL);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
 
mysql> SELECT col FROM autoinc2;
+-----+
| col |
+-----+
|   5 |
|  15 |
|  25 |
|  35 |
+-----+
4 rows in set (0.02 sec)

璇︾粏淇℃伅鍙傝5.3.3鑺傦紝鈥滄湇鍔″櫒绯荤粺鍙橀噺鈥


杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆