本篇內(nèi)容主要講解“MHA搭建及故障維護(hù)的方法是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“MHA搭建及故障維護(hù)的方法是什么”吧!
為丹鳳等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及丹鳳網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、丹鳳網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
CentOS 7改主機(jī)名
hostnamectl set-hostname master 192.168.56.121 master 192.168.56.122 slave1 #備用master 192.168.56.123 slave2 192.168.56.124 manager
將ip和域名配置到/etc/hosts文件中
嘗試在各主機(jī)上的防火墻上加上端口的允許
iptables -I INPUT -s 0/0 -p tcp --dport 3306 -j ACCEPT
這條規(guī)則的意思是,想要在輸入數(shù)據(jù)INPUT中,protocol為tcp/IP的方式,訪(fǎng)問(wèn)端口3306,都會(huì)被允許的
iptables -L -n|grep 3306 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
[master,slave1,slave2,manager]
ssh-keygen -t rsa
[slave1,slave2,manager]
scp .ssh/id_rsa.pub master:/root/.ssh/slave1.pub scp .ssh/id_rsa.pub master:/root/.ssh/slave2.pub scp .ssh/id_rsa.pub master:/root/.ssh/manager.pub
[master]
cat ~/.ssh/*.pub>>~/.ssh/authorized_keys scp ~/.ssh/authorized_keys slave1:/root/.ssh/authorized_keys scp ~/.ssh/authorized_keys slave2:/root/.ssh/authorized_keys scp ~/.ssh/authorized_keys manager:/root/.ssh/authorized_keys
[manager,master,slave1,slave2] yum -y install perl-DBD-MySQL yum -y install perl-Config-Tiny yum -y install perl-Log-Dispatch yum -y install perl-Parallel-ForkManager mha4mysql-node-0.55-0.el6.noarch.rpm
[manager] yum -y install perl yum -y install cpan rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm
缺啥,yum install xxx 啥就行。
略
[manager]
1.創(chuàng)建目錄
mkdir -p /masterha/app1 mkdir /etc/masterha vi /etc/masterha/app1.cnf [server default] user=root password=root manager_workdir=/masterha/app1 manager_log=/masterha/app1/manager.log remote_workdir=/masterha/app1 ssh_user=root repl_user=rep repl_password=repl ping_interval=1 [server1] hostname=192.168.56.122 master_binlog_dir=/var/lib/mysql candidate_master=1 #relay_log_purge=0 [server2] hostname=192.168.56.121 master_binlog_dir=/var/lib/mysql candidate_master=1 [server3] hostname=192.168.56.123 master_binlog_dir=/var/lib/mysql no_master=1 #relay_log_purge=0
[manager] masterha_check_ssh --conf=/etc/masterha/app1.cnf [root@manager ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf Thu Feb 23 12:00:24 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Thu Feb 23 12:00:24 2017 - [info] Reading application default configurations from /etc/masterha/app1.cnf.. Thu Feb 23 12:00:24 2017 - [info] Reading server configurations from /etc/masterha/app1.cnf.. Thu Feb 23 12:00:24 2017 - [info] Starting SSH connection tests.. Thu Feb 23 12:00:25 2017 - [debug] Thu Feb 23 12:00:24 2017 - [debug] Connecting via SSH from root@192.168.56.122(192.168.56.122:22) to root@192.168.56.121(192.168.56.121:22).. Thu Feb 23 12:00:25 2017 - [debug] ok. Thu Feb 23 12:00:25 2017 - [debug] Connecting via SSH from root@192.168.56.122(192.168.56.122:22) to root@192.168.56.123(192.168.56.123:22).. Thu Feb 23 12:00:25 2017 - [debug] ok. Thu Feb 23 12:00:25 2017 - [debug] Thu Feb 23 12:00:25 2017 - [debug] Connecting via SSH from root@192.168.56.121(192.168.56.121:22) to root@192.168.56.122(192.168.56.122:22).. Warning: Permanently added '192.168.56.121' (ECDSA) to the list of known hosts. Thu Feb 23 12:00:25 2017 - [debug] ok. Thu Feb 23 12:00:25 2017 - [debug] Connecting via SSH from root@192.168.56.121(192.168.56.121:22) to root@192.168.56.123(192.168.56.123:22).. Thu Feb 23 12:00:25 2017 - [debug] ok. Thu Feb 23 12:00:26 2017 - [debug] Thu Feb 23 12:00:25 2017 - [debug] Connecting via SSH from root@192.168.56.123(192.168.56.123:22) to root@192.168.56.122(192.168.56.122:22).. Warning: Permanently added '192.168.56.123' (ECDSA) to the list of known hosts. Thu Feb 23 12:00:26 2017 - [debug] ok. Thu Feb 23 12:00:26 2017 - [debug] Connecting via SSH from root@192.168.56.123(192.168.56.123:22) to root@192.168.56.121(192.168.56.121:22).. Thu Feb 23 12:00:26 2017 - [debug] ok. Thu Feb 23 12:00:26 2017 - [info] All SSH connection tests passed successfully. [root@manager ~]#
[manager] masterha_check_repl --conf=/etc/masterha/app1.cnf [root@manager mysql]# masterha_check_repl --conf=/etc/masterha/app1.cnf Thu Feb 23 14:37:05 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Thu Feb 23 14:37:05 2017 - [info] Reading application default configurations from /etc/masterha/app1.cnf.. Thu Feb 23 14:37:05 2017 - [info] Reading server configurations from /etc/masterha/app1.cnf.. Thu Feb 23 14:37:05 2017 - [info] MHA::MasterMonitor version 0.55. Thu Feb 23 14:37:05 2017 - [info] Dead Servers: Thu Feb 23 14:37:05 2017 - [info] Alive Servers: Thu Feb 23 14:37:05 2017 - [info] master(192.168.56.121:3306) Thu Feb 23 14:37:05 2017 - [info] slave1(192.168.56.122:3306) Thu Feb 23 14:37:05 2017 - [info] slave2(192.168.56.123:3306) Thu Feb 23 14:37:05 2017 - [info] Alive Slaves: .......此處省略 Thu Feb 23 14:37:08 2017 - [info] Connecting to root@192.168.56.123(slave2:22).. Creating directory /masterha/app1.. done. Checking slave recovery environment settings.. Opening /var/lib/mysql/relay-log.info ... ok. Relay log found at /tmp, up to mysql-relay-bin.000004 Temporary relay log file is /tmp/mysql-relay-bin.000004 Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Thu Feb 23 14:37:08 2017 - [info] Slaves settings check done. Thu Feb 23 14:37:08 2017 - [info] master (current master) +--slave1 +--slave2 Thu Feb 23 14:37:08 2017 - [info] Checking replication health on slave1.. Thu Feb 23 14:37:08 2017 - [info] ok. Thu Feb 23 14:37:08 2017 - [info] Checking replication health on slave2.. Thu Feb 23 14:37:08 2017 - [info] ok. Thu Feb 23 14:37:08 2017 - [warning] master_ip_failover_script is not defined. Thu Feb 23 14:37:08 2017 - [warning] shutdown_script is not defined. Thu Feb 23 14:37:08 2017 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK.
[manager] masterha_manager --conf=/etc/masterha/app1.cnf tail -f /masterha/app1/manager.log
[master] [root@master ~]# service mysql stop Shutting down MySQL..... SUCCESS! [root@master ~]# [manager]
tail -f /masterha/app1/manager.log
日志文件顯示:
----- Failover Report ----- app1: MySQL Master failover master to slave1 succeeded Master master is down! Check MHA Manager logs at manager:/masterha/app1/manager.log for details. Started automated(non-interactive) failover. The latest slave slave1(192.168.56.122:3306) has all relay logs for recovery. Selected slave1 as a new master. slave1: OK: Applying all logs succeeded. slave2: This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. slave2: OK: Applying all logs succeeded. Slave started, replicating from slave1. slave1: Resetting slave info succeeded. Master failover to slave1(192.168.56.122:3306) completed successfully.
上面的結(jié)果表明master成功切換。
1.切換過(guò)程會(huì)自動(dòng)把read_only關(guān)閉
2.切換之后需要?jiǎng)h除手工刪除/masterha/app1/app1.failover.complete,才能進(jìn)行第二次測(cè)試
3.一旦發(fā)生切換管理進(jìn)程將會(huì)退出,無(wú)法進(jìn)行再次測(cè)試,需將故障數(shù)據(jù)庫(kù)加入到MHA環(huán)境中來(lái)
4.原主節(jié)點(diǎn)重新加入到MHA時(shí)只能設(shè)置為slave,在
change master to master_host='192.168.56.122', master_user='repl', master_password='repl', master_log_file='mysql-bin.000010', master_log_pos=120;
之前需要先 reset slave
5.關(guān)于ip地址的接管有幾種方式,這里采用的是MHA自動(dòng)調(diào)用IP別名的方式,好處是在能夠保證數(shù)據(jù)庫(kù)狀態(tài)與業(yè)務(wù)IP切換的一致性。啟動(dòng)管理節(jié)點(diǎn) 之后 VIP會(huì)自動(dòng)別名到當(dāng)前主節(jié)點(diǎn)上,Keepalived也只能做到對(duì)3306的健康檢查,但是做不到比如像MySQL復(fù)制中的Slave-SQL、 Slave-IO進(jìn)程的檢查,容易出現(xiàn)對(duì)切換的誤判。
6.注意:二級(jí)從服務(wù)器需要將log_slave_updates打開(kāi)
7.手工切換需要先定義好master_ip_online_change_script腳本,不然只會(huì)切換mysql,IP地址不會(huì)綁定上去,可以根據(jù)模板來(lái)配置該腳本
8.通過(guò)設(shè)置no_master=1可以讓某一個(gè)節(jié)點(diǎn)永遠(yuǎn)不成為新的主節(jié)點(diǎn)
①在manager上刪除app1.failover.complete文件
cd /masterha/app1 rm -f app1.failover.complete
②原master主節(jié)點(diǎn)服務(wù)啟動(dòng)
service mysql start
③ manager管理節(jié)點(diǎn),檢查同步報(bào)錯(cuò)
masterha_check_repl --conf=/etc/masterha/app1.cnf Thu Feb 23 15:00:56 2017 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln604] There are 2 non-slave servers! MHA manages at most one non-slave server. Check configurations.
⑤查看現(xiàn)在的slave1上的信息
mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000010 Position: 120 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
④配置187節(jié)點(diǎn)mysql為新的slave,并啟動(dòng)同步進(jìn)程
change master to master_host='192.168.56.122', master_user='repl', master_password='repl', master_log_file='mysql-bin.000010', master_log_pos=120; mysql> start slave;
再次在管理節(jié)點(diǎn)上檢查同步狀態(tài)成功:
masterha_check_repl --conf=/etc/masterha/app1.cnf
需注意:按如上步驟操作后,此時(shí)121節(jié)點(diǎn)作為slaver已加入到集群中,但是宕機(jī)這段時(shí)間122、123中新產(chǎn)生的數(shù)據(jù)在121中沒(méi)有,所以還需要先從主節(jié)點(diǎn)備份導(dǎo)入最新的數(shù)據(jù)再啟動(dòng)同步
⑤啟動(dòng)MHA
nohup masterha_manager –conf=/etc/masterha/app1.cnf > /mha/app1/mha_manager.log &1 &
回切:
同樣的道理,以上步驟配置無(wú)問(wèn)題的話(huà)停止當(dāng)前master的MySQL進(jìn)程,MHA可直接切換master至原節(jié)點(diǎn)
到此,相信大家對(duì)“MHA搭建及故障維護(hù)的方法是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!