keepalived+mysql主主配置手册
Master A:global_defs {
notification_email{
root@localhost
}
notification_email_from keepalived.example.com
router_id MySQL_HA
}
vrrp_script check_mysqld {
script "/etc/keepalived/check_slave.pl 127.0.0.1"
interval 2
weight 21
}
vrrp_instance HA_1 {
state BACKUP //master A和master B上均配置为BACKUP
interface eth0
virtual_router_id80
priority 100
advert_int 2
nopreempt //不抢占模式,只在优先级高的机器上设置即可,优先级低的机器可以不设置。
authentication {
auth_type PASS
auth_pass 23b14455cd
}
track_script {
check_mysqld
}
virtual_ipaddress {
192.168.166.254
}
}
其中,/etc/keepalived/check_slave.sh 脚本内容为:
#!/bin/bash
#######################################
# this script function is :
#check_mysql_slave_replication_status
#
# User YYYY-MM-DD - ACTION
# mlx2013-12-29 - Created
# mail 552326439@qq.com
#######################################
HOST_IP=localhost
HOST_PORT=3306
MYUSER=root
MYPASS="123456"
MYSOCK=/tmp/mysql.sock
MYSQL_PATH=/usr/local/mysql/bin
MYSQL_CMD="$MYSQL_PATH/mysql-u$MYUSER -p$MYPASS -S $MYSOCK"
CHECKNUM=123
MYSQL1=$($MYSQL_CMD -N -s -e"select ${CHECKNUM}")
if [ $? -ne 0 ] || ["${MYSQL1}" -ne "${CHECKNUM}" ];then
/etc/init.d/keepalived stop
exit 1
else
SlaveStatusArr=($($MYSQL_CMD -e"show slave status \G"|egrep "_Behind|_Running"|awk '{print$NF}'))
if [ "${SlaveStatusArr}"= "No" ]|| ["${SlaveStatusArr}" = "No" ];then
/etc/init.d/keepalived stop
fi
fi
说明:此监控脚本可以检测mysql服务是否启动和mysql主从同步是否正常。
Master B:
global_defs {
notification_email{
root@localhost
}
notification_email_from keepalived.example.com
router_id MySQL_HA
}
vrrp_script check_mysqld {
script "/etc/keepalived/check_slave.pl 127.0.0.1"
interval 2
weight 21
}
vrrp_instance HA_1 {
state BACKUP //master A和master B上均配置为BACKUP
interface eth0
virtual_router_id80
priority 90
advert_int 2
authentication {
auth_type PASS
auth_pass 23b14455cd
}
track_script {
check_mysqld
}
virtual_ipaddress {
192.168.166.254
}
}
其中,/etc/keepalived/check_slave.pl 脚本内容为:
#!/bin/bash
#######################################
# this script function is :
# check_mysql_slave_replication_status
#
# User YYYY-MM-DD - ACTION
# mlx2013-12-29 -Created
# mail 552326439@qq.com
#######################################
HOST_IP=localhost
HOST_PORT=3306
MYUSER=root
MYPASS="123456"
MYSOCK=/tmp/mysql.sock
MYSQL_PATH=/usr/local/mysql/bin
MYSQL_CMD="$MYSQL_PATH/mysql -u$MYUSER -p$MYPASS -S$MYSOCK"
CHECKNUM=123
MYSQL1=$($MYSQL_CMD -N -s -e "select${CHECKNUM}")
if [ $? -ne 0 ] || [ "${MYSQL1}" -ne"${CHECKNUM}" ];then
/etc/init.d/keepalived stop
exit 1
else
SlaveStatusArr=($($MYSQL_CMD -e "show slave status \G"|egrep"_Behind|_Running"|awk '{print $NF}'))
if ["${SlaveStatusArr}" = "No" ]|| [ "${SlaveStatusArr}" ="No" ];then
/etc/init.d/keepalived stop
fi
fi
页:
[1]