heartbeate+mysql+drbd 高可用数据库配置

heartbeate   mysql   drbd   系统服务  
1、环境
  • 系统:Centos 6.4 x64
  • 主MYSQLA: 192.168.0.16
  • 从MYSQLB: 192.168.0.17
  • VIP: 192.168.0.18
  • 目录: /data

2、安装
  • 基本安装
yum install heartbeat heartbeat-ldirectord heartbeat-pils heartbeat-stonith -y
yum install mysql mysql-server mysql-client mysql-devel -y
yum install *drbd83* -y
* 定义主机名,主机和从机分别添加hostname
 #vim /etc/hosts
 192.168.0.16 mysqlA
 192.168.0.17 mysqlB
  • c.配置drbd

Maste Node:

vim /etc/drbd.conf
global { usage-count yes; }
common { syncer { rate 100M; }}
resource r0 {
        protocol C;
        startup {
        }
        disk {
                on-io-error detach;
                #size 1G;
        }
        net {
        }
        on mysqlA {
                device /dev/drbd0;
                disk /dev/sda5;
                address 192.168.0.16:7898;
                meta-disk internal;
        }
        on mysqlB {
                device /dev/drbd0;
                disk /dev/sda5;
                address 192.168.0.17:7898;
                meta-disk internal;
        }
}
Slave Node:
global { usage-count yes; }
common { syncer { rate 100M; }}
resource r0 {
        protocol C;
        startup {

        }
        disk {
                on-io-error detach;
                #size 1G;
        }
        net {
        }
        on mysqlA {
                device /dev/drbd0;
                disk /dev/sda5;
                address 192.168.0.16:7898;
                meta-disk internal;
        }
        on mysqlB {
                device /dev/drbd0;
                disk /dev/sda5;
                address 192.168.0.17:7898;
                meta-disk internal;
        }
}
* d.添加一个空白硬盘主从分别添加
fdisk /dev/sda5
mkfs.ext4 /dev/sda5
dd if=/dev/zero of=/dev/sda5 bs=1M count=1;sync
#创建matadata(主从服务器)
drbdadm create-md r0 或者 drbdadm create-md all
/etc/init.d/drbd start
#主节点上执行
drbdadm -- --overwrite-data-of-peer primary all       主节点的块设备才可以被挂载
mkfs.ext3  /dev/drbd0 
mount /dev/drbd0 /data  
* e.节点切换
主
  首先 umount /data
  然后 drbdadm secondary all
次
  首先 drbdadm primary all
  然后 umount /data
  • f.配置mysql (主次节点分别配置)
 cat /etc/my.cnf 
[mysqld]
datadir=/data/mysql/data
/usr/bin/mysql_install_db --user=mysql --datadir=/data/mysql/data
service mysqld start

3、heartbeat安装配置
  • a.配置authkeys
    vim /etc/ha.d/authkeys
    auth 3
      3 md5 hellomysql
    chmod 600 /etc/ha.d/authkeys 
  • b.配置ha.cf
vim /etc/ha.d/ha.cf
    debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 20
warntime 10
initdead 60
udpport 693
ucast eth0 192.168.0.16
ping_group group1 192.168.0.16 192.168.0.17
auto_failback off
node mysqlA
node mysqlB
hopfudge 1
  • c.配置haresources
    vim /etc/ha.d/haresources
    mysqlA  drbddisk::r0 Filesystem::/dev/drbd0::/data/::ext3 mysqld IPaddr::192.168.0.18/24/eth0:1
     主机名      drbd块设备      系统盘  数据库  VIP地址  网卡子接口
  • d.启动headtbeat
/etc/init.d/heartbeat start
  • f.查看启动后状态,主要看VIP是否启动
    [root@mysqlA ha.d]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:EA:35:AB  
          inet addr:192.168.0.16  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12393 errors:0 dropped:0 overruns:0 frame:0
          TX packets:147269 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1219718 (1.1 MiB)  TX bytes:218236336 (208.1 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:EA:35:AB  
          inet addr:192.168.0.18  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2460 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2460 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:466838 (455.8 KiB)  TX bytes:466838 (455.8 KiB)

断开主节点后,是否会自动切换为从节点。