-----构建mysql高可用系列(共9篇)
上一篇文章介绍了本次架构的mha读写分离!
世上本来就没有十全十美的事物。你不能要求一个人没有一点缺点错误。要正视自己的长处和短处,取他人之长补己之短,把自己的优点发挥至极致,你将会拥有精彩的人生。
mha虽然功能强大,但是它同样并不完美,本篇文章主要介绍手工编写shell脚本解决mha的不完美。我只是基本把功实现了,因为我也不完美( ^_^ ) !!!!
修复当AB故障切换一次后,mha-manager会自动退出
(1)
vi
/app/masterha/monitor
.sh
#增加如下内容
#!/bin/bash
while
:
#死循环
do
mhapid=`
ps
-ef|
grep
-
v
grep
|
grep
masterha_manager |
wc
-l`
#获取关于mha-manager的进程
echo
"mhapid:"
$mhapid
if
[ $mhapid -
eq
0 ];
then
#如果没有关于mha-manager的进程
masterha_stop --conf=
/etc/masterha/app1
.cnf
#尝试停止mha-manager
masterha_manager --conf=
/etc/masterha/app1
.cnf --ignore_last_failover
< /dev/null
>
/app/masterha/app1/app1
.log 2>&1 &
#重新启动mha-manager
/app/masterha/app1/check_old_master
.sh
#调用另一个脚本,详情见下面
mhapid=`
ps
-ef|
grep
-
v
grep
|
grep
masterha_manager |
wc
-l`
#获取关于mha-manager的进程
if
[ $mhapid -
eq
0 ];
then
#如果没有关于mha-manager的进程
masterha_stop --conf=
/etc/masterha/app1
.cnf
#尝试停止mha-manager
fi
fi
sleep
5
#休眠5秒
done
(2)
chmod
+x
/app/masterha/monitor
.sh
#授予脚本可执行权限
(3)
nohup
sh
/app/masterha/monitor
.sh &
#以Nohup模式放在后台执行
修复原主库出问题后,修复后不能自动加入现有AB集群
1、在mha manager节点上创建脚本
1) vi
/app/masterha/app1/check_old_master
.sh
#增加如下内容
#!/bin/bash
`
awk
-F:
'/All other slaves should start replication from here. Statement
should be:/{a=$4}END{print a}'/app/masterha/app1/app1
.log
|sed
"s/xxx/lipengfei/"
>
/app/masterha/app1/cmd
.txt`
#从mha manager日志中提取关于加入到新AB架构的信息,存放到cmd.txt中
master1=
"10.142.132.51"
#声明主库1的IP
master2=
"10.142.132.52"
#声明主库2的IP
new_master=$(
awk
'/as a new master./{a=$2}END{print a}'
/app/masterha/app1/app1
.log)
#获取新主库的IP
echo
'the new master: '
${new_master}
if
[
"$master1"
=
"$new_master"
]
#如果当前主为主库1,将提取的信息scop到主库2
then
echo
"the text scop begin :"
${master2}
scp
/app/masterha/app1/cmd
.txt 10.142.132.52:/app/masterha/app1
else
echo
"the text scop begin :"
${master1}
#如果当前主为主库2,将提取的信息scop到主库1
scp
/app/masterha/app1/cmd
.txt 10.142.132.51:/app/masterha/app1
fi
(2)
chmod
+x
/app/masterha/app1/check_old_master
.sh
#给脚本授权可执行权限
2、在52和51两个节点上创建脚本
(1)
vi
/app/masterha/app1/add_ab
.sh
#增加如下内容
#!/bin/bash
MYSQL=
/app/mysql/bin/mysql
#声明Mysql命令绝对路径
MYSQL_HOST=localhost
#声明IP
MYSQL_USER=root
#声明登录mysql的用户
MYSQL_PASSWORD=mysql
#声明登录mysql的用户的密码
myFile=
"/app/masterha/app1/cmd.txt"
#指定读取文件的绝对路径
lipengfei=$(
cat
/app/masterha/app1/cmd
.txt)
#lipengfei保存cmd文件的内容
echo
"lipengfei:"
$lipengfei
if
[ ! -f
"$myFile"
]
#如果没有文件
then
echo
"the text is not exist, Don't do anything!!!!"
else
#如果有文件
echo
"the text is exist,add the AB!!!!"
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e
"stop slave;"
#停止AB复制
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e
"$lipengfei;"
#加入现有AB架构
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e
"start slave;"
#开始AB复制
rm
-rf myFile
#使用一次后,删除此文件
fi
(2)
chmod
+x
/app/masterha/app1/add_ab
.sh
#给脚本授权可执行权限
(3)
vi
/app/masterha/app1/monitor
.sh
#增加如下内容
#!/bin/bash
while
:
do
sh
/app/masterha/app1/add_ab
.sh
sleep
5
done
(4)
chmod
+x
/app/masterha/monitor
.sh
#给脚本授权可执行权限
(5)
nohup
sh
/app/masterha/monitor
.sh
#以Nohup模式放在后台执行
到此为止,我编写的shell脚本就结束了!
我的shell只是实现的基本功能,朋友们可以在我的shell脚本基础上再改写!
朋友们最难的是迈出第一步,永远别轻易放弃,因为你永远不知道未来的你会有多强大!
加油吧,朋友们!