cat>>/etc/masterha/app1/app1.conf <<eof[ align]masterha_check_ssh--conf=/etc/masterha/app1/app1.conf
测试主从复制
masterha_check_repl--conf=/etc/masterha/app1/app1.conf
3.6 启动
masterha_manager--conf=/etc/masterha/app1/app1.conf &
关闭主库,观察failover过程
tail-f /etc/masterha/app1/manager.log
-----Failover Report -----
app1:MySQL Master failover 192.168.199.113 to 192.168.199.115 succeeded
Master192.168.199.113 is down!
CheckMHA Manager logs at mha-master:/etc/masterha/app1/manager.log for details.
Startedautomated(non-interactive) failover.
Thelatest slave 192.168.199.115(192.168.199.115:3306) has all relay logs for recovery.
Selected192.168.199.115 as a new master.
192.168.199.115:OK: Applying all logs succeeded.
Generatingrelay diff files from the latest slave succeeded.
192.168.199.115:Resetting slave info succeeded.
Masterfailover to 192.168.199.115(192.168.199.115:3306) completed successfully.
从日志中可观察到,已经切换到了192.168.199.115上,目前环境为图2.
将环境还原成图1,配置keepalived.
4.部署keepalived
4.1安装keepalived(两台都要装)
yuminstall openssl* popt* -y
ln-s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux
ll/usr/src/
mkdir-p /application/tools
cd/application/tools
tarzxf keepalived-1.1.19.tar.gz
cdkeepalived-1.1.19
./configure--sysconf=/etc
make
makeinstall
cp/usr/local/sbin/keepalived /usr/sbin/keepalived
/etc/init.d/keepalivedstart
ps-ef|grep keep
/etc/init.d/keepalivedstop
4.2 配置keepalived
mha-nodekeepalived配置文件:
[iyunv@mha-nodemysql]# cat /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs{
router_id MHA_01
}
vrrp_scriptcheck_mysql {
script "sh/etc/keepalived/ck_mysql.sh"
interval 2
weight -20
}
vrrp_instanceVI_1 {
state BACKUP
interface eth0
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.199.118/24
}
nopreempt
track_script {
check_mysql
}
}
mha-masterkeepalived配置文件:
[iyunv@mha-masterapp1]# cat /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs{
router_id MHA_02
}
vrrp_scriptcheck_mysql {
script "sh/etc/keepalived/ck_mysql.sh"
interval 2
weight -20
}
vrrp_instanceVI_1 {
state BACKUP
interface eth0
virtual_router_id 60
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.199.118/24
}
track_script {
check_mysql
}
}
停止mha-node主机keepalived服务,测试VIP漂移是否正常(略)
ck_mysql.sh脚本:
#!/bin/sh
/usr/local/mysql/bin/mysql-utest -p123456 -e "select version()" >/dev/null 2>&1
if[ $? -eq 0 ];then
exit 0
else
exit 1
fi
5.测试
关闭主库的mysql,测试从库是否被提升为主库,VIP是否漂到到从库上去。
-----Failover Report -----
app1:MySQL Master failover 192.168.199.113 to 192.168.199.115 succeeded
Master192.168.199.113 is down!
CheckMHA Manager logs at mha-master:/etc/masterha/app1/manager.log for details.
Startedautomated(non-interactive) failover.
Thelatest slave 192.168.199.115(192.168.199.115:3306) has all relay logs forrecovery.
Selected192.168.199.115 as a new master.
192.168.199.115:OK: Applying all logs succeeded.
Generatingrelay diff files from the latest slave succeeded.
192.168.199.115:Resetting slave info succeeded.
Master failover to192.168.199.115(192.168.199.115:3306) completed successfully.
[iyunv@mha-masterapp1]# ip addr |grep 192.168.199.
inet 192.168.199.115/24 brd 192.168.199.255scope global eth0
inet 192.168.199.118/24 scope global secondary eth0