本篇内容主要讲解“MHA搭建及故障维护的方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MHA搭建及故障维护的方法是什么”吧!

(一)环境介绍1.主机部署

CentOS 7改主机名

hostnamectlset-hostnamemaster192.168.56.121master192.168.56.122slave1#备用master192.168.56.123slave2192.168.56.124manager

将ip和域名配置到/etc/hosts文件中

尝试在各主机上的防火墙上加上端口的允许

iptables-IINPUT-s0/0-ptcp--dport3306-jACCEPT

这条规则的意思是,想要在输入数据INPUT中,protocol为tcp/IP的方式,访问端口3306,都会被允许的

iptables-L-n|grep3306ACCEPTtcp--0.0.0.0/00.0.0.0/0tcpdpt:3306(二)用ssh-keygen实现四台主机之间相互免密钥登录1.生成密钥

[master,slave1,slave2,manager]

ssh-keygen-trsa

[slave1,slave2,manager]

scp.ssh/id_rsa.pubmaster:/root/.ssh/slave1.pubscp.ssh/id_rsa.pubmaster:/root/.ssh/slave2.pubscp.ssh/id_rsa.pubmaster:/root/.ssh/manager.pub2.在主机上用cat xxx >> authorized_keys导入公钥到/root/.ssh/authorized_keys文件中

[master]

cat~/.ssh/*.pub>>~/.ssh/authorized_keysscp~/.ssh/authorized_keysslave1:/root/.ssh/authorized_keysscp~/.ssh/authorized_keysslave2:/root/.ssh/authorized_keysscp~/.ssh/authorized_keysmanager:/root/.ssh/authorized_keys(三)安装MHAmha4mysql-node,mha4mysql-manager 软件包1.安装MHAmha4mysql-node

[manager,master,slave1,slave2]yum-yinstallperl-DBD-MySQLyum-yinstallperl-Config-Tinyyum-yinstallperl-Log-Dispatchyum-yinstallperl-Parallel-ForkManagermha4mysql-node-0.55-0.el6.noarch.rpm2.安装mha4mysql-manager

[manager]yum-yinstallperlyum-yinstallcpanrpm-ivhmha4mysql-manager-0.55-0.el6.noarch.rpm

缺啥,yum install xxx 啥就行。

(四)、建立master,slave1,slave2之间主从复制

略(五)、管理机manager上配置MHA文件

[manager]

1.创建目录

mkdir-p/masterha/app1mkdir/etc/masterhavi/etc/masterha/app1.cnf[serverdefault]user=rootpassword=rootmanager_workdir=/masterha/app1manager_log=/masterha/app1/manager.logremote_workdir=/masterha/app1ssh_user=rootrepl_user=reprepl_password=replping_interval=1[server1]hostname=192.168.56.122master_binlog_dir=/var/lib/mysqlcandidate_master=1#relay_log_purge=0[server2]hostname=192.168.56.121master_binlog_dir=/var/lib/mysqlcandidate_master=1[server3]hostname=192.168.56.123master_binlog_dir=/var/lib/mysqlno_master=1#relay_log_purge=0(六)、masterha_check_ssh工具验证ssh信任登录是否成功

[manager]masterha_check_ssh--conf=/etc/masterha/app1.cnf[root@manager~]#masterha_check_ssh--conf=/etc/masterha/app1.cnfThuFeb2312:00:242017-[warning]Globalconfigurationfile/etc/masterha_default.cnfnotfound.Skipping.ThuFeb2312:00:242017-[info]Readingapplicationdefaultconfigurationsfrom/etc/masterha/app1.cnf..ThuFeb2312:00:242017-[info]Readingserverconfigurationsfrom/etc/masterha/app1.cnf..ThuFeb2312:00:242017-[info]StartingSSHconnectiontests..ThuFeb2312:00:252017-[debug]ThuFeb2312:00:242017-[debug]ConnectingviaSSHfromroot@192.168.56.122(192.168.56.122:22)toroot@192.168.56.121(192.168.56.121:22)..ThuFeb2312:00:252017-[debug]ok.ThuFeb2312:00:252017-[debug]ConnectingviaSSHfromroot@192.168.56.122(192.168.56.122:22)toroot@192.168.56.123(192.168.56.123:22)..ThuFeb2312:00:252017-[debug]ok.ThuFeb2312:00:252017-[debug]ThuFeb2312:00:252017-[debug]ConnectingviaSSHfromroot@192.168.56.121(192.168.56.121:22)toroot@192.168.56.122(192.168.56.122:22)..Warning:Permanentlyadded'192.168.56.121'(ECDSA)tothelistofknownhosts.ThuFeb2312:00:252017-[debug]ok.ThuFeb2312:00:252017-[debug]ConnectingviaSSHfromroot@192.168.56.121(192.168.56.121:22)toroot@192.168.56.123(192.168.56.123:22)..ThuFeb2312:00:252017-[debug]ok.ThuFeb2312:00:262017-[debug]ThuFeb2312:00:252017-[debug]ConnectingviaSSHfromroot@192.168.56.123(192.168.56.123:22)toroot@192.168.56.122(192.168.56.122:22)..Warning:Permanentlyadded'192.168.56.123'(ECDSA)tothelistofknownhosts.ThuFeb2312:00:262017-[debug]ok.ThuFeb2312:00:262017-[debug]ConnectingviaSSHfromroot@192.168.56.123(192.168.56.123:22)toroot@192.168.56.121(192.168.56.121:22)..ThuFeb2312:00:262017-[debug]ok.ThuFeb2312:00:262017-[info]AllSSHconnectiontestspassedsuccessfully.[root@manager~]#(七)、masterha_check_repl工具验证mysql复制是否成功

[manager]masterha_check_repl--conf=/etc/masterha/app1.cnf[root@managermysql]#masterha_check_repl--conf=/etc/masterha/app1.cnfThuFeb2314:37:052017-[warning]Globalconfigurationfile/etc/masterha_default.cnfnotfound.Skipping.ThuFeb2314:37:052017-[info]Readingapplicationdefaultconfigurationsfrom/etc/masterha/app1.cnf..ThuFeb2314:37:052017-[info]Readingserverconfigurationsfrom/etc/masterha/app1.cnf..ThuFeb2314:37:052017-[info]MHA::MasterMonitorversion0.55.ThuFeb2314:37:052017-[info]DeadServers:ThuFeb2314:37:052017-[info]AliveServers:ThuFeb2314:37:052017-[info]master(192.168.56.121:3306)ThuFeb2314:37:052017-[info]slave1(192.168.56.122:3306)ThuFeb2314:37:052017-[info]slave2(192.168.56.123:3306)ThuFeb2314:37:052017-[info]AliveSlaves:.......此处省略ThuFeb2314:37:082017-[info]Connectingtoroot@192.168.56.123(slave2:22)..Creatingdirectory/masterha/app1..done.Checkingslaverecoveryenvironmentsettings..Opening/var/lib/mysql/relay-log.info...ok.Relaylogfoundat/tmp,uptomysql-relay-bin.000004Temporaryrelaylogfileis/tmp/mysql-relay-bin.000004Testingmysqlconnectionandprivileges..Warning:Usingapasswordonthecommandlineinterfacecanbeinsecure.done.Testingmysqlbinlogoutput..done.Cleaninguptestfile(s)..done.ThuFeb2314:37:082017-[info]Slavessettingscheckdone.ThuFeb2314:37:082017-[info]master(currentmaster)+--slave1+--slave2ThuFeb2314:37:082017-[info]Checkingreplicationhealthonslave1..ThuFeb2314:37:082017-[info]ok.ThuFeb2314:37:082017-[info]Checkingreplicationhealthonslave2..ThuFeb2314:37:082017-[info]ok.ThuFeb2314:37:082017-[warning]master_ip_failover_scriptisnotdefined.ThuFeb2314:37:082017-[warning]shutdown_scriptisnotdefined.ThuFeb2314:37:082017-[info]Gotexitcode0(Notmasterdead).MySQLReplicationHealthisOK.(八)、启动MHA manager,并监控日志文件

[manager]masterha_manager--conf=/etc/masterha/app1.cnftail-f/masterha/app1/manager.log(九)测试master(宕机后,是否会自动切换1.停掉master上的mysql服务

[master][root@master~]#servicemysqlstopShuttingdownMySQL.....SUCCESS![root@master~]#[manager]2.宕掉master后,/masterha/app1/manager.log文件显示:

tail-f/masterha/app1/manager.log

日志文件显示:

-----FailoverReport-----app1:MySQLMasterfailovermastertoslave1succeededMastermasterisdown!CheckMHAManagerlogsatmanager:/masterha/app1/manager.logfordetails.Startedautomated(non-interactive)failover.Thelatestslaveslave1(192.168.56.122:3306)hasallrelaylogsforrecovery.Selectedslave1asanewmaster.slave1:OK:Applyingalllogssucceeded.slave2:Thishosthasthelatestrelaylogevents.Generatingrelaydifffilesfromthelatestslavesucceeded.slave2:OK:Applyingalllogssucceeded.Slavestarted,replicatingfromslave1.slave1:Resettingslaveinfosucceeded.Masterfailovertoslave1(192.168.56.122:3306)completedsuccessfully.

上面的结果表明master成功切换。

切换过程中需要关注的几个问题

1.切换过程会自动把read_only关闭

2.切换之后需要删除手工删除/masterha/app1/app1.failover.complete,才能进行第二次测试

3.一旦发生切换管理进程将会退出,无法进行再次测试,需将故障数据库加入到MHA环境中来

4.原主节点重新加入到MHA时只能设置为slave,在

changemastertomaster_host='192.168.56.122',master_user='repl',master_password='repl',master_log_file='mysql-bin.000010',master_log_pos=120;

之前需要先 reset slave

5.关于ip地址的接管有几种方式,这里采用的是MHA自动调用IP别名的方式,好处是在能够保证数据库状态与业务IP切换的一致性。启动管理节点之后 VIP会自动别名到当前主节点上,Keepalived也只能做到对3306的健康检查,但是做不到比如像MySQL复制中的Slave-SQL、Slave-IO进程的检查,容易出现对切换的误判。

6.注意:二级从服务器需要将log_slave_updates打开

7.手工切换需要先定义好master_ip_online_change_script脚本,不然只会切换mysql,IP地址不会绑定上去,可以根据模板来配置该脚本

8.通过设置no_master=1可以让某一个节点永远不成为新的主节点

恢复集群运行

①在manager上删除app1.failover.complete文件

cd/masterha/app1rm-fapp1.failover.complete

②原master主节点服务启动

servicemysqlstart

③ manager管理节点,检查同步报错

masterha_check_repl--conf=/etc/masterha/app1.cnfThuFeb2315:00:562017-[error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm,ln604]Thereare2non-slaveservers!MHAmanagesatmostonenon-slaveserver.Checkconfigurations.

⑤查看现在的slave1上的信息

mysql>showmasterstatus\G***************************1.row***************************File:mysql-bin.000010Position:120Binlog_Do_DB:Binlog_Ignore_DB:Executed_Gtid_Set:1rowinset(0.00sec)

④配置187节点mysql为新的slave,并启动同步进程

changemastertomaster_host='192.168.56.122',master_user='repl',master_password='repl',master_log_file='mysql-bin.000010',master_log_pos=120;mysql>startslave;

再次在管理节点上检查同步状态成功:

masterha_check_repl--conf=/etc/masterha/app1.cnf

需注意:按如上步骤操作后,此时121节点作为slaver已加入到集群中,但是宕机这段时间122、123中新产生的数据在121中没有,所以还需要先从主节点备份导入最新的数据再启动同步

⑤启动MHA
nohup masterha_manager –conf=/etc/masterha/app1.cnf > /mha/app1/mha_manager.log &1 &

回切:
同样的道理,以上步骤配置无问题的话停止当前master的MySQL进程,MHA可直接切换master至原节点

到此,相信大家对“MHA搭建及故障维护的方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!