mysql keepalived高可用性配置

--------------------------


http://blog.itpub.net/29254281/viewspace-1256984


yum install libnl-dev*  openssl* -y




wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar zxvf keepalived-1.2.19.tar.gz
 cd keepalived-1.2.19
 ./configure --prefix=/usr/local/keepalived --disable-fwmark
  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/
mkdir -p /etc/keepalived
cd /etc/init.d/
chkconfig --add keepalived
chkconfig keepalived on








 Master服务
 KeepAlived的配置文件:


  [root@mysqldb1 mysql]# vi  /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-ha1
            }


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 60
    priority 100
    advert_int 1
    notify_master "/usr/local/mysql/master.sh"
    nopreempt
    authentication {
    auth_type PASS
    auth_pass 1111
    }
virtual_ipaddress {
    192.168.134.200
    }
}


virtual_server 192.168.134.200 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 192.168.134.143 3306 {
    weight 1
    notify_down /etc/keepalived/stopkeepalived.sh
    TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        connect_port 3306
        }
    }
}





然后编辑Slave的配置文件
vim /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-ha1
            }


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 60
    priority 90
    advert_int 1
    notify_master "/usr/local/mysql/master.sh"
    nopreempt
    authentication {
    auth_type PASS
    auth_pass 1111
    }
virtual_ipaddress {
    192.168.134.200
    }
}


virtual_server 192.168.134.200 3306 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 192.168.134.139 3306 {
    weight 1
    notify_down /etc/keepalived/stopkeepalived.sh
    TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        connect_port 3306
        }
    }
}




其中
priority                      表示优先级
virtual_ipaddress      虚拟的IP地址(VIP)
delay_loop                每个2秒检查一次real_server状态
notify_down              检测到服务down后执行的脚本 
connect_timeout      连接超时时间
nb_get_retry             重连次数
delay_before_retry   重连间隔时间
connect_port            健康检查端口



[root@mysqldb2 mysql]# more /etc/keepalived/stopkeepalived.sh
#!/bin/sh  
pkill keepalived 




[root@mysqldb1 mysql]# more master.sh 
#!/bin/bash


. /root/.bash_profile


Master_Log_File=$(mysql -uroot -p123456 -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -p123456 -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{
print $2}')
Read_Master_Log_Pos=$(mysql -uroot -p123456 -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{prin
t $2}')
Exec_Master_Log_Pos=$(mysql -uroot -p123456 -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{prin
t $2}')


i=1


while true
do


if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
then
   echo "ok" >>/tmp/changemaster.log
   break
else
   sleep 1


   if [ $i -gt 60 ]
   then
      break
   fi
   continue
   let i++
fi
done


mysql -uroot -p123456 -e "stop slave;"  >>/tmp/changemaster.log
mysql -uroot -p123456 -e "reset slave;" >>/tmp/changemaster.log
mysql -uroot -p123456 -e "set global innodb_support_xa=0;" >>/tmp/changemaster.log
mysql -uroot -p123456 -e "set global sync_binlog=0;"  >>/tmp/changemaster.log
mysql -uroot -p123456 -e "set global innodb_flush_log_at_trx_commit=0;" >>/tmp/changemaster.log
mysql -uroot -p123456 -e "set global read_only=off;"  >>/tmp/changemaster.log
#mysql -uroot -p123456 -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'webapp'@'%' IDENTIFIED BY '123456';flush privile
ges;"  >>/tmp/changemaster.log
mysql -uroot -p123456 -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
sed -i 's/read_only=1/read_only=0/' /usr/local/mysql/my.cnf 


在两个服务器上启动MySQL和KeepAlived服务
service mysqld start
service keepalived start


Master的server_id为1
Slave的server_id为2


nopreempt参数表示Master恢复正常之后,是否将VIP继续指向Master
这样的话,会再次引发切换。


两台服务器的KeepAlived会有心跳检测,
如果Master的MySQL服务挂了(3306端口挂了),那么他会选择自杀.
Slave的KeepAlived通过心跳检测发现这个情况,就会将VIP的请求接管。




KeepAlived还有很多参数没有明白是什么意思
生产环境的切换脚本,在Slave提升为Master之后,应该等待所有的中继日志应用完毕,否则可能丢失数据




vi /usr/local/mysql/bin/remove_slave.sh
 
#!/bin/bash
user=root
password=123456
log=/root/mysqllog/remove_slave.log
#--------------------------------------------------------------------------------------
echo "`date`" >> $log
/usr/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log
/bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf
 
chomd 755 /usr/local/mysql/bin/remove_slave.sh






测试:
mysql -uroot -p123zxc -h192.168.8.199
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+


master:
service mysqld stop
service mysqld start
service keepalived start


tail -f  /var/log/messages



编写以下负责运作的关键脚本:
notify_master /etc/keepalived/scripts/redis_master.sh
notify_backup /etc/keepalived/scripts/redis_backup.sh
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh

因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop


请使用浏览器的分享功能分享到微信等