Mysql MHA配置





环境:
192.168.133.138 mha-manager
192.168.133.133 master
192.168.133.143 slave
192.168.133.139 slave




安装Manager:


安装依赖包:
yum -y install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager
yum -y install perl-DBD-MySQL  ncftp 






http://dl.fedoraproject.org/pub/epel/6Server/x86_64/


yum install perl-MIME-Lite -y
wget http://dl.fedoraproject.org/pub/epel/6Server/x86_64/perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
wget http://dl.fedoraproject.org/pub/epel/6Server/x86_64/perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
wget http://dl.fedoraproject.org/pub/epel/6Server/x86_64/perl-Log-Dispatch-2.27-1.el6.noarch.rpm
rpm -ivh perl-Log-Dispatch-2.27-1.el6.noarch.rpm 


wget http://dl.fedoraproject.org/pub/epel/6Server/x86_64/perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
rpm -vih perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm




rpm -ivh mha4mysql-manager-0.56-0.el5.noarch.rpm 
rpm -ivh mha4mysql-node-0.56-0.el5.noarch.rpm


安装Node:
 yum install perl-DBD-MySQL -y
 rpm -ivh mha4mysql-node-0.56-0.el5.noarch.rpm




配置信任


管理:
ssh-keygen -t rsa  
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.138 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.133 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.139 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.143 


主:
ssh-keygen -t rsa  
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.138 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.133 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.139 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.143 




从1:
ssh-keygen -t rsa  
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.138 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.133 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.139 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.143 


从2:
ssh-keygen -t rsa  
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.138 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.133 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.139 
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.134.143 




配置hosts
vi /etc/hosts


192.168.134.138 nosql-env1
192.168.134.133 nosql-env2
192.168.134.143 mysqldb1
192.168.134.139 mysqldb2




配置主从复制


GRANT replication slave ON *.* TO 'repl'@'%' identified by '123456';
flush privileges;




主从:
server-id = 1 
log_bin = /usr/local/mysql/data/mysql-bin
relay_log = /usr/local/mysql/data/mysql-relay-bin
log_slave_updates = 1
#read_only = 1
relay_log_purge=0
 


配置mha


133-M,143-S,139-S,VIP:150)


#创建数据文件目录
mkdir -p /usr/local/masterha/app1    
#创建配置文件目录    
mkdir -p /etc/masterha                  




vi /etc/masterha/app1.cnf


[server default]
user=root                                 #mysql用戶名
password=123456                           #mysql密码
ssh_user=root                             #ssh免密钥登录的帐号名
repl_user=repl                            #mysql复制帐号
repl_password=123456                      #mysql复制密码
ping_interval=1                           #ping间隔,用来检测master是否正常,默认是3秒,尝试三次没有回应的时候自动进行failover


manager_workdir=/usr/local/masterha/app1       #数据目录,主要该目录的权限,需要有创建的权限
manager_log=/usr/local/masterha/manager.log    #日志文件
remote_workdir=/usr/local/masterha/app1        #另外2台机子在运行时候需要创建的目录,注意ssh-keygen帐号的权限问题


#master_ip_failover_script=/usr/bin/master_ip_failover
#master_ip_online_change_script=/usr/bin/master_ip_online_change
#shutdown_script=/usr/bin/power_manager
#report_script=/usr/bin/send_report




[server1] 
hostname=192.168.134.133
master_binlog_dir=/usr/local/mysql/data         #binlog目录 
candidate_master=1 #master机宕掉后,优先启用这台作为新master




check_repl_delay=0            #默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master






[server2] 
hostname=192.168.134.143
master_binlog_dir=/usr/local/mysql/data
candidate_master=1 




[server3] hostname=192.168.134.139
master_binlog_dir=/usr/local/mysql/data
candidate_master=1
no_master=1 #设置no_master=1使主机不能成为新master


查看免密钥是否正常  
masterha_check_ssh --conf=/etc/masterha/app1.cnf 
Sat Sep  5 11:17:56 2015 - [info] All SSH connection tests passed successfully.




查看manager状态  
masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:20254) is running(0:PING_OK), master:192.168.134.133


查看主从复制是否正常  
masterha_check_repl --conf=/etc/masterha/app1.cnf
MySQL Replication Health is OK.


masterha_check_status --conf=/etc/masterha/app1.cnf














运营管理和测试
启动MHA manager:
nohup  masterha_manager  --conf=/etc/masterha/app1.cnf & 
nohup  masterha_manager  --conf=/etc/masterha/app1.cnf & > /tmp/mha_manager.log  < /dev/null 2>&1 &


检查manager运营状态:
tail -f /usr/local/masterha/manager.log
masterha_check_status --conf=/etc/masterha/app1.cnf


手工切换测试
方式一:手工对dead master进行切换
service mysqld stop master停掉


rm -f /usr/local/masterha/app1/app1.failover.complete
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.134.133 --new_master_host=192.168.134.143


方式二:当前master在正常运行情况的切换:在线切换
masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.134.139


停止manager,不停止mysql server
masterha_stop --conf=/etc/masterha/app1.cnf




Testing master failover
master:192.168.134.133:


master宕库测试
killall -9 mysqld mysqld_safe
master死机测试
echo c > /proc/sysrq-trigger   










错误:
masterha_check_ssh --conf=/etc/masterha/app1.cnf 
Can't locate MHA/SSHCheck.pm in @INC (@INC contains: : /usr/local/lib64/perl5 /usr/local/share/per
BEGIN failed--compilation aborted at /usr/bin/masterha_check_ssh line 25.




解决 :
find / -type d -name MHA
/usr/lib/perl5/vendor_perl/MHA


ln -s /usr/lib/perl5/vendor_perl/MHA /usr/lib64/perl5/vendor_perl/
masterha_manager -version


getenforce
setenforce 0




错误:
masterha_check_repl --conf=/etc/masterha/app1.cnf
[error][/usr/lib64/perl5/vendor_perl/MHA/ManagerUtil.pm, ln122] Got error when getting node version. Error:
解决 :
ln -s /usr/lib/perl5/vendor_perl/MHA /usr/lib64/perl5/vendor_perl/


错误:
masterha_check_repl --conf=/etc/masterha/app1.cnf
Sat Sep  5 11:26:28 2015 - [info]   Connecting to root@192.168.134.133(192.168.134.133:22).. 
Failed to save binary log: Binlog not found from /usr/local/mysql/data/mysql-bin! If you got this error at MHA Manager, please set "master_binlog_dir=/path/to/binlog_directory_of_the_master" correctly in the MHA Manager's configuration file and try again.




错误:
masterha_check_repl --conf=/etc/masterha/app1.cnf
There are 2 non-slave servers! MHA manages at most one non-slave server


解决:
将原先宕库的库从新加入到MHA环境中,设置为从库
change master to master_host='192.168.134.139', MASTER_PORT=3306, master_user='repl', master_password='123456', master_log_file='mysql-bin.000004', master_log_pos=328;
start slave;
show slave status\G;
 




master死机测试
echo c > /proc/sysrq-trigger   
错误:
Sat Sep  5 12:12:24 2015 - [error][/usr/lib64/perl5/vendor_perl/MHA/MasterFailover.pm, ln309] Last failover was done at 2015/09/05 11:08:12. Current time is too early to do failover again. If you want to do failover, manually remove /usr/local/masterha/app1/app1.failover.complete and run this script again.
Sat Sep  5 12:12:24 2015 - [error][/usr/lib64/perl5/vendor_perl/MHA/ManagerUtil.pm, ln177] Got ERROR:  at /usr/bin/masterha_manager line 65


没有正常切换
解决 :
rm -f /usr/local/masterha/app1/app1.failover.complete




结论:1主2从,在实现主从切换时,只涉及到2个节点,第2个从节点不会自动指向新的master




http://www.iyunv.com/thread-21790-1-1.html


yum install keepalived -y
yum install php -y


vi /etc/keepalived/keepalived.conf




! Configuration File for keepalived 


global_defs {  
     notification_email { 
     luwenju@live.cn 
     }  
     notification_email_from luwenju@live.cn 
     smtp_server 127.0.0.1 
     smtp_connect_timeout 30 
     router_id MySQL-ha 
     }  


vrrp_instance VI_1 {  
     state BACKUP   #两台配置此处均是BACKUP  
     interface eth0  #网卡,可使用ifconfig查看
     virtual_router_id 51 
     priority 100   #优先级,另一台改为90  
     advert_int 1 
     nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置  
     authentication { 
     auth_type PASS 
     auth_pass 1111 
     }  
     virtual_ipaddress { 
     192.168.134.200 
     }  
     }  


 


virtual_server 192.168.134.200 3306 { 
     delay_loop 2   #每个2秒检查一次real_server状态  
     lb_algo wrr   #LVS算法  
     lb_kind DR    #LVS模式  
     persistence_timeout 60   #会话保持时间  
     protocol TCP 
     real_server 192.168.134.133 3306 { 
     weight 3 
     notify_down /etc/keepalived/check_mysql.sh  #检测到服务down后执行的脚本  
     TCP_CHECK { 
     connect_timeout 10    #连接超时时间  
     nb_get_retry 3       #重连次数  
     delay_before_retry 3   #重连间隔时间  
     connect_port 3306   #健康检查端口  
     }  
     } 




编写检测服务down后所要执行的脚本


vi /etc/keepalived/check_mysql.sh


#!/bin/sh  
pkill keepalived  


chmod +x /etc/keepalived/check_mysql.sh


/etc/init.d/keepalived start








> /etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf
#! Configuration File for keepalived 
global_defs { 
router_id mysql-ha1 
            } 


vrrp_instance VI_1 { 
    state BACKUP  
    interface eth1 
    virtual_router_id 60  
    priority 100  
    advert_int 1 
    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.133 3306 { 
    weight 1 
    notify_down /etc/keepalived/check_mysql.sh
    TCP_CHECK { 
        connect_timeout 10 
        nb_get_retry 3 
        connect_port 3306 
        } 
    } 
}






MySQL MHA之 master_ip_failover.sh脚本






master_ip_failover.sh脚本是用perl编写的,可以在mha-manager源码包中可以找到,下面给出的结合keepalived进行自动切换的脚本:




vi master_ip_failover.sh


#!/usr/bin/env perl
#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
## Note: This is a sample script and is not complete. Modify the script based on your environment.
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);
my $vip = '192.168.134.200';
my $ssh_start_vip = "/etc/init.d/keepalived start";
my $ssh_stop_vip = "/etc/init.d/keepalived stop";
GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);
exit &main();
sub main {
    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
    if ( $command eq "stop" || $command eq "stopssh" ) {
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        #`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
my $ssh_user = "root"; 
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}






http://www.tuicool.com/articles/UB7VZzY


切换方案MHA的探索与测试结果
master故障切换:
service mysqld stop


1.切换过程会自动把read_only关闭
mysql> show variables like '%read_only%';




2.切换之后需要删除手工删除/masterha/app1/app1.failover.complete,才能进行第二次测试
If you want to do failover, manually remove /usr/local/masterha/app1/app1.failover.complete and run this script again
rm -f /usr/local/masterha/app1/app1.failover.complete


3.一旦发生切换管理进程将会退出,无法进行再次测试,需将故障数据库加入到MHA环境中来
 
4.原主节点重新加入到MHA时只能设置为slave,在
change master to MASTER_HOST='192.168.16.5', MASTER_USER='replicationuser',MASTER_PASSWORD='replicationuser',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=106;
之前需要先 reset slave


5.关于ip地址的接管有几种方式,这里采用的是MHA自动调用ip别名的方式,好处是在能够保证数据库状态与业务Ip 切换的一致性。启动管理节点之后 vip会自动别名到当前主节点上,
keepalived也只能做到对3306的健康检查,但是做不到比如像MySQL复制中的slave-SQL、 slave-IO进程的检查,容易出现对切换的误判。


6.注意:二级从服务器需要将log_slave_updates打开




7.手工切换需要先定义好master_ip_online_change_script脚本,不然只会切换mysql,Ip地址不会绑定上去,可以根据模板来配置该脚本 
 
8.通过设置no_master=1可以让某一个节点永远不成为新的主节点


9.如果只有2个节点,采用keepalived,则会准确切换到master上,要采用IP别名的脚本进行VIP的切换




---------------------------
测试相关脚本:
mha管理节点:
[root@nosql-env1 ~]# more /etc/masterha/app1.cnf
[server default]
user=root  
password=123456 
ssh_user=root  
repl_user=repl 
repl_password=123456 
ping_interval=1  
manager_workdir=/usr/local/masterha/app1 
manager_log=/usr/local/masterha/manager.log 
remote_workdir=/usr/local/masterha/app1 




master_ip_failover_script=/usr/local/masterha/master_ip_failover.sh
#master_ip_online_change_script=/tmp/master_ip_failover.sh
#shutdown_script=/usr/bin/power_manager
#report_script=/usr/bin/send_report


[server1] 
hostname=192.168.134.133
master_binlog_dir=/usr/local/mysql/data
candidate_master=1 
check_repl_delay=0 


[server2] 
hostname=192.168.134.143
master_binlog_dir=/usr/local/mysql/data
candidate_master=1 


[server3] 
hostname=192.168.134.139
master_binlog_dir=/usr/local/mysql/data
candidate_master=1




[root@nosql-env1 ~]# more /usr/local/masterha/master_ip_failover.sh
#!/usr/bin/env perl
#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
## Note: This is a sample script and is not complete. Modify the script based on
 your environment.
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
use MHA::DBHelper;
my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);
my $vip = '192.168.134.200';
my $ssh_start_vip = "/etc/init.d/keepalived start";
my $ssh_stop_vip = "/etc/init.d/keepalived stop";
GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);
exit &main();
sub main {
    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
    if ( $command eq "stop" || $command eq "stopssh" ) {
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host 
\n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        #`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
my $ssh_user = "root"; 
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master
_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --
new_master_ip=ip --new_master_port=port\n";
}






所有从节点:
[root@nosql-env2 keepalived]# more keepalived.conf 
#! Configuration File for keepalived 
global_defs { 
router_id mysql-ha1 
            } 


vrrp_instance VI_1 { 
    state BACKUP  
    interface eth1 
    virtual_router_id 60  
    priority 100  
    advert_int 1 
    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.133 3306 { 
    weight 1 
    notify_down /etc/keepalived/check_mysql.sh
    TCP_CHECK { 
        connect_timeout 10 
        nb_get_retry 3 
        connect_port 3306 
        } 
    } 
}


注意每个从节点的网卡名不一定相同




[root@nosql-env2 ~]# more /etc/keepalived/check_mysql.sh
#!/bin/sh  
pkill keepalived  






---------------------------
vi master_ip_failover.sh




#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
  $command, $ssh_user, $orig_master_host, $orig_master_ip,
  $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
my $vip = '192.168.10.218'; # Virtual IP
my $gateway = '192.168.10.1';#Gateway IP
my $interface = 'eth0'
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
GetOptions(
  'command=s' => \$command,
  'ssh_user=s' => \$ssh_user,
  'orig_master_host=s' => \$orig_master_host,
  'orig_master_ip=s' => \$orig_master_ip,
  'orig_master_port=i' => \$orig_master_port,
  'new_master_host=s' => \$new_master_host,
  'new_master_ip=s' => \$new_master_ip,
  'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
  print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
  if ( $command eq "stop" || $command eq "stopssh" ) {
    # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
    # If you manage master ip address at global catalog database,
    # invalidate orig_master_ip here.
    my $exit_code = 1;
    eval {
      print "Disabling the VIP on old master: $orig_master_host \n";
      &stop_vip();
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@\n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    # all arguments are passed.
    # If you manage master ip address at global catalog database,
    # activate new_master_ip here.
    # You can also grant write access (create user, set read_only=0, etc) here.
    my $exit_code = 10;
    eval {
      print "Enabling the VIP - $vip on the new master - $new_master_host \n";
      &start_vip();
      $exit_code = 0;
    };
    if ($@) {
      warn $@;
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {
    print "Checking the Status of the script.. OK \n";
    `ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
  `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
  `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
  print
  "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}








监控mysql脚本准备 
1.监控mysql
vi /etc/keepalived/check_mysql.sh


#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql  is working MYSQL_OK is 1 , mysql downMYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -e "show status;">/dev/null 2>&1
if [ $? = 0 ] ;then
    MYSQL_OK=1
else
    MYSQL_OK=0
fi
    return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"
    check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    exit 0
fi


if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME-eq 0 ]
then
    pkill keepalived
exit 1
fi
sleep 1
done
 


2.监控mysql
vi /etc/keepalived/check_mysql.sh


#!/bin/bash
if [ "$(ps -ef | egrep "mysqld|mysqld_safe" | grep -v egrep )" == "" ]
 then
/etc/init.d/keepalived stop
fi


3.监控mysql


vi /etc/keepalived/check_mysql.sh


#!/bin/sh
#created by oldboy
#mail:oldboy521@gmail.com
PortNum=`netstat -lnt|grep 3306|wc -l`
#PortNum=`nmap 10.0.0.189 -p 3306|grep open|wc -l`
if [ $PortNum -eq 1  ]
then
echo "mysqld is running."
else
echo "mysqld is stoped."
fi


4.监控mysql
vi /etc/keepalived/check_mysql.sh


#!/bin/sh
mysql  -S /tmp/mysql.sock -uroot -p123456  -e "select version();" >/dev/null 2>&1
if [ $? -eq 0 ]
then 
echo "mysqld is running."
else 
echo "mysqld is stoped." 
fi






5.监控mysql
vi /etc/keepalived/check_mysql.sh
#!/bin/bash
if [ "$(ps -ef | egrep "mysqld|mysqld_safe" | grep -v egrep )" == "" ]
 then
/etc/init.d/keepalived stop
fi




功能:判断当mysql进程不存在,则kill掉keepalive,将虚拟IP切换到BACKUP-Keepalive。








http://blog.csdn.net/wzy0623/article/details/38894913
http://my.oschina.net/u/142602/blog/272214








半同步复制开启
master:
ll /usr/local/mysql/lib/plugin/semi*
-rwxr-xr-x. 1 mysql mysql 421194 Jul 15 05:42 semisync_master.so
-rwxr-xr-x. 1 mysql mysql 250310 Jul 15 05:42 semisync_slave.so




install plugin rpl_semi_sync_master soname 'semisync_master.so';
set global rpl_semi_sync_master_enabled=1;
set global rpl_semi_sync_master_timeout=1000;


slave:
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
set global rpl_semi_sync_slave_enabled=1;


show global variables like 'rpl_semi_sync%';
show global status like 'rpl_semi_sync%';


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