###MHA是一种方便简单可靠的MySQL高可用架构,具体的介绍我在这里就不多说了,下面是我在网上找的一个教程,我在此基础上进行了一些修改:
##大致步骤
(一)、环境介绍(二)、用ssh-keygen实现四台主机之间相互免密钥登录(三)、安装MHAmha4mysql-node,mha4mysql-manager和perl环境包(四)、建立master,slave1,slave2之间主从复制(五)、管理机manager上配置MHA文件(六)、masterha_check_ssh工具验证ssh信任登录是否成功(七)、masterha_check_repl工具验证mysql复制是否成功(八)、启动MHA manager,并监控日志文件(九)、测试master宕机后,是否会自动切换
##(一)、环境介绍 ###1.主机部署
master机:192.168.1.181slave1机:192.168.1.182(备用master)slave2机:192.168.1.183manager机:192.168.1.184
将ip和域名配置到/etc/hosts文件中
##(二)、用ssh-keygen实现四台主机之间相互免密钥登录
###1.生成密钥
[master,slave1,slave2,manager]
ssh-keygen -t rsa
[slave1,slave2,manager]
scp id_rsa.pub master:/root/.ssh/slave1(slave2,manager).pub
###2.在主机上用cat xxx >> authorized_keys导入公钥到/root/.ssh/authorized_keys文件中
[master]
cat *.pub>>authorized_keys scp authorized_keys slave1:/root/.shh/authorize_keys scp authorized_keys slave2:/root/.shh/authorize_keys scp authorized_keys manager:/root/.shh/authorize_keys
##(三)安装MHAmha4mysql-node,mha4mysql-manager 软件包
###1.安装MHAmha4mysql-node
[manager,master,slave1,slave2]
yum -y install perl-DBD-MySQLrpm -ivh mha4mysql-node-0.53-0.noarch.rpm
###2.安装mha4mysql-manager
[manager]
yum install perl yum install cpan rpm -ivh perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm perl-Log-Dispatch-2.27-1.el6.noarch.rpm perl-Mail-Sender-0.8.16-3.el6.noarch.rpm perl-Mail-Sendmail-0.79-12.el6.noarch.rpm perl-Time-HiRes-1.9721-136.el6.x86_64.rpm rpm -ivh mha4mysql-manager-0.53-0.el6.noarch.rpm
缺啥,yum install啥就行。
##(四)、建立master,slave1,slave2之间主从复制
略
##(五)、管理机manager上配置MHA文件
[manager]
###1.创建目录
mkdir -p /masterha/app1mkdir /etc/masterhavi /etc/masterha/app1.cnf[server default]user=rootpassword=123456manager_workdir=/masterha/app1manager_log=/masterha/app1/manager.logremote_workdir=/masterha/app1ssh_user=rootrepl_user=reprepl_password=repping_interval=1[server1]hostname=192.168.1.182master_binlog_dir=/data/mysqldb/candidate_master=1#relay_log_purge=0[server2]hostname=192.168.1.181master_binlog_dir=/data/mysqldb/candidate_master=1[server3]hostname=192.168.1.183master_binlog_dir=/data/mysqldb/no_master=1#relay_log_purge=0
##(六)、masterha_check_ssh工具验证ssh信任登录是否成功
[manager]
masterha_check_ssh --conf=/etc/masterha/app1.cnf
##(七)、masterha_check_repl工具验证mysql复制是否成功
[manager]
masterha_check_repl --conf=/etc/masterha/app1.cnfWed Dec 23 22:07:03 2015 - [info] 192.168.1.181 (current master) +--192.168.1.182 +--192.168.1.183Wed Dec 23 22:07:03 2015 - [info] Checking replication health on 192.168.1.182..Wed Dec 23 22:07:03 2015 - [info] ok.Wed Dec 23 22:07:03 2015 - [info] Checking replication health on 192.168.1.183..Wed Dec 23 22:07:03 2015 - [info] ok.Wed Dec 23 22:07:03 2015 - [warning] master_ip_failover_script is not defined.Wed Dec 23 22:07:03 2015 - [warning] shutdown_script is not defined. Wed Dec 23 22:07:03 2015 - [info] Got exit code 0 (Not master dead).MySQL Replication Health is OK
##(八)、启动MHA manager,并监控日志文件
[manager]
masterha_manager --conf=/etc/masterha/app1.cnf tail -f /masterha/app1/manager.log
##(九)测试master(宕机后,是否会自动切换
###1.停掉master上的mysql服务
[master]
/etc/init.d/myqld stop
[manager]
###2.当掉master后,/masterha/app1/manager.log文件显示:
tail -f /masterha/app1/manager.log
日志文件显示:
----- Failover Report -----app1: MySQL Master failover master to slave1 succeededMaster 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.1.182: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.1.182:3306) completed successfully.
###上面的结果表明master成功切换。