HeartBeat+MySQL+DRBD

先卸载两边节点原来磁盘设备所对应的挂载目录,并在fstab里注释掉相关行,防止系统启动时自动挂载


yum install heartbeat.x86_64 heartbeat-libs.x86_64 -y

1.两边节点的mysql的uid和gid要一致

2.附件内heartbeat的配置文件都放在/etc/ha.d/下

3.DRBD和Heartbeat的通信采用网线直连方式,以防干扰,避免交换机单点故障.

4.Service需要如下设置,两边节点需一致

mysqld开机不启动,

drbd开机启动

heartbeart开机启动




加上静态路由直走该网卡通信,em2为DRBD直连网卡,放入

HB1(10.0.1.11)

route add -host 10.0.1.12 dev em2

HB2(10.0.1.12)

route add -host 10.0.1.11 dev em2


加上静态路由直走该网卡通信,eth2为Heartbeat直连网卡

DRBD1(10.0.0.11)

route add -host 10.0.0.12 dev eth2

DRBD2(10.0.0.11)

route add -host 10.0.0.11 dev eth2


ha.cf


该文件2边配置文件都保持一致,但是如果心跳采用单播模式,记得两边配置文件对应行需要修改成对方节点IP

===============================================================================

debugfile /var/log/ha-debug

logfile /var/log/ha-log
logfacility local1
#options configure
keepalive 2
deadtime 30
warntime 10
initdead 120
#心跳网卡上的组播
#mcast eth2 225.0.0.7 694 1 0    //多节点采用组播方式,不过双节点也可以
ucast eth2 192.168.0.82       //双节点可以考虑采用单播方式,IP填写对方节点即可
#node configure
#不要自动failback
auto_failback off          //主节点宕机恢复后,不会自动接管资源,数据库没事别切来切去.
node    cluster1          //主机名,使用uname -n查看
node    cluster2
crm no

===============================================================================


haresources


该文件2边配置文件都保持一致

1.IPaddr,drbddisk,Filesystem,mysqld都是存在于/etc/ha.d/resource.d或者/etc/init.d下的文件,heartbeat会自动去这2个目录下寻找对应的命令

2.IPaddr::192.168.1.151/24/eth1,表示VIP的地址/掩码/以及VIP绑定在哪块网卡上.

3.drbddisk::r0,这里的r0是drbd的resource name,在drbd.conf可以查看

===============================================================================

cluster1 IPaddr::192.168.1.151/24/eth1 drbddisk::r0 Filesystem::/dev/drbd0::/var/lib/mysql::ext4 mysqld

===============================================================================


authkeys


该文件2边配置文件都保持一致

该文件权限属性必须是600  

===============================================================================

auth 1

1 sha1 47e9336850f1db6fa58bc470bc9b7810eb397f04

===============================================================================


drbd.conf

===============================================================================

#
# please have a a look at the example configuration file in
# /usr/share/doc/drbd83/drbd.conf
#
global {
     # minor-count 64;
     # dialog-refresh 5; # 5 seconds
     # disable-ip-verification;
     usage-count no;
}

common {
     syncer { rate 100M; }
}

resource r0 {
     protocol C;
     handlers {
          pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
          pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
          local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f";
          fence-peer "/usr/lib/drbd/crm-fence-peer.sh";
          after-resync-target "/usr/lib/drbd/crm-unfence-peer.sh";
}

net {
     # timeout               60;
     # connect-int          10;
     # ping-int               10;
     # max-buffers          2048;
     # max-epoch-size     2048;
    after-sb-0pri disconnect;
    after-sb-1pri disconnect;
    after-sb-2pri disconnect;
    rr-conflict disconnect;
     cram-hmac-alg     "sha1";
     shared-secret     "FDD123456";
    # 使用dopd (drbd outdate-peer daemon) 功能保证在数据不同步时不进行切换(Heartbeat)

}

disk {
     on-io-error     detach;
#     fencing          resource-only;
     # 使用dopd (drbd outdate-peer daemon) 功能保证在数据不同步时不进行切换
}

startup {
     wfc-timeout 120;
     degr-wfc-timeout 120;
}

#on之后必须是主机名,使用uname -n得到
on cluster1 {
     device     /dev/drbd0;
     disk     /dev/sdb1;
     address     192.168.0.81:7788;
     meta-disk     internal;
}

on cluster2 {
     device     /dev/drbd0;
     disk     /dev/sdb1;
     address     192.168.0.82:7788;
     meta-disk     internal;
}

}

===============================================================================


chk_mysql.sh


这个文件的作用是检查MySQL的进程是否存在,如果不存在则杀死heartbeat,触发备节点接管业务.

该文件加上可执行权限后放在/usr/bin下,然后放在/etc/init.d/mysql的start段的末尾:

case "$1" in
  start)
    start
    bash /usr/bin/chk_mysql.sh  &
    ;;

这样就能随mysql启动而启动了

==============================================================================

#!/bin/bash
#filename:chk_mysql.sh
CHK_MYSQL=$(ps -C chk_mysql.sh --no-header|wc -l)
if [[ "$CHK_MYSQL" -gt 0 ]]
        then

         echo "chk_mysql.sh is started"

                exit 0
else
while test 1 = 1
do
DRBD=$(drbd-overview |awk '{print $3}'|awk -F\/ '{print $1}')
if [[ "$DRBD" = "Primary" ]]
        then
                echo "DRBD is Primary,it's going to check MySQL status"
                MYSQL=$(ps -C mysqld --no-header|wc -l)
                if [[ "$MYSQL" -eq 0 ]]
                        then
                                echo "MySQL is not running,pkill heartheart"
                 $(killall heartbeat) && sleep 60 && break
             else
                                echo "MySQL is runing,do nothing"
                                sleep 60
                fi
        else
                echo "DRBD is not Primary,do nothing"
                sleep 60
fi
done

fi

===========================================================




你可能感兴趣的:(drbd)