假设已经搭建好了mysql并且存在正常状态的主主复制
1、安装keepalived并且根据业务的vip及主机网卡,ip配置好/etc/keepalived/keepalived.conf,具体操作已在代码框中实现
1、安装环境(谨慎,建议先编译,能通过就不要进行第一步了)yum -y install popt popt-devel popt-static openssl-devel kernel-devel【必要时卸载掉libnl 和 libnl-devel】2、编译安装#tar -zxvf keepalived-1.2.23.tar.gz#cd keepalived-1.2.23#./configure --prefix=/usr/local/keepalived编译结果如下则正确,不必每一项都比对Keepalived configuration------------------------Keepalived version : 1.2.23Compiler : gccCompiler flags : -g -O2Extra Lib : -lssl -lcrypto -lcrypt Use IPVS Framework : YesIPVS sync daemon support : YesIPVS use libnl : Nofwmark socket support : YesUse VRRP Framework : YesUse VRRP VMAC : YesUse VRRP authentication : YesSNMP keepalived support : NoSNMP checker support : NoSNMP RFCv2 support : NoSNMP RFCv3 support : NoSHA1 support : NoUse Debug flags : NoMemory alloc check : Nolibnl version : NoneUse IPv4 devconf : NoUse libiptc : NoUse libipset : No#make && make install3、配置环境#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/#mkdir -p /etc/keepalived#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/#chmod +x /etc/init.d/keepalived4、编辑配置文件#vim /etc/keepalived/keepalived.confglobal_defs { router_id yy-interfacedb01 ###主机名,hostname}vrrp_instance VI_1 { state MASTER ###ha环境中扮演的角色 interface bond0 ###网卡 virtual_router_id 63 ###主机和备机相同的router_id,数字可随机 priority 100 ###争夺vip依靠的权限,两台keepalived服务同时存在的主机,权限值高的将会获取vip advert_int 1 authentication { auth_type PASS auth_pass 1663 ###主从两台机子配置应该相同 } virtual_ipaddress { 172.16.16.64/32 ###此处填写vip,掩码必须写32,原因我也不清楚 }}
2、主机上配置高可用环境(主从两台机子做相同配置)
1、编写监控脚本 #mkdir /home/mysql/watch#touch /home/mysql/watch/check_mysql_process.sh ### check_mysql_process.sh 内容如下 ####!/bin/sh#check mysql & switch hb HA process N/Arm -f /home/mysql/watch/mysql_process.exist ps -ef|grep mysql|grep datadir|grep 8808 |grep -v grep > /home/mysql/watch/mysql_process.existif [ -s /home/mysql/watch/mysql_process.exist ] ;thenecho "OK"else/etc/init.d/keepalived stop#echo "hahaha" > /tmp/wocaoecho `date` >> /home/mysql/watch/result.txtfi### 其中需要注意的是数据库实例端口号 ###2、部署监控脚本,每10s中执行一次#chmod a+x /home/mysql/watch/check_mysql_process.sh#crontab -e### MySQL HA ###* * * * * /home/mysql/watch/check_mysql_process.sh >/dev/null 2>&1* * * * * sleep 10;/home/mysql/watch/check_mysql_process.sh >/dev/null 2>&1* * * * * sleep 20;/home/mysql/watch/check_mysql_process.sh >/dev/null 2>&1* * * * * sleep 30;/home/mysql/watch/check_mysql_process.sh >/dev/null 2>&1* * * * * sleep 40;/home/mysql/watch/check_mysql_process.sh >/dev/null 2>&1* * * * * sleep 50;/home/mysql/watch/check_mysql_process.sh >/dev/null 2>&1