MySQL+MMM实现MySQL高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL+MMM实现MySQL高可用相关的知识,希望对你有一定的参考价值。
涓銆MMM绠浠嬶細
MMM鍗Multi-Master Replication Manager for mysql:mysql澶氫富澶嶅埗绠$悊鍣,鍩轰簬perl瀹炵幇,鍏充簬mysql涓讳富澶嶅埗閰嶇疆鐨勭洃鎺с佹晠闅滆浆绉诲拰绠$悊鐨勪竴濂楀彲浼哥缉鐨勮剼鏈浠讹紙鍦ㄤ换浣曟椂鍊欏彧鏈変竴涓妭鐐瑰彲浠ヨ鍐欏叆锛夛紝MMM涔熻兘瀵逛粠鏈嶅姟鍣ㄨ繘琛岃璐熻浇鍧囪 锛屾墍浠ュ彲浠ョ敤瀹冩潵鍦ㄤ竴缁勭敤浜庡鍒剁殑鏈嶅姟鍣ㄥ惎鍔ㄨ櫄鎷ip锛岄櫎姝や箣澶栵紝瀹冭繕鏈夊疄鐜版暟鎹浠姐佽妭鐐逛箣闂撮噸鏂板悓姝ュ姛鑳界殑鑴氭湰銆MySQL鏈韩娌℃湁鎻愪緵replication failover鐨勮В鍐虫柟妗堬紝閫氳繃MMM鏂规鑳藉疄鐜版湇鍔″櫒鐨勬晠闅滆浆绉伙紝浠庤屽疄鐜mysql鐨勯珮鍙敤銆MMM涓嶄粎鑳芥彁渚涙诞鍔IP鐨勫姛鑳斤紝濡傛灉褰撳墠鐨勪富鏈嶅姟鍣ㄦ寕鎺夊悗锛屼細灏嗕綘鍚庣鐨勪粠鏈嶅姟鍣ㄨ嚜鍔ㄨ浆鍚戞柊鐨勪富鏈嶅姟鍣ㄨ繘琛屽悓姝ュ鍒讹紝涓嶇敤鎵嬪伐鏇存敼鍚屾閰嶇疆銆傝繖涓柟妗堟槸鐩墠姣旇緝鎴愮啛鐨勮В鍐虫柟妗堛璇︽儏璇风湅瀹樼綉锛http://mysql-mmm.org
浼樼偣锛楂樺彲鐢ㄦэ紝鎵╁睍鎬уソ锛屽嚭鐜版晠闅滆嚜鍔ㄥ垏鎹紝瀵逛簬涓讳富鍚屾锛屽湪鍚屼竴鏃堕棿鍙彁渚涗竴鍙版暟鎹簱鍐欐搷浣滐紝淇濊瘉鐨勬暟鎹殑涓鑷存с褰撲富鏈嶅姟鍣ㄦ寕鎺変互鍚庯紝鍙︿竴涓富绔嬪嵆鎺ョ锛屽叾浠栫殑浠庢湇鍔″櫒鑳借嚜鍔ㄥ垏鎹紝涓嶇敤浜哄伐骞查銆
缂虹偣锛monitor鑺傜偣鏄崟鐐癸紝涓嶈繃杩欎釜浣犱篃鍙互缁撳悎keepalived鎴栬haertbeat鍋氭垚楂樺彲鐢;鑷冲皯涓変釜鑺傜偣锛屽涓绘満鐨勬暟閲忔湁瑕佹眰锛岄渶瑕佸疄鐜拌鍐欏垎绂,杩橀渶瑕佸湪鍓嶇缂栧啓璇诲啓鍒嗙绋嬪簭銆傚湪璇诲啓闈炲父绻佸繖鐨勪笟鍔$郴缁熶笅琛ㄧ幇涓嶆槸寰堢ǔ瀹氾紝鍙兘浼氬嚭鐜板鍒跺欢鏃躲佸垏鎹㈠け鏁堢瓑闂銆MMM鏂规骞朵笉澶傚簲浜庡鏁版嵁瀹夊叏鎬ц姹傚緢楂橈紝骞朵笖璇汇佸啓绻佸繖鐨勭幆澧冧腑銆
閫傜敤鍦烘櫙:
MMM鐨勯傜敤鍦烘櫙涓烘暟鎹簱璁块棶閲忓ぇ锛屽苟涓旇兘瀹炵幇璇诲啓鍒嗙鐨勫満鏅
Mmm涓昏鍔熻兘鐢变笅闈笁涓剼鏈彁渚:
mmm_mond 璐熻矗鎵鏈夌殑鐩戞帶宸ヤ綔鐨勭洃鎺у畧鎶よ繘绋嬶紝鍐冲畾鑺傜偣鐨勭Щ闄(mmm_mond杩涚▼瀹氭椂蹇冭烦妫娴嬶紝澶辫触鍒欏皢write ip娴姩鍒板彟澶栦竴鍙master)绛夌瓑
mmm_agentd 杩愯鍦mysql鏈嶅姟鍣ㄤ笂鐨勪唬鐞嗗畧鎶よ繘绋嬶紝閫氳繃绠鍗曡繙绋嬫湇鍔¢泦鎻愪緵缁欑洃鎺ц妭鐐
mmm_control 閫氳繃鍛戒护琛岀鐞mmm_mond杩涚▼
鍦ㄦ暣涓洃绠¤繃绋嬩腑锛岄渶瑕佸湪mysql涓坊鍔犵浉鍏虫巿鏉冪敤鎴凤紝鎺堟潈鐨勭敤鎴峰寘鎷竴涓mmm_monitor鐢ㄦ埛鍜屼竴涓mmm_agent鐢ㄦ埛锛屽鏋滄兂浣跨敤mmm鐨勫浠藉伐鍏峰垯杩樿娣诲姞涓涓mmm_tools鐢ㄦ埛銆
浜屻侀儴缃插疄鏂
锛1锛銆佺幆澧冧粙缁
鎿嶄綔绯荤粺锛CentOS 7.2锛64浣嶏級
鏁版嵁搴撶郴缁燂細MySQL 5.7.13
瑙掕壊 | IP | hostname | Server-id | Write vip | Read vip |
monitor | 192.168.10.100 | monitor1 | 鏃 | ||
Master1 | 192.168.10.101 | master1 | 1 | 192.168.10.2 | |
Master2(backup) | 192.168.10.102 | master2 | 2 | 192.168.10.3 | |
Slave1 | 192.168.10.103 | slave1 | 3 | 192.168.10.4 | |
Slave2 | 192.168.10.104 | slave2 | 4 | 192.168.10.5 |
锛2锛夈鍑嗗宸ヤ綔
1銆鍏抽棴鎵鏈変富鏈虹殑SElinux
2銆閰嶇疆NTP锛淇濊瘉鍚屾鏃堕棿
3銆鍦ㄦ墍鏈変富鏈轰笂閰嶇疆/etc/hosts鏂囦欢锛屾坊鍔犲涓嬪唴瀹癸細
# vim /etc/hosts 192.168.10.100 monitor1 192.168.10.101 master1 192.168.10.102 master2 192.168.10.103 slave1 192.168.10.104 slave2
4銆鍦ㄦ墍鏈変富鏈轰笂瀹夎perl perl-devel perl-CPAN libart_lgpl.x86_64 rrdtool.x86_64 rrdtool-perl.x86_64鍖
# yum -y install perl-* libart_lgpl.x86_64 rrdtool.x86_64 rrdtool-perl.x86_64
<-娉->锛氫娇鐢centos7鍦ㄧ嚎yum婧愬畨瑁
5銆佸湪鎵鏈変富鏈轰笂瀹夎perl鐨勭浉鍏冲簱
# cpan -i Algorithm::Diff Class::Singleton DBI DBD::mysql Log::Dispatch Log::Log4perl Mail::Send Net::Ping Proc::Daemon Time::HiRes Params::Validate Net::ARP Proc::Daemon Log::Log4perl
浣跨敤浠ヤ笅鍛戒护鏌ョ湅鎴愬姛瀹夎浜嗛偅浜沺erl搴
instmodsh
鎴
perldoc -t perllocal
锛3锛銆閰嶇疆MySQL鍩虹鐜
1銆佺紪杈戞瘡鍙MySQL涓绘満鐨勯厤缃枃浠/etc/my.cnf锛鍦ㄥ叾涓鍒嗗埆鍔犲叆浠ヤ笅鍐呭锛 娉ㄦ剰server-id涓嶈兘閲嶅銆
master1涓绘満锛
log-bin = mysql-bin binlog_format = mixed server-id = 1 relay-log = relay-bin relay-log-index = slave-relay-bin.index log-slave-updates = 1 auto-increment-increment = 2 auto-increment-offset = 1
master2涓绘満锛
log-bin = mysql-bin binlog_format = mixed server-id = 2 relay-log = relay-bin relay-log-index = slave-relay-bin.index log-slave-updates = 1 auto-increment-increment = 2 auto-increment-offset = 2
slave1涓绘満锛
server-id = 3 relay-log = relay-bin relay-log-index = slave-relay-bin.index read_only = 1
slave2涓绘満锛
server-id = 4 relay-log = relay-bin relay-log-index = slave-relay-bin.index read_only = 1
-------------------------------------------------------------------------------------------
2銆鍦ㄥ畬鎴愪簡瀵my.cnf鐨勪慨鏀瑰悗锛岄噸鏂板惎鍔mysql鏈嶅姟
# systemctl restart mysqld
<-娉->锛鎵鏈MySQL涓绘満鐨uuid涓嶈兘涓鏍凤紝淇敼/usr/local/mysql/data/auto.cnf涓殑鍊
3銆佷负鍥涘彴鏁版嵁搴撲富鏈哄缓绔嬮槻鐏瑙勫垯
# firewall-cmd --permanent --add-port=3306/tcp # firewall-cmd --reload
锛4锛夈侀厤缃富浠庡鍒剁幆澧
锛master1鍜master2閰嶇疆鎴愪富涓伙紝slave1鍜slave2閰嶇疆鎴master1鐨勪粠锛
1銆鍦master1鍜master2涓婃巿鏉冿細
mysql> grant replication slave on *.* to [email protected]‘192.168.10.%‘ identified by ‘123456‘;
2銆佸皢master2銆slave1鍜slave2閰嶇疆鎴master1鐨勪粠搴擄細
鍦master1涓婃墽琛show master status; 鑾峰彇binlog鏂囦欢鍜Position鐐
mysql> show master status;
鍦master2銆slave1鍜slave2鎵ц
mysql> change master to master_host=‘192.168.10.101‘,master_port=3306,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=452; mysql>start slave;
3銆楠岃瘉涓讳粠澶嶅埗
master2涓绘満锛
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.101 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
slave1涓绘満锛
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.101 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
slave2涓绘満锛
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.101 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
濡傛灉Slave_IO_Running鍜Slave_SQL_Running閮戒负YES锛岄偅涔堜富浠庡氨宸茬粡閰嶇疆OK浜
4銆鎶master1閰嶇疆鎴master2鐨勪粠搴:
鍦master2涓婃墽琛show master status ;鑾峰彇binlog鏂囦欢鍜Position鐐
mysql> show master status;
鍦master1涓婃墽琛岋細
mysql> change master to master_host=‘192.168.10.102‘,master_port=3306,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=452; mysql> start slave;
楠岃瘉涓讳粠澶嶅埗锛
master1涓绘満:
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.102 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 452 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
濡傛灉Slave_IO_Running鍜Slave_SQL_Running閮戒负YES锛岄偅涔堜富浠庡氨宸茬粡閰嶇疆OK浜
锛5锛銆mysql-mmm閰嶇疆锛
1銆鍦mysql1涓MySQL鏈嶅姟鍣ㄤ笂鍒涘缓鐩稿叧鐢ㄦ埛锛
<-娉->锛鍥犱负鐜板湪涓轰富浠庡鍒跺叧绯伙紝鎵鏈夊彧闇瑕佸湪master1涓婂垱寤哄氨鍙互锛屽彟澶3鍙颁粠鏈嶅姟鍣ㄩ兘浼氬鍒master1涓婄殑鎿嶄綔
鍒涘缓浠g悊璐﹀彿锛
mysql> grant super,replication client,process on *.* to ‘mmm_agent‘@‘192.168.10.%‘ identified by ‘123456‘;
鍒涘缓鐩戞帶璐﹀彿锛
mysql> grant replication client on *.* to ‘mmm_monitor‘@‘192.168. 10.%‘ identified by ‘123456‘;
2銆佷负浜嗙‘淇濋兘鍒涘缓浜嗭紝璇妫鏌master2鍜slave1銆slave2涓夊彴db涓婃槸鍚﹂兘瀛樺湪鐩戞帶鍜屼唬鐞嗚处鍙锛
mysql> select user,host from mysql.user where user in (‘mmm_monitor‘,‘mmm_agent‘);
鐩稿叧鐢ㄦ埛鐨勪綔鐢細
mmm_monitor鐢ㄦ埛锛mmm鐩戞帶鐢ㄤ簬瀵mysql鏈嶅姟鍣ㄨ繘绋嬪仴搴锋鏌
mmm_agent鐢ㄦ埛锛mmm浠g悊鐢ㄦ潵鏇存敼鍙妯″紡锛屽鍒剁殑涓绘湇鍔″櫒绛
锛6锛銆mysql-mmm瀹夎
1銆鍦monitor涓绘満(192.168.31.106) 涓婂畨瑁呯洃鎺х▼搴
#wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz # chmod +x mysql-mmm-2.2.1.tar.gz # tar -zxf mysql-mmm-2.2.1.tar.gz # cd mysql-mmm-2.2.1 # make install
2銆鍦ㄦ暟鎹簱鏈嶅姟鍣(master1銆master2銆slave1銆slave2)涓婂畨瑁呬唬鐞
#wget http://pkgs.fedoraproject.org/repo/pkgs/mysql-mmm/mysql-mmm-2.2.1.tar.gz/f5f8b48bdf89251d3183328f0249461e/mysql-mmm-2.2.1.tar.gz # chmod +x mysql-mmm-2.2.1.tar.gz # tar -zxf mysql-mmm-2.2.1.tar.gz # cd mysql-mmm-2.2.1 # make install
锛7锛銆侀厤缃mmm
1銆缂栧啓閰嶇疆鏂囦欢锛屼簲鍙颁富鏈哄繀椤讳竴鑷达細
# vim /etc/mysql-mmm/mmm_common.conf active_master_role writer #绉瀬鐨刴aster瑙掕壊鐨勬爣绀猴紝鎵鏈夌殑db鏈嶅姟鍣ㄨ寮鍚痳ead_only鍙傛暟锛屽浜巜riter鏈嶅姟鍣ㄧ洃鎺т唬鐞嗕細鑷姩灏唕ead_only灞炴у叧闂 <host default> cluster_interface eno16777736 #缇ら泦鐨勭綉缁滄帴鍙 pid_path /var/run/mmm_agentd.pid #pid璺緞 bin_path /usr/lib/mysql-mmm/ #鍙墽琛屾枃浠惰矾寰 replication_user rep #澶嶅埗鐢ㄦ埛 replication_password 123456 #澶嶅埗鐢ㄦ埛瀵嗙爜 agent_user mmm_agent #浠g悊鐢ㄦ埛 agent_password 123456 #浠g悊鐢ㄦ埛瀵嗙爜 </host> <host master1> #master1鐨刪ost鍚 ip 192.168.10.101#master1鐨刬p mode master #瑙掕壊灞炴э紝master浠h〃鏄富 peer master2 #涓巑aster1瀵圭瓑鐨勬湇鍔″櫒鐨刪ost鍚嶏紝涔熷氨鏄痬aster2鐨勬湇鍔″櫒host鍚 </host> <host master2> #鍜宮aster鐨勬蹇典竴鏍 ip 192.168.10.102 mode master peer master1 </host> <host slave1> #浠庡簱鐨刪ost鍚,濡傛灉瀛樺湪澶氫釜浠庡簱鍙互閲嶅涓鏍风殑閰嶇疆 ip 192.168.10.103 #浠庣殑ip mode slave #slave鐨勮鑹插睘鎬т唬琛ㄥ綋鍓峢ost鏄粠 </host> <host slave2> #鍜宻lave鐨勬蹇典竴鏍 ip 192.168.10.104 mode slave </host> <role writer> #writer瑙掕壊閰嶇疆 hosts master1,master2 #鑳借繘琛屽啓鎿嶄綔鐨勬湇鍔″櫒鐨刪ost鍚嶏紝濡傛灉涓嶆兂鍒囨崲鍐欐搷浣滆繖閲屽彲浠ュ彧閰嶇疆master,杩欐牱涔熷彲浠ラ伩鍏嶅洜涓虹綉缁滃欢鏃惰岃繘琛寃rite鐨勫垏鎹紝浣嗘槸涓鏃aster鍑虹幇鏁呴殰閭d箞褰撳墠鐨凪MM灏辨病鏈墂riter浜嗗彧鏈夊澶栫殑read鎿嶄綔銆 ips 192.168.10.2 #瀵瑰鎻愪緵鐨勫啓鎿嶄綔鐨勮櫄鎷烮P mode exclusive #exclusive浠h〃鍙厑璁稿瓨鍦ㄤ竴涓富锛屼篃灏辨槸鍙兘鎻愪緵涓涓啓鐨処P </role> <role reader> #read瑙掕壊閰嶇疆 hosts master2,slave1,slave2 #瀵瑰鎻愪緵璇绘搷浣滅殑鏈嶅姟鍣ㄧ殑host鍚,褰撶劧杩欓噷涔熷彲浠ユ妸master鍔犺繘鏉 ips 192.168.10.3, 192.168.10.4, 192.168.10.5 #瀵瑰鎻愪緵璇绘搷浣滅殑铏氭嫙ip锛岃繖涓変釜ip鍜宧ost涓嶆槸涓涓瀵瑰簲鐨,骞朵笖ips涔焗osts鐨勬暟鐩篃鍙互涓嶇浉鍚岋紝濡傛灉杩欐牱閰嶇疆鐨勮瘽鍏朵腑涓涓猦osts浼氬垎閰嶄袱涓猧p mode balanced #balanced浠h〃璐熻浇鍧囪 </role>
鍚屾椂灏嗚繖涓枃浠舵嫹璐濆埌鍏跺畠鐨勬湇鍔″櫒锛岄厤缃笉鍙
#for host in master1 master2 slave1 slave2 ; do scp /etc/mysql-mmm/mmm_common.conf $host:/etc/mysql-mmm/ ; done
2銆浠g悊鏂囦欢閰嶇疆
缂栬緫 4鍙mysql鑺傜偣鏈轰笂鐨/etc/mysql-mmm/mmm_agent.conf
# vim /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf thi master1 #璇ュ兼牴鎹嚜韬富鏈哄悕鑰岃缃
<-娉->锛杩欎釜鍙厤缃db鏈嶅姟鍣紝鐩戞帶鏈嶅姟鍣ㄤ笉闇瑕侀厤缃
3銆鍚姩浠g悊杩涚▼
缂栬緫mysql-mmm-agent鑴氭湰鏂囦欢锛鍦#!/bin/sh涓嬮潰鍔犲叆濡備笅鍐呭
# vim /etc/init.d/mysql-mmm-agent source /root/.bash_profile #娣诲姞姝ゅ唴瀹规槸涓轰簡mysql-mmm-agent鏈嶅姟鑳藉惎鏈鸿嚜鍚
娣诲姞鎴愮郴缁熸湇鍔″苟璁剧疆涓鸿嚜鍚姩
# chkconfig --add mysql-mmm-agent # chkconfig mysql-mmm-agent on # /etc/init.d/mysql-mmm-agent start Daemon bin: ‘/usr/sbin/mmm_agentd‘ Daemon pid: ‘/var/run/mmm_agentd.pid‘ Starting MMM Agent daemon... Ok # netstat -antp | grep mmm_agentd tcp 0 0 192.168.10.101:9989 0.0.0.0:* LISTEN 9693/mmm_agentd
4銆閰嶇疆闃茬伀澧
# firewall-cmd --permanent --add-port=9989/tcp # firewall-cmd --reload
5銆缂栬緫 monitor涓绘満涓婄殑mmm_mon.conf 閰嶇疆
# vim /etc/mysql-mmm/mmm_mon.conf includemmm_common.conf <monitor> ip 127.0.0.1 #涓轰簡瀹夊叏鎬э紝璁剧疆鍙湪鏈満鐩戝惉锛宮mm_mond榛樿鐩戝惉9988 pid_path /var/run/mmm_mond.pid bin_path /usr/lib/mysql-mmm/ status_path /var/lib/misc/mmm_mond.status ping_ips 192.168.10.101, 192.168.10.102, 192.168.10.103,192.168.10.104 #鐢ㄤ簬娴嬭瘯缃戠粶鍙敤鎬 IP 鍦板潃鍒楄〃锛屽彧瑕佸叾涓湁涓涓湴鍧 ping 閫氾紝灏变唬琛ㄧ綉缁滄甯革紝杩欓噷涓嶈鍐欏叆鏈満鍦板潃 auto_set_online 0 #璁剧疆鑷姩online鐨勬椂闂达紝榛樿鏄秴杩60s灏卞皢瀹冭缃负online锛岄粯璁ゆ槸60s锛岃繖閲屽皢鍏惰涓0灏辨槸绔嬪嵆online </monitor> <check default> check_period 5 #妫鏌ュ懆鏈熼粯璁や负5s trap_period 10 #涓涓妭鐐硅妫娴嬩笉鎴愬姛鐨勬椂闂存寔缁璽rap_period绉掞紝灏辨厧閲嶇殑璁や负杩欎釜鑺傜偣澶辫触浜,榛樿鍊硷細10s timeout 2 #妫鏌ヨ秴鏃剁殑鏃堕棿锛岄粯璁ゅ硷細2s restart_after 10000 #鍦ㄥ畬鎴恟estart_after娆℃鏌ュ悗锛岄噸鍚痗hecker杩涚▼榛樿10000 max_backlog 86400 #璁板綍妫鏌ep_backlog鏃ュ織鐨勬渶澶ф鏁帮紝榛樿鍊硷細60 </check> <host default> monitor_user mmm_monitor #鐩戞帶db鏈嶅姟鍣ㄧ殑鐢ㄦ埛 monitor_password 123456 #鐩戞帶db鏈嶅姟鍣ㄧ殑瀵嗙爜 </host> debug 0 #debug 0姝e父妯″紡锛1涓篸ebug妯″紡
6銆鍚姩鐩戞帶杩涚▼锛
缂栬緫mysql-mmm-monitor鑴氭湰鏂囦欢锛鍦#!/bin/sh涓嬮潰鍔犲叆濡備笅鍐呭
# vim /etc/init.d/mysql-mmm-monitor source /root/.bash_profile
娣诲姞鎴愮郴缁熸湇鍔″苟璁剧疆涓鸿嚜鍚姩
#chkconfig --add mysql-mmm-monitor #chkconfig mysql-mmm-monitor on #/etc/init.d/mysql-mmm-monitor start Daemon bin: ‘/usr/sbin/mmm_mond‘ Daemon pid: ‘/var/run/mmm_mond.pid‘ Starting MMM Monitor daemon: Ok [[email protected] ~]# netstat -anpt | grep 9988 tcp 0 0 127.0.0.1:9988 0.0.0.0:* LISTEN 8546/mmm_mond
<-娉->锛鏃犺鏄湪db绔繕鏄湪鐩戞帶绔鏋滄湁瀵归厤缃枃浠惰繘琛屼慨鏀规搷浣滈兘闇瑕侀噸鍚唬鐞嗚繘绋嬪拰鐩戞帶杩涚▼銆MMM鍚姩椤哄簭涓鍏堝惎鍔monitor锛屽啀鍚姩 agent
7銆妫鏌ラ泦缇ょ姸鎬侊細
[[email protected] ~]# mmm_control show master1(192.168.10.101) master/ONLINE. Roles: writer(192.168.10.2) master2(192.168.10.102) master/ONLINE. Roles: reader(192.168.10.4) slave1(192.168.10.103) slave/ONLINE. Roles: reader(192.168.10.3) slave2(192.168.10.104) slave/ONLINE. Roles: reader(192.168.10.5)
<-娉->锛濡傛灉鏈嶅姟鍣ㄧ姸鎬佷笉鏄ONLINE锛屽彲浠ョ敤濡備笅鍛戒护灏嗘湇鍔″櫒涓婄嚎锛屼緥濡傦細
[[email protected] ~]#mmm_control set_online master1 [[email protected] ~]#mmm_control set_online master2 [[email protected] ~]#mmm_control set_online slave1 [[email protected] ~]#mmm_control set_online slave2
浠涓婇潰鐨勬樉绀鍙互鐪嬪埌锛屽啓璇锋眰鐨VIP鍦master1涓婏紝鎵鏈変粠鑺傜偣涔熼兘鎶master1褰撳仛涓昏妭鐐广
8銆鏌ョ湅鏄惁鍚敤vip
[[email protected] ~]# ip addr show dev eno16777736 3: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:82:86:c8 brd ff:ff:ff:ff:ff:ff inet 192.168.10.101/24 brd 192.168.10.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.10.2/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe82:86c8/64 scope link valid_lft forever preferred_lft forever
[[email protected] ~]# ip addr show dev eno16777736 3: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:6e:b1:23 brd ff:ff:ff:ff:ff:ff inet 192.168.10.102/24 brd 192.168.10.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.10.4/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe6e:b123/64 scope link valid_lft forever preferred_lft forever
[[email protected] ~]# ip addr show dev eno16777736 3: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:d4:84:54 brd ff:ff:ff:ff:ff:ff inet 192.168.10.103/24 brd 192.168.10.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.10.3/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fed4:8454/64 scope link valid_lft forever preferred_lft forever
[[email protected] ~]# ip addr show dev eno16777736 3: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:29:80:59 brd ff:ff:ff:ff:ff:ff inet 192.168.10.104/24 brd 192.168.10.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.10.5/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe29:8059/64 scope link valid_lft forever preferred_lft forever
鍦master2锛slave1锛slave2涓绘満涓婃煡鐪嬩富mysql鐨勬寚鍚
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.101 Master_User: rep Master_Port: 3306 Connect_Retry: 60
锛8锛夈MMM楂樺彲鐢ㄦф祴璇曪細
鏈嶅姟鍣ㄨ鍐欓噰鏈VIP鍦板潃杩涜璇诲啓锛屽嚭鐜版晠闅滄椂VIP浼氭紓绉诲埌鍏跺畠鑺傜偣锛岀敱鍏跺畠鑺傜偣鎻愪緵鏈嶅姟銆
棣栧厛鏌ョ湅鏁翠釜闆嗙兢鐨勭姸鎬侊紝鍙互鐪嬪埌鏁翠釜闆嗙兢鐘舵佹甯
[[email protected] ~]# mmm_control show master1(192.168.10.101) master/ONLINE. Roles: writer(192.168.10.2) master2(192.168.10.102) master/ONLINE. Roles: reader(192.168.10.4) slave1(192.168.10.103) slave/ONLINE. Roles: reader(192.168.10.3) slave2(192.168.10.104) slave/ONLINE. Roles: reader(192.168.10.5)
妯℃嫙master1瀹曟満锛屾墜鍔ㄥ仠姝mysql鏈嶅姟锛岃瀵monitor鏃ュ織,master1鐨勬棩蹇楀涓嬶細
[[email protected] ~]# tail -f /var/log/mysql-mmm/mmm_mond.log 2017/03/31 15:43:00 FATAL State of host ‘master1‘ changed from ONLINE to HARD_OFFLINE (ping: OK, mysql: not OK) 2017/03/31 15:43:00 INFO Removing all roles from host ‘master1‘: 2017/03/31 15:43:00 INFO Removed role ‘writer(192.168.10.2)‘ from host ‘master1‘ 2017/03/31 15:43:00 INFO Orphaned role ‘writer(192.168.10.2)‘ has been assigned to ‘master2‘
浠庝互涓婁俊鎭腑鍙互鐪嬪嚭鏉ワ紝master1鍚満鍚庯紝鑷姩灏writer瑙掕壊杞Щ缁master2
鏌ョ湅缇ら泦鐨勬渶鏂扮姸鎬
[[email protected] ~]# mmm_control show master1(192.168.10.101) master/HARD_OFFLINE. Roles: master2(192.168.10.102) master/ONLINE. Roles: reader(192.168.10.4), writer(192.168.10.2) slave1(192.168.10.103) slave/ONLINE. Roles: reader(192.168.10.3) slave2(192.168.10.104) slave/ONLINE. Roles: reader(192.168.10.5)
浠庢樉绀虹粨鏋滃彲浠ョ湅鍑master1鐨勭姸鎬佹湁ONLINE杞崲涓HARD_OFFLINE锛屽啓VIP杞Щ鍒颁簡master2涓绘満涓娿
妫鏌ユ墍鏈夌殑db鏈嶅姟鍣ㄧ兢闆嗙姸鎬
[[email protected] ~]# mmm_control checks all master1 ping [last change: 2017/03/31 15:28:30] OK master1 mysql [last change: 2017/03/31 15:43:00] ERROR: Connect error (host = 192.168.10.101:3306, user = mmm_monitor)! Can‘t connect to MySQL server on ‘192.168.10.101‘ (111) master1 rep_threads [last change: 2017/03/31 15:28:30] OK master1 rep_backlog [last change: 2017/03/31 15:28:30] OK: Backlog is null slave1 ping [last change: 2017/03/31 15:28:30] OK slave1 mysql [last change: 2017/03/31 15:28:30] OK slave1 rep_threads [last change: 2017/03/31 15:28:30] OK slave1 rep_backlog [last change: 2017/03/31 15:28:30] OK: Backlog is null master2 ping [last change: 2017/03/31 15:28:30] OK master2 mysql [last change: 2017/03/31 15:28:30] OK master2 rep_threads [last change: 2017/03/31 15:28:30] OK master2 rep_backlog [last change: 2017/03/31 15:28:30] OK: Backlog is null slave2 ping [last change: 2017/03/31 15:28:30] OK slave2 mysql [last change: 2017/03/31 15:28:30] OK slave2 rep_threads [last change: 2017/03/31 15:28:30] OK slave2 rep_backlog [last change: 2017/03/31 15:28:30] OK: Backlog is null
浠庝笂闈㈠彲浠ョ湅鍒master1鑳ping閫氾紝璇存槑鍙槸鏈嶅姟姝绘帀浜嗐
鏌ョ湅master2涓绘満鐨ip鍦板潃锛
[[email protected] ~]# ip addr show dev eno16777736 3: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:6e:b1:23 brd ff:ff:ff:ff:ff:ff inet 192.168.10.102/24 brd 192.168.10.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.10.4/32 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.10.2/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe6e:b123/64 scope link valid_lft forever preferred_lft forever
slave1涓绘満锛
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.102 Master_User: rep Master_Port: 3306
slave2涓绘満锛
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.102 Master_User: rep Master_Port: 3306
鍚姩master1涓绘満鐨mysql鏈嶅姟锛瑙傚療monitor鏃ュ織,master1鐨勬棩蹇楀涓嬶細
[[email protected] ~]# tail -f /var/log/mysql-mmm/mmm_mond.log 2017/03/31 15:54:46 INFO Check ‘rep_backlog‘ on ‘master1‘ is ok! 2017/03/31 15:54:46 INFO Check ‘rep_threads‘ on ‘master1‘ is ok! 2017/03/31 15:54:50 INFO Check ‘mysql‘ on ‘master1‘ is ok! 2017/03/31 15:54:53 FATAL State of host ‘master1‘ changed from HARD_OFFLINE to AWAITING_RECOVERY
浠庝笂闈㈠彲浠ョ湅鍒master1鐨勭姸鎬佺敱hard_offline鏀瑰彉涓awaiting_recovery鐘舵
鐢ㄥ涓嬪懡浠ゅ皢鏈嶅姟鍣ㄤ笂绾匡細
[[email protected] ~]#mmm_control set_online master1
鏌ョ湅缇ら泦鏈鏂扮姸鎬
[[email protected] ~]# mmm_control show master1(192.168.10.101) master/ONLINE. Roles: master2(192.168.10.102) master/ONLINE. Roles: reader(192.168.10.4), writer(192.168.10.2) slave1(192.168.10.103) slave/ONLINE. Roles: reader(192.168.10.3) slave2(192.168.10.104) slave/ONLINE. Roles: reader(192.168.10.5)
鍙互鐪嬪埌涓诲簱鍚姩涓嶄細鎺ョ涓伙紝鍙埌鐜版湁鐨勪富鍐嶆瀹曟満銆
涓夈鎬荤粨
锛1锛master2澶囬変富鑺傜偣瀹曟満涓嶅奖鍝嶉泦缇ょ殑鐘舵侊紝灏辨槸绉婚櫎浜master2澶囬夎妭鐐圭殑璇荤姸鎬併
锛2锛master1涓昏妭鐐瑰畷鏈猴紝鐢master2澶囬変富鑺傜偣鎺ョ鍐欒鑹诧紝slave1,slave2鎸囧悜鏂master2涓诲簱杩涜澶嶅埗锛slave1,slave2浼氳嚜鍔change master鍒master2.
锛3锛濡傛灉master1涓诲簱瀹曟満锛master2澶嶅埗搴旂敤鍙堣惤鍚庝簬master1鏃跺氨鍙樻垚浜嗕富鍙啓鐘舵侊紝杩欐椂鐨勬暟鎹富鏃犳硶淇濊瘉涓鑷存с
濡傛灉master2,slave1,slave2寤惰繜浜master1涓伙紝杩欎釜鏃master1瀹曟満锛slave1,slave2灏嗕細绛夊緟鏁版嵁杩戒笂db1鍚庯紝鍐嶉噸鏂版寚鍚戞柊鐨勪富node2杩涜澶嶅埗鎿嶄綔锛岃繖鏃剁殑鏁版嵁涔熸棤娉曚繚璇佸悓姝ョ殑涓鑷存с
锛4锛濡傛灉閲囩敤MMM楂樺彲鐢ㄦ灦鏋勶紝涓伙紝涓诲閫夎妭鐐规満鍣ㄩ厤缃竴鏍凤紝鑰屼笖寮鍚崐鍚屾杩涗竴姝ユ彁楂樺畨鍏ㄦф垨閲囩敤MariaDB/mysql5.7杩涜澶氱嚎绋嬩粠澶嶅埗锛屾彁楂樺鍒剁殑鎬ц兘銆
闄勶細
1銆鏃ュ織鏂囦欢锛
鏃ュ織鏂囦欢寰寰鏄垎鏋愰敊璇殑鍏抽敭锛屾墍浠ヨ鍠勪簬鍒╃敤鏃ュ織鏂囦欢杩涜闂鍒嗘瀽銆
db绔細/var/log/mysql-mmm/mmm_agentd.log
鐩戞帶绔細/var/log/mysql-mmm/mmm_mond.log
2銆鍛戒护鏂囦欢锛
mmm_agentd锛db浠g悊杩涚▼鐨勫惎鍔ㄦ枃浠
mmm_mond锛氱洃鎺ц繘绋嬬殑鍚姩鏂囦欢
mmm_backup锛氬浠芥枃浠
mmm_restore锛氳繕鍘熸枃浠
mmm_control锛氱洃鎺ф搷浣滃懡浠ゆ枃浠
db鏈嶅姟鍣ㄧ鍙湁mmm_agentd绋嬪簭锛屽叾瀹冪殑閮芥槸鍦monitor鏈嶅姟鍣ㄧ銆
3銆mmm_control鐢ㄦ硶
mmm_control绋嬪簭鍙互鐢ㄤ簬鐩戞帶缇ら泦鐘舵併佸垏鎹writer銆佽缃online\offline鎿嶄綔绛夈
Valid commands are:
help - show this message #甯姪淇℃伅
ping - ping monitor #ping褰撳墠鐨勭兢闆嗘槸鍚︽甯
show - show status #缇ら泦鍦ㄧ嚎鐘舵佹鏌
checks [<host>|all [<check>|all]] - show checks status#鎵ц鐩戞帶妫鏌ユ搷浣
set_online<host> - set host <host> online #灏host璁剧疆涓online
set_offline<host> - set host <host> offline #灏host璁剧疆涓offline
mode - print current mode. #鎵撳嵃杈撳嚭褰撳墠鐨mode
set_active - switch into active mode.
set_manual - switch into manual mode.
set_passive - switch into passive mode.
move_role [--force] <role><host> - move exclusive role <role> to host <host> #绉婚櫎writer鏈嶅姟鍣ㄤ负鎸囧畾鐨host鏈嶅姟鍣(Only use --force if you know what you are doing!)
set_ip<ip><host> - set role with ip<ip> to host <host>
妫鏌ユ墍鏈夌殑db鏈嶅姟鍣ㄧ兢闆嗙姸鎬锛
[[email protected] ~]# mmm_control checks all
妫鏌ラ」鍖呮嫭锛ping銆mysql鏄惁姝e父杩愯銆佸鍒剁嚎绋嬫槸鍚︽甯哥瓑
妫鏌ョ兢闆嗙幆澧冨湪绾跨姸鍐锛
[[email protected] ~]# mmm_control show
瀵规寚瀹氱殑host鎵цoffline鎿嶄綔锛
[[email protected] ~]# mmm_controlset_offline slave2
瀵规寚瀹氱殑host鎵цonine鎿嶄綔锛
[[email protected] ~]# mmm_controlset_online slave2
鎵цwrite鍒囨崲(鎵嬪姩鍒囨崲):
鏌ョ湅褰撳墠鐨slave瀵瑰簲鐨master
[[email protected] ~]# mysql -uroot -p123456 -e ‘show slave status\G;‘ mysql: [Warning] Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.102
writer鍒囨崲锛岃纭繚mmm_common.conf鏂囦欢涓殑writer灞炴ф湁閰嶇疆瀵瑰簲鐨host,鍚﹀垯鏃犳硶鍒囨崲
[[email protected] ~]# mmm_control move_role writer master1 OK: Role ‘writer‘ has been moved from ‘master2‘ to ‘master1‘. Now you can wait some time and check new roles info! [[email protected] ~]# mmm_control show master1(192.168.10.101) master/ONLINE. Roles: writer(192.168.10.2) master2(192.168.10.102) master/ONLINE. Roles: reader(192.168.10.4) slave1(192.168.10.103) slave/ONLINE. Roles: reader(192.168.10.3) slave2(192.168.10.104) slave/ONLINE. Roles: reader(192.168.10.5)
save浠庡簱鑷姩鍒囨崲鍒颁簡鏂扮殑master
[[email protected] ~]# mysql -uroot -p123456 -e ‘show slave status\G;‘ mysql: [Warning] Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.101
4銆鍏跺畠澶勭悊闂
濡傛灉涓嶆兂璁writer浠master鍒囨崲鍒backup锛堝寘鎷富浠庣殑寤舵椂涔熶細瀵艰嚧鍐VIP鐨勫垏鎹級锛岄偅涔堝彲浠ュ湪閰嶇疆/etc/mysql-mmm/mmm_common.conf鏃讹紝鍘绘帀<role write>涓殑backup
<role writer>#writer瑙掕壊閰嶇疆
hosts master1 #杩欓噷鍙厤缃竴涓Hosts
ips 192.168.10.2#瀵瑰鎻愪緵鐨勫啓鎿嶄綔鐨勮櫄鎷IP
mode exclusive #exclusive浠h〃鍙厑璁稿瓨鍦ㄤ竴涓富锛屼篃灏辨槸鍙兘鎻愪緵涓涓啓鐨IP
</role>
杩欐牱鐨勮瘽褰master1鍑虹幇鏁呴殰浜writer鍐欐搷浣滀笉浼氬垏鎹㈠埌master2鏈嶅姟鍣紝骞朵笖slave涔熶笉浼氭寚鍚戞柊鐨master锛屾鏃跺綋鍓嶇殑MMM涔嬪墠瀵瑰鎻愪緵鍐欐湇鍔°
5銆鎬荤粨
1.瀵瑰鎻愪緵璇诲啓鐨勮櫄鎷IP鏄敱monitor绋嬪簭鎺у埗銆傚鏋monitor娌℃湁鍚姩閭d箞db鏈嶅姟鍣ㄤ笉浼氳鍒嗛厤铏氭嫙ip,浣嗘槸濡傛灉宸茬粡鍒嗛厤濂戒簡铏氭嫙ip锛褰monitor绋嬪簭鍏抽棴浜嗗師鍏堝垎閰嶇殑铏氭嫙ip涓嶄細绔嬪嵆鍏抽棴澶栭儴绋嬪簭杩樺彲浠ヨ繛鎺ヨ闂紙鍙涓嶉噸鍚綉缁滐級锛岃繖鏍风殑濂藉灏辨槸瀵逛簬monitor鐨勫彲闈犳ц姹傚氨浼氫綆涓浜涳紝浣嗘槸濡傛灉杩欎釜鏃跺欏叾涓殑鏌愪竴涓db鏈嶅姟鍣ㄦ晠闅滀簡灏辨棤娉曞鐞嗗垏鎹紝涔熷氨鏄師鍏堢殑铏氭嫙ip杩樻槸缁存寔涓嶅彉锛屾寕鎺夌殑閭e彴DB鐨勮櫄鎷ip浼氬彉鐨勪笉鍙闂
2.agent绋嬪簭鍙monitor绋嬪簭鐨勬帶鍒跺鐞write鍒囨崲锛屼粠搴撳垏鎹㈢瓑鎿嶄綔銆傚鏋monitor杩涚▼鍏抽棴浜嗛偅涔agent杩涚▼灏辫捣涓嶅埌浠涔堜綔鐢紝瀹冩湰韬笉鑳藉鐞嗘晠闅溿
3.monitor绋嬪簭璐熻矗鐩戞帶db鏈嶅姟鍣ㄧ殑鐘舵侊紝鍖呮嫭Mysql鏁版嵁搴撱佹湇鍔″櫒鏄惁杩愯銆佸鍒剁嚎绋嬫槸鍚︽甯搞佷富浠庡欢鏃剁瓑锛涘畠杩樼敤浜庢帶鍒agent绋嬪簭澶勭悊鏁呴殰銆
4.monitor浼氭瘡闅斿嚑绉掗挓鐩戞帶db鏈嶅姟鍣ㄧ殑鐘舵侊紝濡傛灉db鏈嶅姟鍣ㄥ凡缁忎粠鏁呴殰鍙樻垚浜嗘甯革紝閭d箞monitor浼氳嚜鍔ㄥ湪60s涔嬪悗灏嗗叾璁剧疆涓online鐘舵(榛樿鏄60s鍙互璁句负鍏跺畠鐨勫)锛屾湁鐩戞帶绔殑閰嶇疆鏂囦欢鍙傛暟鈥渁uto_set_online鈥鍐冲畾锛岀兢闆嗘湇鍔″櫒鐨勭姸鎬佹湁涓夌鍒嗗埆鏄細HARD_OFFLINE鈫扐WAITING_RECOVERY鈫抩nline
5.榛樿monitor浼氭帶鍒mmm_agent浼氬皢writer db鏈嶅姟鍣read_only淇敼涓OFF锛屽叾瀹冪殑db鏈嶅姟鍣read_only淇敼涓ON,鎵浠ヤ负浜嗕弗璋ㄥ彲浠ュ湪鎵鏈夌殑鏈嶅姟鍣ㄧ殑my.cnf鏂囦欢涓姞鍏read_only=1鐢monitor鎺у埗鏉ユ帶鍒writer鍜read,root鐢ㄦ埛鍜屽鍒剁敤鎴蜂笉鍙read_only鍙傛暟鐨勫奖鍝銆
本文出自 “乌云科技” 博客,请务必保留此出处http://wuyunkeji.blog.51cto.com/12028005/1912261
以上是关于MySQL+MMM实现MySQL高可用的主要内容,如果未能解决你的问题,请参考以下文章