#!/bin/bash
source /cloud_nsd/conf/mysql.conf
if [ -f "$Script_Path/myfunction.lib" ];thensource $Script_Path/myfunction.lib
elseecho -e "\033[31m函数库不存在\033[0m"exit $NOEXIST
fi
INIT(){bash $Script_Path/Pssh.sh -pyum -y install perl-DBD-mysql perl-DBIyum -y install $Soft_Path/mysql/mha-soft-student/perl-*.rpmrpm -ivh $Soft_Path/mysql/mha-soft-student/mha4mysql-node-0.56-0.el6.noarch.rpm
}
CONFIG(){cp $Mysql_Conf $Mysql_Conf.bak sed -i '/\[mysqld\]/a relay_log_purge=off' $Mysql_Conf sed -i '/\[mysqld\]/a binlog-format="mixed"' $Mysql_Conf sed -i "/\[mysqld\]/a log-bin=$HOSTNAME" $Mysql_Confsed -i "/\[mysqld\]/a server_id=$Server_Id" $Mysql_Confsed -i '/\[mysqld\]/a rpl-semi-sync-slave-enabled=1' $Mysql_Conf sed -i '/\[mysqld\]/a rpl-semi-sync-master-enabled=1' $Mysql_Conf sed -i '/\[mysqld\]/a plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"' $Mysql_Conf
}
SELECT_MASTER(){ mysql -uroot -p$Mysql_Pwd -e "change master to master_host=$Master_Slave_Ip,master_user=$Master_User,master_password=$Master_Passwd,master_log_file=$Master_Log,master_log_pos=$Master_Pos"mysql -uroot -p$Mysql_Pwd -e "start slave"Io_Thread=`mysql -uroot -p$Mysql_Pwd -e "show slave status\G" |grep 'Slave_IO_Running' |awk '{print $2}'`Sql_Thread=`mysql -uroot -p$Mysql_Pwd -e "show slave status\G" |grep 'Slave_SQL_Running:' |awk '{print $2}'`if [ "$Io_Thread" == "Yes" -a "$Sql_Thread" == "Yes" ];thencecho 36 "主从同步成功"else cecho 31 "主从同步失败"exit $ISERRORfi
}
MASTER(){ Float_Ip=`echo $Vip |sed -r 's/(.)(.*)(.)/\2/'` ifconfig $Nic:1 $Float_Ip CONFIGsystemctl restart mysqldmysql -uroot -p$Mysql_Pwd -e "grant insert,select on *.* to $Client_User@$Grant_Net identified by $Client_Pwd"mysql -uroot -p$Mysql_Pwd -e "grant all on *.* to $Moni_User@$Grant_Net identified by $Moni_Passwd"mysql -uroot -p$Mysql_Pwd -e "grant all on *.* to $Master_User@$Grant_Net identified by $Master_Passwd"mysql -uroot -p$Mysql_Pwd -e "show grants for $Moni_User" && cecho 36 "监控用户添加成功"mysql -uroot -p$Mysql_Pwd -e "show grants for $Master_User" && cecho 36 "同步授权用户添加成功"[ $? -ne 0 ] && ERROR MASTER && exit $CONFERROR
}
BACKUP_MASTER(){CONFIGsystemctl restart mysqldSELECT_MASTER && cecho 36 "备用主服务器配置成功"
}
DATA_BACKUP(){cp $Mysql_Conf $Mysql_Conf.bak sed -i '/\[mysqld\]/a relay_log_purge=off' $Mysql_Conf sed -i "/\[mysqld\]/a server_id=$Server_Id" $Mysql_Confsed -i '/\[mysqld\]/a rpl-semi-sync-slave-enabled=1' $Mysql_Confsed -i '/\[mysqld\]/a plugin-load="rpl_semi_sync_slave=semisync_slave.so"' $Mysql_Confsystemctl restart mysqldSELECT_MASTER && cecho 36 "数据备份服务器配置成功"
}
MANAGER(){ Master_Host&#61;&#96;echo $Master_Slave_Ip |sed -r &#39;s/(.)(.*)(.)/\2/&#39;&#96;yum -y install perl-ExtUtils-* perl-CPAN-*CA_TAR $Soft_Path/mysql/mha-soft-student/mha4mysql-manager-0.56.tar.gz cd /opt/mha4mysql-manager-0.56cecho 32 "安装MHA....."perl Makefile.PL && make && make install MKDIR &#96;dirname $Mha_Conf&#96; touch $Mha_Confcp $Soft_Path/mysql/mha-soft-student/master_ip_failover &#96;dirname $Mha_Conf&#96;sed -i "35c my \$vip &#61; $Vip; \# Virtual IP" &#96;dirname $Mha_Conf&#96;/master_ip_failover cat >> $Mha_Conf << EOF
[server default]
manager_workdir&#61;&#96;dirname $Mha_Conf&#96;
manager_log&#61;&#96;dirname $Mha_Conf&#96;/manager.log
master_ip_failover_script&#61;&#96;dirname $Mha_Conf&#96;/master_ip_failoverssh_port&#61;22
ssh_user&#61;root
repl_user&#61;$Master_User
repl_password&#61;$Master_Passwd
user&#61;$Moni_User
password&#61;$Moni_Passwd[server$Master_ID]
hostname&#61;$Master_Host
candidate_master&#61;1[server$Bk_Mst1_ID]
candidate_master&#61;1
hostname&#61;$Bk_Mst_Host_1[server$Bk_Mst2_ID]
candidate_master&#61;1
hostname&#61;$Bk_Mst_Host_2[server$Data_Bk1_ID]
hostname&#61;$Data_Bk_1
no_master&#61;1
EOF
}
TEST(){masterha_check_ssh --conf&#61;$Mha_Conf && masterha_check_repl --conf&#61;$Mha_Conf[ $? -eq 0 ] && OK MHA
}
HELP(){cat << EOF
MHA version 1.1.1
Usage: MHA [optional]
&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
optional arguments:-h 提供帮助信息-I 初始化环境-M 部署主服务器-B 部署备用主服务器-D 部署数据备份服务器-m 部署管理集群主机-T 测试集群
EXAMPLE:MHA.sh -I
EOF
}
[ $
while getopts :hIMBDmT ARGS
docase $ARGS inh)HELP;;I)INIT;;M)MASTER;;B)BACKUP_MASTER;;D)DATA_BACKUP;;m)MANAGER;;T)TEST;; \?)cecho 31 "Invalid option:bash &#96;basename $0&#96; [-h]"esac
done