澳门新萄京官方网站-www.8455.com-澳门新萄京赌场网址

澳门新萄京官方网站MySQL实现高可用,基于keepa

2019-10-05 作者:数据库网络   |   浏览(92)

MySQL的高可用方案经常有如下两种:

依据keepalived搭建MySQL的高可用集群

据他们说keepalived搭建MySQL的高可用集群,keepalived搭建mysql

MySQL的高可用方案平常有如下三种:

keepalived 双主,MHA,MMM,Heartbeat DRBD,PXC,Galera Cluster

正如常用的是keepalived 双主,MHA和PXC。

对于小公司,经常推荐使用keepalived 双主,轻松。

下边来安插一下

 

布局情形:

剧中人物                                    主机IP                    主机名               操作系统版本     软件版本

VIP                                    192.168.244.10

master1                             192.168.244.145       master1            CentOS7.1       MySQL 5.6.26,Keepalived v1.2.13

master2                             192.168.244.146       master2            CentOS7.1       MySQL 5.6.26,Keepalived v1.2.13

 

一、 配置MySQL双主复制意况

     1. 退换配置文件

      master1中有关复制的布局如下:

[mysqld]
log-bin=mysql-bin
server-id=1
log_slave_updates=1

     master2

[mysqld]
log-bin=mysql-bin
server-id=2
log_slave_updates=1
read_only=1

   2. 成立复制顾客

    master1中创建:

CREATE USER 'repl'@'192.168.244.146' IDENTIFIED BY 'mysql';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.244.146';

    master2中创建:

CREATE USER 'repl'@'192.168.244.145' IDENTIFIED BY 'mysql';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.244.145';

  3. 执行CHANGE MASTER TO语句

     因是起始搭建MySQL主从复制集群,所以不需求获得全局读锁来得到二进制日志文件的地方,直接依据show master status的输出来承认。

     master1上执行:

CHANGE MASTER TO
  MASTER_HOST='192.168.244.146',
  MASTER_USER='repl',
  MASTER_PASSWORD='mysql',
  MASTER_LOG_FILE='mysql-bin.000004',
  MASTER_LOG_POS=64729;

    master2上执行:

CHANGE MASTER TO
  MASTER_HOST='192.168.244.145',
  MASTER_USER='repl',
  MASTER_PASSWORD='mysql',
  MASTER_LOG_FILE='mysql-bin.000003',
  MASTER_LOG_POS=68479;

    4. 分别在八个节点上实行start slave语句并经过show slave statusG查看复制是还是不是搭建成功。

        成功标准:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

 

二、 配置Keepalived

     1. 安装Keepalived

      # yum install -y keepalived

      当然,也可一向编译官方的源码包。

     2. 改造Keepalived的布置文件

     master1

     [[email protected] ~]# vim /etc/keepalived/keepalived.conf

vrrp_script chk_mysql {
    script "/etc/keepalived/check_mysql.sh"
    interval 30         #设置检查间隔时长,可根据自己的需求自行设定
}
vrrp_instance VI_1 {
    state BACKUP        #通过下面的priority来区分MASTER和BACKUP,也只有如此,底下的nopreempt才有效
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt           #防止切换到从库后,主keepalived恢复后自动切换回主库
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_mysql
    }

    virtual_ipaddress {
        192.168.244.10/24
    }
}

至于keepalived的参数的详实介绍,可参看:LVS Keepalived搭建MyCAT高可用负载均衡集群

其中,/etc/keepalived/check_mysql.sh内容如下:

#!/bin/bash

###判断如果上次检查的脚本还没执行完,则退出此次执行
if [ `ps -ef|grep -w "$0"|grep -v "grep"|wc -l` -gt 2 ];then
    exit 0
fi 
mysql_con='mysql -uroot -p123456'
error_log="/etc/keepalived/logs/check_mysql.err"

###定义一个简单判断mysql是否可用的函数
function excute_query {
    ${mysql_con} -e "select 1;" 2>> ${error_log}
}

###定义无法执行查询,且mysql服务异常时的处理函数
function service_error {
    echo -e "`date " %F  %H:%M:%S"`    -----mysql service error,now stop keepalived-----" >> ${error_log}
    service keepalived stop &>> ${error_log}
    echo "DB1 keepalived 已停止"|mail -s "DB1 keepalived 已停止,请及时处理!" [email protected]126.com 2>> ${error_log}
    echo -e "n---------------------------------------------------------n" >> ${error_log}
}

###定义无法执行查询,但mysql服务正常的处理函数
function query_error {
    echo -e "`date " %F  %H:%M:%S"`    -----query error, but mysql service ok, retry after 30s-----" >> ${error_log}
    sleep 30
    excute_query
    if [ $? -ne 0 ];then
        echo -e "`date " %F  %H:%M:%S"`    -----still can't execute query-----" >> ${error_log}

        ###对DB1设置read_only属性
        echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 1 on DB1-----" >> ${error_log}
        mysql_con -e "set global read_only = 1;" 2>> ${error_log}

        ###kill掉当前客户端连接
        echo -e "`date " %F  %H:%M:%S"`    -----kill current client thread-----" >> ${error_log}
        rm -f /tmp/kill.sql &>/dev/null
        ###这里其实是一个批量kill线程的小技巧
        mysql_con -e 'select concat("kill ",id,";") from  information_schema.PROCESSLIST where command="Query" or command="Execute" into outfile "/tmp/kill.sql";'
        mysql_con -e "source /tmp/kill.sql"
        sleep 2    ###给kill一个执行和缓冲时间
        ###关闭本机keepalived       
        echo -e "`date " %F  %H:%M:%S"`    -----stop keepalived-----" >> ${error_log}
        service keepalived stop &>> ${error_log}
        echo "DB1 keepalived 已停止"|mail -s "DB1 keepalived 已停止,请及时处理!" [email protected]126.com 2>> ${error_log}
        echo -e "n---------------------------------------------------------n" >> ${error_log}
    else
        echo -e "`date " %F  %H:%M:%S"`    -----query ok after 30s-----" >> ${error_log}
        echo -e "n---------------------------------------------------------n" >> ${error_log}
    fi
}

###检查开始: 执行查询
excute_query
if [ $? -ne 0 ];then
    service mysqld status &>/dev/null
    if [ $? -ne 0 ];then
        service_error
    else
        query_error
    fi
fi

透过切实的查询语句来剖断数据库服务的可用性,尽管查询退步,则推断mysqld进度自身的情形,假若不健康,则一直甘休当前节点的keepalived,将VIP转移到别的二个节点,假使经常,则等待30s,再度实施查询语句,依旧败北,则将近期的master节点设置为read_only,并kill掉当前的客户端连接,然后截止当前的keepalived。

       

       master2 

       [[email protected] ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master /etc/keepalived/notify_master_mysql.sh    #此条指令告诉keepalived发现自己转为MASTER后执行的脚本
    virtual_ipaddress {
        192.168.244.10/24
    }
}

其中,/etc/keepalived/notify_master_mysql.sh的原委如下:

#!/bin/bash
###当keepalived监测到本机转为MASTER状态时,执行该脚本

change_log=/etc/keepalived/logs/state_change.log
mysql_con='mysql -uroot -p123456'
echo -e "`date " %F  %H:%M:%S"`   -----keepalived change to MASTER-----" >> $change_log

slave_info() {
    ###统一定义一个函数取得slave的position、running、和log_file等信息
    ###根据函数后面所跟参数来决定取得哪些数据
    if [ $1 = slave_status ];then
        slave_stat=`${mysql_con} -e "show slave statusG;"|egrep -w "Slave_IO_Running|Slave_SQL_Running"`
        Slave_IO_Running=`echo $slave_stat|awk '{print $2}'`
        Slave_SQL_Running=`echo $slave_stat|awk '{print $4}'`
    elif [ $1 = log_file -a $2 = pos ];then
        log_file_pos=`${mysql_con} -e "show slave statusG;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"`
        Master_Log_File=`echo $log_file_pos|awk '{print $2}'`
        Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'`
        Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'`
        Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'`
    fi
}

action() {
    ###经判断'应该&可以'切换时执行的动作
    echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 0 on DB2-----" >> $change_log

    ###解除read_only属性
    ${mysql_con} -e "set global read_only = 0;" 2>> $change_log

    echo "DB2 keepalived转为MASTER状态,线上数据库切换至DB2"|mail -s "DB2 keepalived change to MASTER"
    [email protected]126.com 2>> $change_log

    echo -e "---------------------------------------------------------n" >> $change_log
}

slave_info slave_status
if [ $Slave_SQL_Running = Yes ];then
    i=0    #一个计数器
    slave_info log_file pos
        ###判断从master接收到的binlog是否全部在本地执行(这样仍无法完全确定从库已追上主库,因为无法完全保证io_thread没有延时(由网络传输问题导致的从库落后的概率很小)
    until [ $Master_Log_File = $Relay_Master_Log_File -a $Read_Master_Log_Pos = $Exec_Master_Log_Pos ]
     do
        if [ $i -lt 10 ];then    #将等待exec_pos追上read_pos的时间限制为10s
            echo -e "`date " %F  %H:%M:%S"`    -----Relay_Master_Log_File=$Relay_Master_Log_File,Exec_Master_Log_Pos=$Exec_Master_Log_Pos is behind Master_Log_File=$Master_Log_File,Read_Master_Log_Pos=$Read_Master_Log_Pos, wait......" >> $change_log    #输出消息到日志,等待exec_pos=read_pos
            i=$(($i 1))
            sleep 1
            slave_info log_file pos
        else
            echo -e "The waits time is more than 10s,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log
            action
            exit 0
        fi
    done
    action 

else
    slave_info log_file pos
    echo -e "DB2's slave status is wrong,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log
    action
fi

 

方方面面脚本的逻辑是让从的Exec_Master_Log_Pos尽大概的追上Read_Master_Log_Pos,它给了10s的限制,假使依旧尚未追上,则直接将master2设置为主(通过化解read_only属性),其实那在那之中或然有待构和的,例如10s的限量是不是创立,依旧自然须求Exec_Master_Log_Pos=Read_Master_Log_Pos才切换。

 

当原主恢复生机不荒谬后,如何将VIP从master2切回到master第11中学呢?

#!/bin/bash
###手动执行将主库切换回DB1的操作

mysql_con='mysql -uroot -p123456'

echo -e "`date " %F  %H:%M:%S"`    -----change to BACKUP manually-----" >> /etc/keepalived/logs/state_change.log
echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 1 on DB2-----" >> /etc/keepalived/logs/state_change.log
$mysql_con -e "set global read_only = 1;" 2>> /etc/keepalived/logs/state_change.log

###kill掉当前客户端连接
echo -e "`date " %F  %H:%M:%S"`    -----kill current client thread-----" >> /etc/keepalived/logs/state_change.log
rm -f /tmp/kill.sql &>/dev/null
###这里其实是一个批量kill线程的小技巧
$mysql_con -e 'select concat("kill ",id,";") from  information_schema.PROCESSLIST where command="Query" or command="Execute" into outfile "/tmp/kill.sql";'
$mysql_con -e "source /tmp/kill.sql" 2>> /etc/keepalived/logs/state_change.log
sleep 2    ###给kill一个执行和缓冲时间

###确保DB1已经追上了,下面的repl为复制所用的账户,-h后跟DB1的内网IP
log_file_pos=`mysql -urepl -pmysql -h192.168.244.145 -e "show slave statusG;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"`
Master_Log_File=`echo $log_file_pos|awk '{print $2}'`
Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'`
Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'`
Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'`
until [ $Read_Master_Log_Pos = $Exec_Master_Log_Pos -a $Master_Log_File = $Relay_Master_Log_File ]
do
    echo -e "`date " %F  %H:%M:%S"`    -----DB1 Exec_Master_Log_Pos($exec_pos) is behind Read_Master_Log_Pos($read_pos), wait......" >> /etc/keepalived/logs/state_change.log
    sleep 1
done

###然后解除DB1的read_only属性
echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 0 on DB1-----" >> /etc/keepalived/logs/state_change.log
ssh 192.168.244.145 'mysql -uroot -p123456 -e "set global read_only = 0;" && /etc/init.d/keepalived start' 2>> /etc/keepalived/logs/state_change.log

###重启DB2的keepalived使VIP漂移到DB1
echo -e "`date " %F  %H:%M:%S"`    -----make VIP move to DB1-----" >> /etc/keepalived/logs/state_change.log
/sbin/service keepalived restart &>> /etc/keepalived/logs/state_change.log

echo "DB2 keepalived转为BACKUP状态,线上数据库切换至DB1"|mail -s "DB2 keepalived change to BACKUP" [email protected]126.com 2>> /etc/keepalived/logs/state_change.log

echo -e "--------------------------------------------------n" >> /etc/keepalived/logs/state_change.log

 

总结:

1. /etc/keepalived/check_mysql.sh和/etc/keepalived/notify_master_mysql.sh必需加可进行权限。

    就算后边三个没有加可实践权限,则master1上将不会绑定VIP,日志直接提示如下音讯:

May 25 14:37:09 master1 Keepalived_vrrp[3165]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 25 14:37:09 master1 Keepalived_vrrp[3165]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
May 25 14:37:50 master1 Keepalived_vrrp[3165]: VRRP_Instance(VI_1) Now in FAULT state

2. 在Keepalived中有三种方式,分别是master->backup情势和backup->backup情势,这两种情势有哪些分歧吗?

    在master->backup格局下,一旦主库宕掉,设想IP会自动漂移到从库,当主库修复后,keepalived运行后,还大概会把虚构IP抢过来,纵然你设置nopreempt(不抢占)的法子并吞IP的动作也会产生。在backup->backup方式下,当主库宕掉后设想IP会自动漂移到从库上,当原主恢复以后重启keepalived服务,并不会抢占新主的设想IP,就算是优先级高于从库的优先等级,也不会抢占IP。为了减小IP的飘浮次数,生产中大家常见是把修复好的主库当做新主库的备库。

  1. 正文是在MySQL主库高可用 -- 双主单活故障自动切换方案 基础上,结合自个儿对此MySQL的敞亮整理的。原著的脚本直接实施稍微难题,思路有有一点缺点,于是结成本身的其实条件,重新修改了一把。

  2. 在测量试验的经过中,有以下几点要求在乎:

    1> master1检验脚本的逻辑是一旦MySQL的劳务不可用,则透过service keepalived stop命令来关闭keepalived,但在其实测验的进度中,却出现了就是实行了service keepalived stop命令,keepalived进程依然未有止住,导致MySQL的劳动就算不可用了,但VIP并不未有漂移到master2上。

         优化方案:在推行service keepalived stop后,等待5s,再一次检查实验keepalived的景色,倘诺keepalived未有关闭,则一向kill掉。

    2>  keepalived的日记私下认可是出口到/var/log/messages中,那样不便利查看。如何自定义keepalived的日志输出文件呢?

          如若是用service运转的,修改/etc/sysconfig/keepalived文件

KEEPALIVED_OPTIONS="-D -d -S 0" 

         借使不是,则运转的时候钦定以上参数,如:

/usr/local/keepalived/sbin/keepalived -d -D -S 0 

        修改/etc/syslog.conf

# keepalived -S 0 
local0.*                                                /var/log/keepalived.log

       重启syslog

       RHEL 5&6:service syslog restart

       RHEL 7:service rsyslog restart 

        

 

  

 

 

 

        

       

 

 

 

   

  

     

      

    

 

MySQL的高可用方案经常有如下两种: keepalived 双主,MHA,MMM,Heartbeat DRBD,PXC,Galera...

MySQL的高可用方案有众多,例如Cluster,MMM,MHA,DRBD等,这么些都相比较复杂,笔者前面的稿子也是有介绍。最近Oracle官方也生产了Fabric。临时大家没有必要这么复杂的条件,那么些方案各有长短。有时简单的且我们能够hold住的方案才是相符大家的。例如MySQL Replication,然后加上各个高可用软件,举个例子Keepalived等,就能够促成大家须要的高可用情况。

keepalived 双主,MHA,MMM,Heartbeat DRBD,PXC,Galera Cluster

 原文:

MySQL架构为master/slave,当master故障时,vip漂移到slave上。提供服务。当然也得以设置为双master,不过不是每种方案都以完美的。这里安装为双master有个难题亟需在意,例如,当客商公布小说时,由于此时主机的下压力异常的大时,若是落后3000秒,那么那台主机宕机了,另一台主机接管(vip漂移到从机上)时,因为一块延时大,顾客方才公布的稿子还没复制过来,于是顾客又发布了一次小说,当原本的master修复好后,由于I/O和SQL线程还处在展开状态,因而还有或许会继续共同刚才未有一齐复制完的数目,那时有十分大可能率把客商新公布的篇章更换掉。这里所以接纳master/slave架构。在这种框架结构中,故障切换未来,接纳手动操作的诀窍与新的master实行复制。

正如常用的是keepalived 双主,MHA和PXC。

MySQL的高可用方案日常有如下两种:

归纳情形如下:

对于小商铺,平常推荐应用keepalived 双主,轻松。

keepalived 双主,MHA,MMM,Heartbeat DRBD,PXC,Galera Cluster

澳门新萄京官方网站 1

上边来配置一下

相比常用的是keepalived 双主,MHA和PXC。

master     192.168.0.100
slave      192.168.0.101
VIP        192.168.0.88

 

对于小集团,日常推荐应用keepalived 双主,轻巧。

主从复制遭逢的搭建自个儿那边就不演示了。有亟待的同桌团结看看合法手册。下边直接介绍keepalived的设置及陈设利用。

配备境况:

上面来布置一下

1.keepalived软件设置(主从操作同样)

剧中人物                                    主机IP                    主机名               操作系统版本     软件版本

 

[root@mysql-server-01 ~]# wget -q http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
[root@mysql-server-01 ~]# tar xf keepalived-1.2.13.tar.gz
[root@mysql-server-01 ~]# cd keepalived-1.2.13
[root@mysql-server-01 keepalived-1.2.13]# ./configure && make && make install

[root@mysql-server-01 keepalived]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@mysql-server-01 keepalived]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@mysql-server-01 keepalived]# mkdir /etc/keepalived
[root@mysql-server-01 keepalived]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@mysql-server-01 keepalived]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@mysql-server-01 keepalived]# chkconfig --add keepalived
[root@mysql-server-01 keepalived]# chkconfig --level 345 keepalived on

VIP                                    192.168.244.10

配备情形:

2.主从的布局文件修改(主的keepalived配置文件修改后如下,其实差别样的就优先级而已)
master的keepalived配置文件如下

master1                             192.168.244.145       master1            CentOS7.1       MySQL 5.6.26,Keepalived v1.2.13

剧中人物                                    主机IP                    主机名               操作系统版本     软件版本

[root@mysql-server-01 keepalived]# cat keepalived.conf
global_defs {
   router_id MySQL-HA
} 

vrrp_script check_run {
script "/data/sh/mysql_check.sh"
interval 300
}

vrrp_sync_group VG1 {
group {
VI_1
}
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1  
    virtual_router_id 51
    priority 100  
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_run
    }

    notify_master /data/sh/master.sh
    notify_backup /data/sh/backup.sh
    notify_stop /data/sh/stop.sh

    virtual_ipaddress {
        192.168.0.88
    }
}

[root@mysql-server-01 keepalived]# 

master2                             192.168.244.146       master2            CentOS7.1       MySQL 5.6.26,Keepalived v1.2.13

VIP                                    192.168.244.10

slave的keepalived配置文件修改之后如下:

 

master1                             192.168.244.145       master1            CentOS7.1       MySQL 5.6.26,Keepalived v1.2.13

[root@mysql-server-02 keepalived]# cat keepalived.conf
global_defs {
   router_id MySQL-HA
} 

vrrp_script check_run {
script "/data/sh/mysql_check.sh"
interval 300
}

vrrp_sync_group VG1 {
group {
VI_1
}
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 90 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    check_run
    }

    notify_master /data/sh/master.sh
    notify_backup /data/sh/backup.sh
    notify_stop /data/sh/stop.sh

    virtual_ipaddress {
        192.168.0.88
    }
}
[root@mysql-server-02 keepalived]# 

一、 配置MySQL双主复制意况

master2                             192.168.244.146       master2            CentOS7.1       MySQL 5.6.26,Keepalived v1.2.13

个中有多少个至关心珍视要参数的地点:
notify_master:状态更改为master以往实施的台本。

     1. 改造配置文件

 

notify_backup: 状态更动为backup未来实施的脚本。

      master第11中学关于复制的配备如下:

一、 配置MySQL双主复制情状

notify_fault: 状态改造为fault后实行的脚本。

[mysqld]
log-bin=mysql-bin
server-id=1
log_slave_updates=1

     1. 修改配置文件

notify_stop: V奇骏RP结束以往实践的脚本。

     master2

      master第11中学关于复制的安插如下:

state backup:大家都安装为了backup,便是为着爆发故障现在不会自动切换。

[mysqld]
log-bin=mysql-bin
server-id=2
log_slave_updates=1
read_only=1
[mysqld]
log-bin=mysql-bin
server-id=1
log_slave_updates=1

nopreempt: 不开展抢占操作

   2. 开立复制客户

     master2

个中使用了那4个剧本:backup.sh  master.sh  mysql_check.sh  stop.sh

    master1中创建:

[mysqld]
log-bin=mysql-bin
server-id=2
log_slave_updates=1
read_only=1

mysql_check.sh是为了检查mysqld进度是或不是存活的剧本,当开掘一而再不上mysql,自动把keepalived进度干掉,让VIP实行漂移。

CREATE USER 'repl'@'192.168.244.146' IDENTIFIED BY 'mysql';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.244.146';

   2. 创制复制顾客

下边包车型客车脚本主从服务器上边都有,只是从服务器上边包车型大巴master.sh有个别分歧样。增多了当slave升高为主库时,发送邮件文告。

    master2中创建:

    master1中创建:

澳门新萄京官方网站 2澳门新萄京官方网站 3

CREATE USER 'repl'@'192.168.244.145' IDENTIFIED BY 'mysql';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.244.145';
CREATE USER 'repl'@'192.168.244.146' IDENTIFIED BY 'mysql';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.244.146';
[root@mysql-server-01 sh]# cat mysql_check.sh 
#!/bin/bash

. /root/.bash_profile

count=1

while true
do

mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14520.sock -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
   exit 0
else
   if [ $i = 1 ] && [ $j = 0 ]
   then
       exit 0
   else
        if [ $count -gt 5 ]
        then
              break
        fi
   let count  
   continue
   fi
fi

done

/etc/init.d/keepalived stop
[root@mysql-server-01 sh]# 

  3. 执行CHANGE MASTER TO语句

    master2中创建:

View Code

     因是最早搭建MySQL主从复制集群,所以无需得到全局读锁来赢得二进制日志文件的职分,直接遵照show master status的输出来认可。

CREATE USER 'repl'@'192.168.244.145' IDENTIFIED BY 'mysql';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.244.145';

master.sh的职能是状态改为master以后试行的台本。首先决断复制是或不是有延迟,要是有延迟,等1分钟后,不论是不是有延期。都跳过,并终止复制。並且授权账号,记录binlog和pos点。

     master1上执行:

  3. 执行CHANGE MASTER TO语句

澳门新萄京官方网站 4澳门新萄京官方网站 5

CHANGE MASTER TO
  MASTER_HOST='192.168.244.146',
  MASTER_USER='repl',
  MASTER_PASSWORD='mysql',
  MASTER_LOG_FILE='mysql-bin.000004',
  MASTER_LOG_POS=64729;

     因是开始搭建MySQL主从复制集群,所以没有须要获得全局读锁来博取二进制日志文件的岗位,直接根据show master status的输出来承认。

[root@mysql-server-02 sh]# cat master.sh 
#!/bin/bash

. /root/.bash_profile

Master_Log_File=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $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"
   break
else
   sleep 1

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

mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "stop slave;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status;" > /tmp/master_status_$(date " %y%m%d-%H%M").txt


[root@mysql-server-02 sh]# 

    master2上执行:

     master1上执行:

View Code

CHANGE MASTER TO
  MASTER_HOST='192.168.244.145',
  MASTER_USER='repl',
  MASTER_PASSWORD='mysql',
  MASTER_LOG_FILE='mysql-bin.000003',
  MASTER_LOG_POS=68479;
CHANGE MASTER TO
  MASTER_HOST='192.168.244.146',
  MASTER_USER='repl',
  MASTER_PASSWORD='mysql',
  MASTER_LOG_FILE='mysql-bin.000004',
  MASTER_LOG_POS=64729;

slave上的master.sh

    4. 分头在八个节点上实践start slave语句并因此show slave statusG查看复制是还是不是搭建成功。

    master2上执行:

澳门新萄京官方网站 6澳门新萄京官方网站 7

        成功规范:

CHANGE MASTER TO
  MASTER_HOST='192.168.244.145',
  MASTER_USER='repl',
  MASTER_PASSWORD='mysql',
  MASTER_LOG_FILE='mysql-bin.000003',
  MASTER_LOG_POS=68479;
[root@mysql-server-02 sh]# cat master.sh 
#!/bin/bash

. /root/.bash_profile

Master_Log_File=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show slave statusG" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $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"
   break
else
   sleep 1

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

mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "stop slave;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master status;" > /tmp/master_status_$(date " %y%m%d-%H%M").txt


#当slave提升为主以后,发送邮件
echo "#####################################" > /tmp/status
echo "salve已经提升为主库,请进行检查!" >> /tmp/status
ifconfig | sed -n '/inet /{s/.*addr://;s/ .*//;p}' | grep -v 127.0.0.1 >> /tmp/status
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -Nse "show variables like 'port'" >> /tmp/status
echo "#####################################" >> /tmp/status
master=`cat /tmp/status`
echo "$master" | mutt -s "slave to primary!!!" 13143753516@139.com
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

    4. 个别在五个节点上实行start slave语句并透过show slave statusG查看复制是或不是搭建成功。

View Code

 

        成功规范:

剧本中反省复制是不是延时的思维如下:
1、首先看 Relay_Master_Log_File 和 Master_Log_File 是或不是有距离
2、如果Relay_Master_Log_File 和 Master_Log_File 大有径庭的话,那表明延迟比较大了
3、如果Relay_Master_Log_File 和 Master_Log_File 无差别,再来看Exec_Master_Log_Pos 和 Read_Master_Log_Pos 的差异

二、 配置Keepalived

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

并非透过Seconds_Behind_Master去看清,该值表示slave上SQL线程和IO线程之间的延期,实际上还要考虑到 Master_Log_File 和 Relay_Master_Log_File 是或不是大有径庭,更加小心的则是要同不时间在master上进行show master status进行对照。那也是MHA在切换进程中得以成功的。MMM的切换也只是在从库上实施了show slave status。所以数据一致性供给仍旧MHA给力。扯远了。^_^

     1. 安装Keepalived

 

backup.sh脚本的功效是状态改动为backup将来施行的本子。

      # yum install -y keepalived

二、 配置Keepalived

澳门新萄京官方网站 8澳门新萄京官方网站 9

      当然,也可一贯编写翻译官方的源码包。

     1. 安装Keepalived

[root@mysql-server-02 sh]# cat backup.sh 
#!/bin/bash

. /root/.bash_profile

mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global event_scheduler=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=0;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=0;"

     2. 修改Keepalived的布署文件

      # yum install -y keepalived

View Code

     master1

      当然,也可径直编写翻译官方的源码包。

stop.sh 代表keepalived结束以往供给实行的本子。退换密码,设置参数,检查是否还可能有写入操作,最终无论是不是进行完成,都退出。

澳门新萄京官方网站MySQL实现高可用,基于keepalived搭建MySQL的高可用集群。     [root@master1 ~]# vim /etc/keepalived/keepalived.conf

     2. 退换Keepalived的安插文件

澳门新萄京官方网站 10澳门新萄京官方网站 11

vrrp_script chk_mysql {
    script "/etc/keepalived/check_mysql.sh"
    interval 30         #设置检查间隔时长,可根据自己的需求自行设定
}
vrrp_instance VI_1 {
    state BACKUP        #通过下面的priority来区分MASTER和BACKUP,也只有如此,底下的nopreempt才有效
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt           #防止切换到从库后,主keepalived恢复后自动切换回主库
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_mysql
    }

    virtual_ipaddress {
        192.168.244.10/24
    }
}

     master1

[root@mysql-server-02 sh]# cat stop.sh 
#!/bin/bash

. /root/.bash_profile

mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '1q2w3e4r';flush privileges;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_support_xa=1;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global sync_binlog=1;"
mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "set global innodb_flush_log_at_trx_commit=1;"

M_File1=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master statusG" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master statusG" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master statusG" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql -uroot -pmsandbox -S /tmp/mysql_sandbox14521.sock -e "show master statusG" | awk -F': ' '/Position/{print $2}')

i=1

while true
do

if [ $M_File1 = $M_File1 ] && [ $M_Position1 -eq $M_Position2 ]
then
   echo "ok"
   break
else
   sleep 1

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


[root@mysql-server-02 sh]# 

有关keepalived的参数的详细介绍,可参照他事他说加以考察:LVS Keepalived搭建MyCAT高可用负载均衡集群

     [root@master1 ~]# vim /etc/keepalived/keepalived.conf

View Code

其中,/etc/keepalived/check_mysql.sh内容如下:

澳门新萄京官方网站 12

到此处基本就介绍完了。最后大家先看主从复制是不是正规,借使不奇怪,然后分别运营keepalived,然后实行故障切换测量试验。

#!/bin/bash

###判断如果上次检查的脚本还没执行完,则退出此次执行
if [ `ps -ef|grep -w "$0"|grep -v "grep"|wc -l` -gt 2 ];then
    exit 0
fi 
mysql_con='mysql -uroot -p123456'
error_log="/etc/keepalived/logs/check_mysql.err"

###定义一个简单判断mysql是否可用的函数
function excute_query {
    ${mysql_con} -e "select 1;" 2>> ${error_log}
}

###定义无法执行查询,且mysql服务异常时的处理函数
function service_error {
    echo -e "`date " %F  %H:%M:%S"`    -----mysql service error,now stop keepalived-----" >> ${error_log}
    service keepalived stop &>> ${error_log}
    echo "DB1 keepalived 已停止"|mail -s "DB1 keepalived 已停止,请及时处理!" slowtech@126.com 2>> ${error_log}
    echo -e "n---------------------------------------------------------n" >> ${error_log}
}

###定义无法执行查询,但mysql服务正常的处理函数
function query_error {
    echo -e "`date " %F  %H:%M:%S"`    -----query error, but mysql service ok, retry after 30s-----" >> ${error_log}
    sleep 30
    excute_query
    if [ $? -ne 0 ];then
        echo -e "`date " %F  %H:%M:%S"`    -----still can't execute query-----" >> ${error_log}

        ###对DB1设置read_only属性
        echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 1 on DB1-----" >> ${error_log}
        mysql_con -e "set global read_only = 1;" 2>> ${error_log}

        ###kill掉当前客户端连接
        echo -e "`date " %F  %H:%M:%S"`    -----kill current client thread-----" >> ${error_log}
        rm -f /tmp/kill.sql &>/dev/null
        ###这里其实是一个批量kill线程的小技巧
        mysql_con -e 'select concat("kill ",id,";") from  information_schema.PROCESSLIST where command="Query" or command="Execute" into outfile "/tmp/kill.sql";'
        mysql_con -e "source /tmp/kill.sql"
        sleep 2    ###给kill一个执行和缓冲时间
        ###关闭本机keepalived       
        echo -e "`date " %F  %H:%M:%S"`    -----stop keepalived-----" >> ${error_log}
        service keepalived stop &>> ${error_log}
        echo "DB1 keepalived 已停止"|mail -s "DB1 keepalived 已停止,请及时处理!" slowtech@126.com 2>> ${error_log}
        echo -e "n---------------------------------------------------------n" >> ${error_log}
    else
        echo -e "`date " %F  %H:%M:%S"`    -----query ok after 30s-----" >> ${error_log}
        echo -e "n---------------------------------------------------------n" >> ${error_log}
    fi
}

###检查开始: 执行查询
excute_query
if [ $? -ne 0 ];then
    service mysqld status &>/dev/null
    if [ $? -ne 0 ];then
        service_error
    else
        query_error
    fi
fi
vrrp_script chk_mysql {
    script "/etc/keepalived/check_mysql.sh"
    interval 30         #设置检查间隔时长,可根据自己的需求自行设定
}
vrrp_instance VI_1 {
    state BACKUP        #通过下面的priority来区分MASTER和BACKUP,也只有如此,底下的nopreempt才有效
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt           #防止切换到从库后,主keepalived恢复后自动切换回主库
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_mysql
    }

    virtual_ipaddress {
        192.168.244.10/24
    }
}

slave状态:

由此实际的查询语句来决断数据库服务的可用性,假诺查询战败,则剖断mysqld进度自身的气象,倘使不正规,则直接甘休当前节点的keepalived,将VIP转移到其它三个节点,若无难点,则等待30s,再度实行查询语句,依然败诉,则将眼下的master节点设置为read_only,并kill掉当前的顾客端连接,然后结束当前的keepalived。

澳门新萄京官方网站 13

node2 [localhost] {msandbox} ((none)) > pager cat | egrep 'Master_Log_File|Relay_Master_Log_File|Read_Master_Log_Pos|Exec_Master_Log_Pos|Running'
PAGER set to 'cat | egrep 'Master_Log_File|Relay_Master_Log_File|Read_Master_Log_Pos|Exec_Master_Log_Pos|Running''
node2 [localhost] {msandbox} ((none)) > show slave statusG
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 409
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
          Exec_Master_Log_Pos: 409
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)

node2 [localhost] {msandbox} ((none)) > 

       

关于keepalived的参数的详尽介绍,可参照:LVS Keepalived搭建MyCAT高可用负载均衡集群

master 状态:

       master2 

其中,/etc/keepalived/check_mysql.sh内容如下:

node1 [localhost] {msandbox} ((none)) > show master status;
 ------------------ ---------- -------------- ------------------ ------------------- 
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
 ------------------ ---------- -------------- ------------------ ------------------- 
| mysql-bin.000001 |      409 |              |                  |                   |
 ------------------ ---------- -------------- ------------------ ------------------- 
1 row in set (0.00 sec)

node1 [localhost] {msandbox} ((none)) > 

       [root@master2 ~]# vim /etc/keepalived/keepalived.conf

澳门新萄京官方网站 14

依据自己后面给的判定标准,能够看见笔者的复制未有别的延时。
下边分别在master上和slave上运行keepalived进度。以及查看日志(上边的查看只是给我们表达什么判别复制是不是推迟)

! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master /etc/keepalived/notify_master_mysql.sh    #此条指令告诉keepalived发现自己转为MASTER后执行的脚本
    virtual_ipaddress {
        192.168.244.10/24
    }
}
#!/bin/bash

###判断如果上次检查的脚本还没执行完,则退出此次执行
if [ `ps -ef|grep -w "$0"|grep -v "grep"|wc -l` -gt 2 ];then
    exit 0
fi 
mysql_con='mysql -uroot -p123456'
error_log="/etc/keepalived/logs/check_mysql.err"

###定义一个简单判断mysql是否可用的函数
function excute_query {
    ${mysql_con} -e "select 1;" 2>> ${error_log}
}

###定义无法执行查询,且mysql服务异常时的处理函数
function service_error {
    echo -e "`date " %F  %H:%M:%S"`    -----mysql service error,now stop keepalived-----" >> ${error_log}
    service keepalived stop &>> ${error_log}
    echo "DB1 keepalived 已停止"|mail -s "DB1 keepalived 已停止,请及时处理!" slowtech@126.com 2>> ${error_log}
    echo -e "n---------------------------------------------------------n" >> ${error_log}
}

###定义无法执行查询,但mysql服务正常的处理函数
function query_error {
    echo -e "`date " %F  %H:%M:%S"`    -----query error, but mysql service ok, retry after 30s-----" >> ${error_log}
    sleep 30
    excute_query
    if [ $? -ne 0 ];then
        echo -e "`date " %F  %H:%M:%S"`    -----still can't execute query-----" >> ${error_log}

        ###对DB1设置read_only属性
        echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 1 on DB1-----" >> ${error_log}
        mysql_con -e "set global read_only = 1;" 2>> ${error_log}

        ###kill掉当前客户端连接
        echo -e "`date " %F  %H:%M:%S"`    -----kill current client thread-----" >> ${error_log}
        rm -f /tmp/kill.sql &>/dev/null
        ###这里其实是一个批量kill线程的小技巧
        mysql_con -e 'select concat("kill ",id,";") from  information_schema.PROCESSLIST where command="Query" or command="Execute" into outfile "/tmp/kill.sql";'
        mysql_con -e "source /tmp/kill.sql"
        sleep 2    ###给kill一个执行和缓冲时间
        ###关闭本机keepalived       
        echo -e "`date " %F  %H:%M:%S"`    -----stop keepalived-----" >> ${error_log}
        service keepalived stop &>> ${error_log}
        echo "DB1 keepalived 已停止"|mail -s "DB1 keepalived 已停止,请及时处理!" slowtech@126.com 2>> ${error_log}
        echo -e "n---------------------------------------------------------n" >> ${error_log}
    else
        echo -e "`date " %F  %H:%M:%S"`    -----query ok after 30s-----" >> ${error_log}
        echo -e "n---------------------------------------------------------n" >> ${error_log}
    fi
}

###检查开始: 执行查询
excute_query
if [ $? -ne 0 ];then
    service mysqld status &>/dev/null
    if [ $? -ne 0 ];then
        service_error
    else
        query_error
    fi
fi

master

其中,/etc/keepalived/notify_master_mysql.sh的剧情如下:

澳门新萄京官方网站 15

澳门新萄京官方网站 16澳门新萄京官方网站 17

#!/bin/bash
###当keepalived监测到本机转为MASTER状态时,执行该脚本

change_log=/etc/keepalived/logs/state_change.log
mysql_con='mysql -uroot -p123456'
echo -e "`date " %F  %H:%M:%S"`   -----keepalived change to MASTER-----" >> $change_log

slave_info() {
    ###统一定义一个函数取得slave的position、running、和log_file等信息
    ###根据函数后面所跟参数来决定取得哪些数据
    if [ $1 = slave_status ];then
        slave_stat=`${mysql_con} -e "show slave statusG;"|egrep -w "Slave_IO_Running|Slave_SQL_Running"`
        Slave_IO_Running=`echo $slave_stat|awk '{print $2}'`
        Slave_SQL_Running=`echo $slave_stat|awk '{print $4}'`
    elif [ $1 = log_file -a $2 = pos ];then
        log_file_pos=`${mysql_con} -e "show slave statusG;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"`
        Master_Log_File=`echo $log_file_pos|awk '{print $2}'`
        Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'`
        Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'`
        Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'`
    fi
}

action() {
    ###经判断'应该&可以'切换时执行的动作
    echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 0 on DB2-----" >> $change_log

    ###解除read_only属性
    ${mysql_con} -e "set global read_only = 0;" 2>> $change_log

    echo "DB2 keepalived转为MASTER状态,线上数据库切换至DB2"|mail -s "DB2 keepalived change to MASTER"
    slowtech@126.com 2>> $change_log

    echo -e "---------------------------------------------------------n" >> $change_log
}

slave_info slave_status
if [ $Slave_SQL_Running = Yes ];then
    i=0    #一个计数器
    slave_info log_file pos
        ###判断从master接收到的binlog是否全部在本地执行(这样仍无法完全确定从库已追上主库,因为无法完全保证io_thread没有延时(由网络传输问题导致的从库落后的概率很小)
    until [ $Master_Log_File = $Relay_Master_Log_File -a $Read_Master_Log_Pos = $Exec_Master_Log_Pos ]
     do
        if [ $i -lt 10 ];then    #将等待exec_pos追上read_pos的时间限制为10s
            echo -e "`date " %F  %H:%M:%S"`    -----Relay_Master_Log_File=$Relay_Master_Log_File,Exec_Master_Log_Pos=$Exec_Master_Log_Pos is behind Master_Log_File=$Master_Log_File,Read_Master_Log_Pos=$Read_Master_Log_Pos, wait......" >> $change_log    #输出消息到日志,等待exec_pos=read_pos
            i=$(($i 1))
            sleep 1
            slave_info log_file pos
        else
            echo -e "The waits time is more than 10s,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log
            action
            exit 0
        fi
    done
    action 

else
    slave_info log_file pos
    echo -e "DB2's slave status is wrong,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log
    action
fi

因此实际的查询语句来决断数据库服务的可用性,假若查询战败,则剖断mysqld进程本人的状态,如若不正规,则间接停止当前节点的keepalived,将VIP转移到别的一个节点,假若平常,则等待30s,再一次实践查询语句,照旧败诉,则将日前的master节点设置为read_only,并kill掉当前的顾客端连接,然后截至当前的keepalived。

[root@mysql-server-01 sh]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[root@mysql-server-01 sh]# tail -f /var/log/messages
Jul 20 20:48:03 mysql-server-01 Keepalived_vrrp[13040]: Netlink reflector reports IP 192.168.87.134 added
Jul 20 20:48:03 mysql-server-01 Keepalived_vrrp[13040]: Netlink reflector reports IP 192.168.0.100 added
Jul 20 20:48:03 mysql-server-01 Keepalived_vrrp[13040]: Registering Kernel netlink reflector
Jul 20 20:48:03 mysql-server-01 Keepalived_vrrp[13040]: Registering Kernel netlink command channel
Jul 20 20:48:03 mysql-server-01 Keepalived_vrrp[13040]: Registering gratuitous ARP shared channel
Jul 20 20:48:03 mysql-server-01 Keepalived_healthcheckers[13039]: Netlink reflector reports IP 192.168.0.100 added
Jul 20 20:48:03 mysql-server-01 Keepalived_healthcheckers[13039]: Netlink reflector reports IP 192.168.87.134 added
Jul 20 20:48:03 mysql-server-01 Keepalived_healthcheckers[13039]: Netlink reflector reports IP 192.168.0.100 added
Jul 20 20:48:03 mysql-server-01 Keepalived_healthcheckers[13039]: Registering Kernel netlink reflector
Jul 20 20:48:03 mysql-server-01 Keepalived_healthcheckers[13039]: Registering Kernel netlink command channel
Jul 20 20:48:23 mysql-server-01 Keepalived_healthcheckers[13039]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 20 20:48:23 mysql-server-01 Keepalived_healthcheckers[13039]: Configuration is using : 6489 Bytes
Jul 20 20:48:23 mysql-server-01 Keepalived_vrrp[13040]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 20 20:48:23 mysql-server-01 Keepalived_vrrp[13040]: Configuration is using : 66476 Bytes
Jul 20 20:48:23 mysql-server-01 Keepalived_vrrp[13040]: Using LinkWatch kernel netlink reflector...
Jul 20 20:48:23 mysql-server-01 Keepalived_healthcheckers[13039]: Using LinkWatch kernel netlink reflector...
Jul 20 20:48:23 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 20 20:48:23 mysql-server-01 Keepalived_vrrp[13040]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 20 20:48:23 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Script(check_run) succeeded
Jul 20 20:48:27 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 20 20:48:27 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Group(VG1) Syncing instances to MASTER state
Jul 20 20:48:28 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 20 20:48:28 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 20 20:48:28 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.0.88
Jul 20 20:48:28 mysql-server-01 Keepalived_healthcheckers[13039]: Netlink reflector reports IP 192.168.0.88 added
Jul 20 20:48:33 mysql-server-01 Keepalived_vrrp[13040]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.0.88

 

       

View Code

成套脚本的逻辑是让从的Exec_Master_Log_Pos尽或许的追上Read_Master_Log_Pos,它给了10s的限定,假若仍旧不曾追上,则平昔将master2设置为主(通过免去read_only属性),其实那之中只怕有待商榷的,举例10s的范围是不是合理,照旧自然要求Exec_Master_Log_Pos=Read_Master_Log_Pos才切换。

       master2 

slave

 

       [root@master2 ~]# vim /etc/keepalived/keepalived.conf

澳门新萄京官方网站 18澳门新萄京官方网站 19

当原主恢复生机不奇怪后,怎么样将VIP从master2切回到master第11中学吗?

澳门新萄京官方网站 20

[root@mysql-server-02 tmp]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[root@mysql-server-02 tmp]# tail -f /var/log/messages
Jul 20 20:48:14 mysql-server-02 Keepalived_vrrp[10680]: Netlink reflector reports IP fe80::20c:29ff:fefe:dc91 added
Jul 20 20:48:14 mysql-server-02 Keepalived_healthcheckers[10679]: Netlink reflector reports IP 192.168.0.101 added
Jul 20 20:48:14 mysql-server-02 Keepalived_healthcheckers[10679]: Netlink reflector reports IP fe80::20c:29ff:fefe:dc91 added
Jul 20 20:48:14 mysql-server-02 Keepalived_vrrp[10680]: Netlink reflector reports IP fe80::20c:29ff:fefe:dc9b added
Jul 20 20:48:14 mysql-server-02 Keepalived_vrrp[10680]: Registering Kernel netlink reflector
Jul 20 20:48:14 mysql-server-02 Keepalived_healthcheckers[10679]: Netlink reflector reports IP fe80::20c:29ff:fefe:dc9b added
Jul 20 20:48:14 mysql-server-02 Keepalived_healthcheckers[10679]: Registering Kernel netlink reflector
Jul 20 20:48:14 mysql-server-02 Keepalived_vrrp[10680]: Registering Kernel netlink command channel
Jul 20 20:48:14 mysql-server-02 Keepalived_healthcheckers[10679]: Registering Kernel netlink command channel
Jul 20 20:48:14 mysql-server-02 Keepalived_vrrp[10680]: Registering gratuitous ARP shared channel
Jul 20 20:48:34 mysql-server-02 Keepalived_healthcheckers[10679]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 20 20:48:34 mysql-server-02 Keepalived_healthcheckers[10679]: Configuration is using : 6467 Bytes
Jul 20 20:48:34 mysql-server-02 Keepalived_vrrp[10680]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 20 20:48:34 mysql-server-02 Keepalived_vrrp[10680]: Configuration is using : 66454 Bytes
Jul 20 20:48:34 mysql-server-02 Keepalived_vrrp[10680]: Using LinkWatch kernel netlink reflector...
Jul 20 20:48:34 mysql-server-02 Keepalived_healthcheckers[10679]: Using LinkWatch kernel netlink reflector...
Jul 20 20:48:34 mysql-server-02 Keepalived_vrrp[10680]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 20 20:48:34 mysql-server-02 Keepalived_vrrp[10680]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 20 20:48:35 mysql-server-02 Keepalived_vrrp[10680]: VRRP_Script(check_run) succeeded
#!/bin/bash
###手动执行将主库切换回DB1的操作

mysql_con='mysql -uroot -p123456'

echo -e "`date " %F  %H:%M:%S"`    -----change to BACKUP manually-----" >> /etc/keepalived/logs/state_change.log
echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 1 on DB2-----" >> /etc/keepalived/logs/state_change.log
$mysql_con -e "set global read_only = 1;" 2>> /etc/keepalived/logs/state_change.log

###kill掉当前客户端连接
echo -e "`date " %F  %H:%M:%S"`    -----kill current client thread-----" >> /etc/keepalived/logs/state_change.log
rm -f /tmp/kill.sql &>/dev/null
###这里其实是一个批量kill线程的小技巧
$mysql_con -e 'select concat("kill ",id,";") from  information_schema.PROCESSLIST where command="Query" or command="Execute" into outfile "/tmp/kill.sql";'
$mysql_con -e "source /tmp/kill.sql" 2>> /etc/keepalived/logs/state_change.log
sleep 2    ###给kill一个执行和缓冲时间

###确保DB1已经追上了,下面的repl为复制所用的账户,-h后跟DB1的内网IP
log_file_pos=`mysql -urepl -pmysql -h192.168.244.145 -e "show slave statusG;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"`
Master_Log_File=`echo $log_file_pos|awk '{print $2}'`
Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'`
Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'`
Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'`
until [ $Read_Master_Log_Pos = $Exec_Master_Log_Pos -a $Master_Log_File = $Relay_Master_Log_File ]
do
    echo -e "`date " %F  %H:%M:%S"`    -----DB1 Exec_Master_Log_Pos($exec_pos) is behind Read_Master_Log_Pos($read_pos), wait......" >> /etc/keepalived/logs/state_change.log
    sleep 1
done

###然后解除DB1的read_only属性
echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 0 on DB1-----" >> /etc/keepalived/logs/state_change.log
ssh 192.168.244.145 'mysql -uroot -p123456 -e "set global read_only = 0;" && /etc/init.d/keepalived start' 2>> /etc/keepalived/logs/state_change.log

###重启DB2的keepalived使VIP漂移到DB1
echo -e "`date " %F  %H:%M:%S"`    -----make VIP move to DB1-----" >> /etc/keepalived/logs/state_change.log
/sbin/service keepalived restart &>> /etc/keepalived/logs/state_change.log

echo "DB2 keepalived转为BACKUP状态,线上数据库切换至DB1"|mail -s "DB2 keepalived change to BACKUP" slowtech@126.com 2>> /etc/keepalived/logs/state_change.log

echo -e "--------------------------------------------------n" >> /etc/keepalived/logs/state_change.log
! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master /etc/keepalived/notify_master_mysql.sh    #此条指令告诉keepalived发现自己转为MASTER后执行的脚本
    virtual_ipaddress {
        192.168.244.10/24
    }
}

View Code

 

澳门新萄京官方网站 21

能够望见VIP已经绑定在了master上,实行ip addr看看是还是不是有那些VIP

总结:

其中,/etc/keepalived/notify_master_mysql.sh的情节如下:

[root@mysql-server-01 ~]# ip addr | grep eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.0.100/24 brd 192.168.0.255 scope global eth1
    inet 192.168.0.88/32 scope global eth1
[root@mysql-server-01 ~]# 

1. /etc/keepalived/check_mysql.sh和/etc/keepalived/notify_master_mysql.sh必得加可实践权限。

澳门新萄京官方网站 22

能够见到vip也早已绑定成功。

    若是前面二个未有加可实践权限,则master1上将不会绑定VIP,日志直接提醒如下音讯:

#!/bin/bash
###当keepalived监测到本机转为MASTER状态时,执行该脚本

change_log=/etc/keepalived/logs/state_change.log
mysql_con='mysql -uroot -p123456'
echo -e "`date " %F  %H:%M:%S"`   -----keepalived change to MASTER-----" >> $change_log

slave_info() {
    ###统一定义一个函数取得slave的position、running、和log_file等信息
    ###根据函数后面所跟参数来决定取得哪些数据
    if [ $1 = slave_status ];then
        slave_stat=`${mysql_con} -e "show slave statusG;"|egrep -w "Slave_IO_Running|Slave_SQL_Running"`
        Slave_IO_Running=`echo $slave_stat|awk '{print $2}'`
        Slave_SQL_Running=`echo $slave_stat|awk '{print $4}'`
    elif [ $1 = log_file -a $2 = pos ];then
        log_file_pos=`${mysql_con} -e "show slave statusG;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"`
        Master_Log_File=`echo $log_file_pos|awk '{print $2}'`
        Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'`
        Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'`
        Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'`
    fi
}

action() {
    ###经判断'应该&可以'切换时执行的动作
    echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 0 on DB2-----" >> $change_log

    ###解除read_only属性
    ${mysql_con} -e "set global read_only = 0;" 2>> $change_log

    echo "DB2 keepalived转为MASTER状态,线上数据库切换至DB2"|mail -s "DB2 keepalived change to MASTER"
    slowtech@126.com 2>> $change_log

    echo -e "---------------------------------------------------------n" >> $change_log
}

slave_info slave_status
if [ $Slave_SQL_Running = Yes ];then
    i=0    #一个计数器
    slave_info log_file pos
        ###判断从master接收到的binlog是否全部在本地执行(这样仍无法完全确定从库已追上主库,因为无法完全保证io_thread没有延时(由网络传输问题导致的从库落后的概率很小)
    until [ $Master_Log_File = $Relay_Master_Log_File -a $Read_Master_Log_Pos = $Exec_Master_Log_Pos ]
     do
        if [ $i -lt 10 ];then    #将等待exec_pos追上read_pos的时间限制为10s
            echo -e "`date " %F  %H:%M:%S"`    -----Relay_Master_Log_File=$Relay_Master_Log_File,Exec_Master_Log_Pos=$Exec_Master_Log_Pos is behind Master_Log_File=$Master_Log_File,Read_Master_Log_Pos=$Read_Master_Log_Pos, wait......" >> $change_log    #输出消息到日志,等待exec_pos=read_pos
            i=$(($i 1))
            sleep 1
            slave_info log_file pos
        else
            echo -e "The waits time is more than 10s,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log
            action
            exit 0
        fi
    done
    action 

else
    slave_info log_file pos
    echo -e "DB2's slave status is wrong,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log
    action
fi

当今我们从远程机器登录看看,使用vip,创造测量检验库,插入数据,最终模拟mysqld crash

May 25 14:37:09 master1 Keepalived_vrrp[3165]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 25 14:37:09 master1 Keepalived_vrrp[3165]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
May 25 14:37:50 master1 Keepalived_vrrp[3165]: VRRP_Instance(VI_1) Now in FAULT state

澳门新萄京官方网站 23

澳门新萄京官方网站 24澳门新萄京官方网站 25

2. 在Keepalived中有二种形式,分别是master->backup情势和backup->backup情势,那二种方式有哪些区别吗?

 

[root@mysql-server-03 ~]# mysql -uadmin -p123456 -h 192.168.0.88 -P 14520   
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 47
Server version: 5.6.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> create database dengyayun;
Query OK, 1 row affected (0.01 sec)

mysql> use dengyayun
Database changed
mysql> create table t1 ( id int);
Query OK, 0 rows affected (0.38 sec)

mysql> insert into t1 select 999;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> 

    在master->backup方式下,一旦主库宕掉,设想IP会自动漂移到从库,当主库修复后,keepalived运维后,还只怕会把虚构IP抢过来,纵然你设置nopreempt(不抢占)的点子抢占IP的动作也会产生。在backup->backup情势下,当主库宕掉后设想IP会自动漂移到从库上,当原主苏醒现在重启keepalived服务,并不会抢占新主的虚拟IP,尽管是事先级高于从库的优先等级,也不会抢占IP。为了减小IP的飘浮次数,生产中大家常见是把修复好的主库充作新主库的备库。

漫天脚本的逻辑是让从的Exec_Master_Log_Pos尽只怕的追上Read_Master_Log_Pos,它给了10s的限定,如若依然尚未追上,则平昔将master2设置为主(通过化解read_only属性),其实那其中或许有待商谈的,比如10s的范围是还是不是站得住,依然自然供给Exec_Master_Log_Pos=Read_Master_Log_Pos才切换。

View Code

  1. 正文是在MySQL主库高可用 -- 双主单活故障自动切换方案 基础上,结合自身对于MySQL的了解整理的。原著的本子直接实施多少难题,思路有有一点点缺点,于是结成本人的莫过于条件,重新修改了一把。

  2. 在测验的历程中,有以下几点须求在意:

 

察觉使用vip登录没至极,创造库以及插入数据都木有标题。现在杀掉mysqld进度,看vip是还是不是开展了上浮,以及查看数据是还是不是存在。

    1> master1检查测验脚本的逻辑是一旦MySQL的服务不可用,则透过service keepalived stop命令来关闭keepalived,但在其实地衡量试的长河中,却出现了正是施行了service keepalived stop命令,keepalived进度依然未有甘休,导致MySQL的劳务固然不可用了,但VIP并不未有漂移到master2上。

当原主恢复生机平常后,怎样将VIP从master2切回到master第11中学吗?

[root@mysql-server-01 ~]# pkill -9 mysqld

         优化方案:在推行service keepalived stop后,等待5s,再一次检验keepalived的动静,假如keepalived未有苏息,则一向kill掉。

澳门新萄京官方网站 26

过了一会儿,报警邮件就到了,以及vip也曾经切换了。如下:
澳门新萄京官方网站 27

    2>  keepalived的日记暗中同意是出口到/var/log/messages中,那样不方便人民群众查看。怎么样自定义keepalived的日志输出文件呢?

#!/bin/bash
###手动执行将主库切换回DB1的操作

mysql_con='mysql -uroot -p123456'

echo -e "`date " %F  %H:%M:%S"`    -----change to BACKUP manually-----" >> /etc/keepalived/logs/state_change.log
echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 1 on DB2-----" >> /etc/keepalived/logs/state_change.log
$mysql_con -e "set global read_only = 1;" 2>> /etc/keepalived/logs/state_change.log

###kill掉当前客户端连接
echo -e "`date " %F  %H:%M:%S"`    -----kill current client thread-----" >> /etc/keepalived/logs/state_change.log
rm -f /tmp/kill.sql &>/dev/null
###这里其实是一个批量kill线程的小技巧
$mysql_con -e 'select concat("kill ",id,";") from  information_schema.PROCESSLIST where command="Query" or command="Execute" into outfile "/tmp/kill.sql";'
$mysql_con -e "source /tmp/kill.sql" 2>> /etc/keepalived/logs/state_change.log
sleep 2    ###给kill一个执行和缓冲时间

###确保DB1已经追上了,下面的repl为复制所用的账户,-h后跟DB1的内网IP
log_file_pos=`mysql -urepl -pmysql -h192.168.244.145 -e "show slave statusG;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"`
Master_Log_File=`echo $log_file_pos|awk '{print $2}'`
Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'`
Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'`
Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'`
until [ $Read_Master_Log_Pos = $Exec_Master_Log_Pos -a $Master_Log_File = $Relay_Master_Log_File ]
do
    echo -e "`date " %F  %H:%M:%S"`    -----DB1 Exec_Master_Log_Pos($exec_pos) is behind Read_Master_Log_Pos($read_pos), wait......" >> /etc/keepalived/logs/state_change.log
    sleep 1
done

###然后解除DB1的read_only属性
echo -e "`date " %F  %H:%M:%S"`    -----set read_only = 0 on DB1-----" >> /etc/keepalived/logs/state_change.log
ssh 192.168.244.145 'mysql -uroot -p123456 -e "set global read_only = 0;" && /etc/init.d/keepalived start' 2>> /etc/keepalived/logs/state_change.log

###重启DB2的keepalived使VIP漂移到DB1
echo -e "`date " %F  %H:%M:%S"`    -----make VIP move to DB1-----" >> /etc/keepalived/logs/state_change.log
/sbin/service keepalived restart &>> /etc/keepalived/logs/state_change.log

echo "DB2 keepalived转为BACKUP状态,线上数据库切换至DB1"|mail -s "DB2 keepalived change to BACKUP" slowtech@126.com 2>> /etc/keepalived/logs/state_change.log

echo -e "--------------------------------------------------n" >> /etc/keepalived/logs/state_change.log

查看slave上边的message音讯,如下输出:

          假如是用service运转的,修改/etc/sysconfig/keepalived文件

澳门新萄京官方网站 28

[root@mysql-server-02 ~]# tail -n 20 /var/log/messages 
Jul 20 22:00:20 mysql-server-02 Keepalived_healthcheckers[13327]: Registering Kernel netlink command channel
Jul 20 22:00:40 mysql-server-02 Keepalived_vrrp[13328]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 20 22:00:40 mysql-server-02 Keepalived_vrrp[13328]: Configuration is using : 66454 Bytes
Jul 20 22:00:40 mysql-server-02 Keepalived_vrrp[13328]: Using LinkWatch kernel netlink reflector...
Jul 20 22:00:40 mysql-server-02 Keepalived_healthcheckers[13327]: Opening file '/etc/keepalived/keepalived.conf'.
Jul 20 22:00:40 mysql-server-02 Keepalived_healthcheckers[13327]: Configuration is using : 6467 Bytes
Jul 20 22:00:40 mysql-server-02 Keepalived_healthcheckers[13327]: Using LinkWatch kernel netlink reflector...
Jul 20 22:00:40 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 20 22:00:40 mysql-server-02 Keepalived_vrrp[13328]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Jul 20 22:00:40 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Script(check_run) succeeded
Jul 20 22:07:47 mysql-server-02 dhclient[7343]: DHCPREQUEST on eth0 to 192.168.87.254 port 67 (xid=0x4ada08db)
Jul 20 22:07:47 mysql-server-02 dhclient[7343]: DHCPACK from 192.168.87.254 (xid=0x4ada08db)
Jul 20 22:07:49 mysql-server-02 dhclient[7343]: bound to 192.168.87.135 -- renewal in 885 seconds.
Jul 20 22:10:38 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 20 22:10:38 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Group(VG1) Syncing instances to MASTER state
Jul 20 22:10:39 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 20 22:10:39 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 20 22:10:39 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.0.88
Jul 20 22:10:39 mysql-server-02 Keepalived_healthcheckers[13327]: Netlink reflector reports IP 192.168.0.88 added
Jul 20 22:10:44 mysql-server-02 Keepalived_vrrp[13328]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.0.88
[root@mysql-server-02 ~]# 
KEEPALIVED_OPTIONS="-D -d -S 0" 

 

最终我们再一次使用vip登录;开采数目未有丰裕。复制也截止了,因为早就切换为主库。

         如若不是,则运营的时候钦点以上参数,如:

总结:

[root@mysql-server-03 ~]# mysql -uadmin -p123456 -h 192.168.0.88 -P14521
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 301
Server version: 5.6.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> select * from dengyayun.t1;
 ------ 
| id   |
 ------ 
|  999 |
 ------ 
1 row in set (0.00 sec)

mysql> pager cat | egrep 'IO_Running|SQL_Running'
PAGER set to 'cat | egrep 'IO_Running|SQL_Running''
mysql> show slave statusG
             Slave_IO_Running: No
            Slave_SQL_Running: No
      Slave_SQL_Running_State: 
1 row in set (0.00 sec)

mysql> 
/usr/local/keepalived/sbin/keepalived -d -D -S 0 

1. /etc/keepalived/check_mysql.sh和/etc/keepalived/notify_master_mysql.sh必须加可实践权限。

        修改/etc/syslog.conf

    要是前者未有加可实践权限,则master1上将不会绑定VIP,日志直接提醒如下消息:

# keepalived -S 0 
local0.*                                                /var/log/keepalived.log
May 25 14:37:09 master1 Keepalived_vrrp[3165]: VRRP_Instance(VI_1) Entering BACKUP STATE
May 25 14:37:09 master1 Keepalived_vrrp[3165]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
May 25 14:37:50 master1 Keepalived_vrrp[3165]: VRRP_Instance(VI_1) Now in FAULT state

       重启syslog

2. 在Keepalived中有二种格局,分别是master->backup方式和backup->backup方式,那三种形式有如何不一致吗?

       RHEL 5&6:service syslog restart

    在master->backup格局下,一旦主库宕掉,设想IP会自动漂移到从库,当主库修复后,keepalived启动后,还大概会把虚构IP抢过来,纵然你设置nopreempt(不抢占)的方法侵占IP的动作也会发出。在backup->backup方式下,当主库宕掉后设想IP会自动漂移到从库上,当原主恢复生机之后重启keepalived服务,并不会抢占新主的设想IP,就算是初期级高于从库的初期等级,也不会抢占IP。为了减小IP的漂流次数,生产中我们平常是把修复好的主库当作新主库的备库。

       RHEL 7:service rsyslog restart

  1. 本文是在MySQL主库高可用 -- 双主单活故障自动切换方案 基础上,结合本人对此MySQL的精晓整理的。原来的文章的脚本直接施行多少难点,思路有有一点点短处,于是结成自身的其实条件,重新修改了一把。

  2. 在测验的经过中,有以下几点需求留意:

   

    1> master1检查测量试验脚本的逻辑是一旦MySQL的劳务不可用,则透过service keepalived stop命令来关闭keepalived,但在其实地衡量试的进度中,却现身了正是推行了service keepalived stop命令,keepalived进度依然未有小憩,导致MySQL的劳务即便不可用了,但VIP并不未有漂移到master2上。

  

         优化方案:在施行service keepalived stop后,等待5s,再度检查评定keepalived的状态,假若keepalived未有关闭,则平昔kill掉。

     

    2>  keepalived的日记私下认可是出口到/var/log/messages中,那样不便利查看。怎样自定义keepalived的日志输出文件呢?

      

          假使是用service运转的,修改/etc/sysconfig/keepalived文件

    

KEEPALIVED_OPTIONS="-D -d -S 0" 

 

         假诺不是,则运行的时候钦赐以上参数,如:

/usr/local/keepalived/sbin/keepalived -d -D -S 0 

        修改/etc/syslog.conf

# keepalived -S 0 
local0.*                                                /var/log/keepalived.log

       重启syslog

       RHEL 5&6:service syslog restart

       RHEL 7:service rsyslog restart

本文由澳门新萄京官方网站发布于数据库网络,转载请注明出处:澳门新萄京官方网站MySQL实现高可用,基于keepa

关键词: