KeepAlived + mysqlMM高可用 安装配置

2270阅读 0评论2016-06-01 douyaqiang123
分类:Mysql/postgreSQL



1 架构图

 

 

2 环境准备


2.1 检查LINUX版本

# uname -r

2.6.32-220.el6.x86_64

 

2.2 检查keepalived版本

# ll keepalived-1.2.7.tar.gz

-rw-r--r--. 1 root root 289380 Aug 29  2012 keepalived-1.2.7.tar.gz

 

2.3 配置mysql 互为主从


2.4 创建用于监控的表及权限

create table test.keepalived(id int primary key,time datetime);

grant update,insert  on test.keepalived to keepalived@'127.0.0.1' identified by 'keepalived';

 

 

 

3 安装keepalived

 

wget

yum -y install gcc openssl-devel popt-devel

tar xvf keepalived-1.2.7.tar.gz

cd  keepalived-1.2.7

./configure  --prefix=/usr/local/keepalived

 

IPVS lvs模块,如果没用的lvs可以关闭这个功能,我们只用到了VRRP模块,

Use VRRP Framework 必须为YES 其他可有可无。Use VRR VMAC YES 表示VRRP支持虚拟MAC地址,如果为NO 表示不支持虚拟MAC地址。支持虚拟mac需要内核支持,centos 5.5系统(2.5.18内核)不支持,centos6.22.6.32-220.el6)支持,应该在centos6.0以上都支持。如果内核不支持也有相应的patch

 

make && make install

\cp  /usr/local/keepalived/sbin/keepalived  /usr/sbin/

\cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/

\cp /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/

chmod +x /etc/init.d/keepalived

chkconfig  --add  keepalived

chkconfig  keepalived on

mkdir -p /etc/keepalived

 

 

 

4 配置keepalived

      

4.1 主服务器

# vim  /etc/keepalived/keepalived.conf

配置内容如下

! Configuration File for keepalived

global_defs {

   router_id  MYSQL-ha  #机器标示

}

 

#定义健康检查脚本

vrrp_script check_running { 

       #健康检查脚本路径

       script "/etc/keepalived/keepalived_check_mysql.sh"  #健康检查脚本路径

       #检查心跳

       interval 5        

}

 

#定义VRRP同步组

vrrp_sync_group VG1 {

       group {

              PWRD_1

       }

       #变为主时运行的脚本

       notify_master /etc/keepalived/master.sh

       #变为从时运行的脚本

       notify_backup /etc/keepalived/slave.sh

       #检查到错误时

       notify_fault /etc/keepalived/slave.sh

}

 

#定义VRRP实例

vrrp_instance  PWRD_1 {     

       #statinstance的初始状态,但是决定是maser还是slave的是priority

       state BACKUP  

       #VIP(虚拟ip)绑定的网卡

       interface eth0 

       #设置VRID,相同为一组它决定了多播的地址,masterslave必须相同

       virtual_router_id 88

       #设置优先级,优先级高的为master

       priority 150    

       #心跳时间,单位为s

       advert_int 1

       #设置不抢占,只能设置在statebackup的节点上,而且这个节点的优先级比别的高。   

       nopreempt

       #设置认证    

       authentication { 

       #认证方式,有PASSAH两种认证方式       

       auth_type PASS

       #认证密码

       auth_pass 99999

           }

       #调用自定义定义的脚本函数  

       track_script { 

              check_running

       }

       #设置虚拟地址

       virtual_ipaddress {

               192.168.26.38

       }

}

 

4.2 备服务器

# vim  /etc/keepalived/keepalived.conf

配置内容如下

! Configuration File for keepalived

global_defs {

   router_id  MYSQL-ha  #机器标示

}

 

#定义健康检查脚本

vrrp_script check_running { 

       #健康检查脚本路径

       script "/etc/keepalived/keepalived_check_mysql.sh"  #健康检查脚本路径

       #检查心跳

       interval 5        

}

 

#定义VRRP同步组

vrrp_sync_group VG1 {

       group {

              PWRD_1

       }

       #变为主时运行的脚本

       notify_master /etc/keepalived/master.sh

       #变为从时运行的脚本

       notify_backup /etc/keepalived/slave.sh

       #检查到错误时

        notify_fault /etc/keepalived/slave.sh

}

 

#定义VRRP实例

vrrp_instance  PWRD_1 {     

       #statinstance的初始状态,但是决定是maser还是slave的是priority

       state BACKUP  

       #VIP(虚拟ip)绑定的网卡

       interface eth0 

       #设置VRID,相同为一组它决定了多播的地址,masterslave必须相同

       virtual_router_id 88

       #设置优先级,优先级高的为master

       priority 100 

       #心跳时间,单位为s

       advert_int 1

       #设置认证    

       authentication { 

       #认证方式,有PASSAH两种认证方式       

       auth_type PASS

       #认证密码

       auth_pass 99999

           }

       #调用自定义定义的脚本函数  

       track_script { 

              check_running

       }

       #设置虚拟地址

       virtual_ipaddress {

               192.168.26.38

       }

}

 

主服务器和备服务器有两个差别:
       1
、主服务器的优先级(priority)是150,从服务器的是100

       2、主服务器配置了nopreempt,从服务器没配置。

 

 

5 服务器的检测脚本


5.1 主服务器的检测脚本

# vim  /etc/keepalived/keepalived_check_mysql.sh

脚本内容如下

#!/bin/bash

MYSQL=/usr/local/mysql/bin/mysql

MYSQL_HOST=127.0.0.1

MYSQL_USER=keepalived

MYSQL_PASSWORD=keepalived

CHECK_COUNTS=3

 

#mysql  is working MYSQL_OK is 0 , mysql is not working MYSQL_OK is 1

MYSQL_OK=0

 

function check_Mysql_Runing (){

$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "replace into test.keepalived values(1,now());" >/dev/null 2>&1

if [ $? = 0 ] ;then

        MYSQL_OK=0

else

        MYSQL_OK=1

fi

return $MYSQL_OK

 

}

 

while [ $CHECK_COUNTS -ne 0 ]

do

        let "CHECK_COUNTS -= 1"

        check_Mysql_Runing

 

        if [ $MYSQL_OK = 0 ] ; then

                exit 0

        fi

 

        if [ $MYSQL_OK -eq 1 ] &&  [ $CHECK_COUNTS -eq 0 ]

        then

                exit 1

        fi

 

        sleep 1

done

 

master.sh slave.sh提供监控接口

# vim /etc/keepalived/master.sh

#!/bin/bash

echo "#`date +"%Y-%m-%d %H:%M"` master " >> /etc/keepalived/change.log

 

# vim /etc/keepalived/slave.sh

#!/bin/bash

echo "#`date +"%Y-%m-%d %H:%M"` slave " >> /etc/keepalived/change.log

 

添加执行权限

chmod +x /etc/keepalived/keepalived_check_mysql.sh

chmod +x /etc/keepalived/master.sh

chmod +x /etc/keepalived/slave.sh

 

 

 

5.2 备服务器的检测脚本

 

1.  # vim  /etc/keepalived/keepalived_check_mysql.sh

脚本内容如下

 

#!/bin/bash

MYSQL=/usr/local/mysql/bin/mysql

MYSQL_HOST=127.0.0.1

MYSQL_USER=keepalived

MYSQL_PASSWORD=keepalived

CHECK_COUNTS=3

 

#mysql  is working MYSQL_OK is 0 , mysql is not working MYSQL_OK is 1

MYSQL_OK=0

 

function check_Mysql_Runing (){

$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "replace into test.keepalived values(2,now());" >/dev/null 2>&1

if [ $? = 0 ] ;then

        MYSQL_OK=0

else

        MYSQL_OK=1

fi

return $MYSQL_OK

 

}

 

while [ $CHECK_COUNTS -ne 0 ]

do

        let "CHECK_COUNTS -= 1"

        check_Mysql_Runing

 

        if [ $MYSQL_OK = 0 ] ; then

                exit 0

        fi

 

        if [ $MYSQL_OK -eq 1 ] &&  [ $CHECK_COUNTS -eq 0 ]

        then

                exit 1

        fi

 

        sleep 1

done

 

2.  master.sh slave.sh提供监控接口

# vim /etc/keepalived/master.sh

#!/bin/bash

echo "#`date +"%Y-%m-%d %H:%M"` master " >> /etc/keepalived/change.log

 

# vim /etc/keepalived/slave.sh

#!/bin/bash

echo "#`date +"%Y-%m-%d %H:%M"` slave " >> /etc/keepalived/change.log

 

3.  添加执行权限

chmod +x /etc/keepalived/keepalived_check_mysql.sh

chmod +x /etc/keepalived/master.sh

chmod +x /etc/keepalived/slave.sh

 

主服务器和备服务器检查脚本差别:
      
主服务器检查脚本的sql语句是replace into test.keepalived values(1,now());

从服务器检查脚本的sql语句是replace into test.keepalived values(2,now());


6 启动keepalived


6.1 启动主服务器的keepalived

启动keepalived先启动服务mysql

# /etc/init.d/mysqld star

# /etc/init.d/keepalived start

 

查看VIP已经添加到eth0

#ip a

查看/var/log/messages日志


 

 

 

6.2 启动从服务器的keepalived

启动keepalived先启动服务mysql

# /etc/init.d/mysqld start

# /etc/init.d/keepalived start

 

 

 

 

 

7 测试


7.1 主服务器

模拟数据库宕掉

# /etc/init.d/mysqld stop

查看vip已经去掉

#ip a 

 

查看日志(/var/log/messages)

 

7.2 从服务器

ip a查看 VIP已经添加到eth0

 

查看日志(/var/log/messages)

 

 

上一篇:mysql使用存储过程实现插入大规模量模拟数据
下一篇:不小心删除数据--MySQL数据恢复