基于Keepalvied的Mysql主主漂移(切换)

Keepalived实现原理:Keepalived详细介绍简介

实验环境

Master1、Amoeba--IP:192.168.1.5

Master2---IP:192.168.1.10

同时安装keepalived


依赖包
[root@dep5 keepalived-1.1.20]# yum install openssl-devel popt-devel -y
make clean&& make clean all

 

tar -xvf keepalived-1.1.20.tar.gz && cd keepalived-1.1.20 && ./configure --prefix=/usr/local/keepalived && make && make install && cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ &&  mkdir /etc/keepalived && cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ &&  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/


#############必须给检查文件给执行权限
chmod /etc/keepalived/manage_mysql/ -x -R

双主都需要设置用户
grant all on *.* to keepalived@'%' identified by 'keepalived';

双主都需要安装keepalived并且配置
keepalived配置文件

 

! Configuration File for keepalived
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MYSQLHA_DEVEL
}

vrrp_script check_mysqld {
    script "/etc/keepalived/mysqlcheck/check_mysql.sh"  #检查MYSQL 复制状态脚本
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP  #这里主201和主202都写上BACKUP
    interface eth0  #vip对外的网络接口
    virtual_router_id 66 #标识号。。主主要统一起来
    priority 100   #优先级。主202上改成90
    advert_int 2
#    nopreempt   #不抢占模式。在优先级高的那台设置此参数,一般在主上设置,主的优先级高


    authentication {
        auth_type PASS
        auth_pass 1111
    }

      track_script {
          check_mysqld  #调用MYSQL脚本的函数
    }

    virtual_ipaddress {
        192.168.5.10/24
    }
}

 

检查Mysql状态脚本

#!/bin/bash
#Totle:check_mysql
#Description:Check mysql status
#system:Use Linux
#======================================
#Set mysql host 
#每个机器改成自己的IP地址和远程授权的用户
Host_S=192.168.1.5
User_S=keepalived
Pwss_S=keepalived
Port_S=3306
#======================================
#Function-->Check_mysql_IO
#=====================================
Check_mysql_IO()
{

Check_IO=`mysql -u$User_S -p$Pwss_S -h $Host_S -P $Port_S -e "show slave status\G" | grep "Runnin" | sed 's/ //g' | grep "IO" | awk -F: '{print $NF}' | grep "Yes" | wc -l`

Check_SQL=`mysql -u$User_S -p$Pwss_S -h $Host_S -P $Port_S -e "show slave status\G" | grep "Runnin" | sed 's/ //g' | grep "SQL" | awk -F: '{print $NF}' | grep "Yes" | wc -l`


if [ $Check_IO -ne 1 -o $Check_SQL -ne 1 ]
then
  /etc/init.d/keepalived stop
  return 1
else
  Check_PID=`/etc/init.d/keepalived status | grep "pid" | wc -l`
  if [ $Check_PID -eq 1 ]
  then
      echo "OK"
      exit 0
  else
      /etc/init.d/keepalived start
     if [ $? -eq 0 ]
     then
         echo "keepalived start ok" >/tmp/checkmysql.log
         exit 0
     else
         echo "keepalived start fail..">/tmp/checkmysql.log
         return 1
     fi
  fi 
fi
}

#======================================
#Function-->Main
#=====================================
Main()
{

Check_mysql_IO
    if [ $? -eq 1 ]
    then
        echo "Mysql $Host_S IO or SQL error" | mail -s "Mysql IO error" 50738846@qq.com
        exit 1
    fi

}
Main;

 

posted on 2016-08-27 21:03  losbyday  阅读(426)  评论(0编辑  收藏  举报

导航