数据库相关 / 负载均衡高可用

drbd+heartbeat(v1)+mysql搭建高可用数据库并保留原有硬盘数据[原创]

随着两篇drbd相关文章的完成,蚊子对drbd的理解更进了一层,本篇文章就在前两篇的基础上结合了Heartbeat来搭建一个mysql高可用的应用,并保留原有mysql数据的内容。

先来介绍一下蚊子的环境吧

Vmware6.0
             linux01 192.168.211.128
             linux02 192.168.211.129

mysql数据路径/data/mysql
因为没有合适的mysql数据所以使用wordpress的数据,wordpress2.8.5
nginx version: nginx/0.8.20
PHP 5.2.11 (cli) (built: Oct 23 2009 21:01:40)

一,准备工作

linux01上完成的内容

1,准备一个含有mysql数据的硬盘分区

蚊子这里没有现成的mysql数据,就使用wordpress的数据库内容了,mysql的版本不限,可以是二进制解压用的,也可以是源码编译的,蚊子这里是用的二进制的,操作如下

清空分区内容
[[email protected] ~]# mkfs.ext3 /dev/VG/data

挂载分区
[[email protected] ~]# mount /dev/VG/data  /data/

初始化数据库

[[email protected] mysql]# /usr/local/mysql/scripts/mysql_install_db –user=mysql –datadir=/data/mysql
Installing MySQL system tables…
091025 19:33:53 [Warning] option ‘thread_stack’: unsigned value 65536 adjusted to 131072
OK
Filling help tables…
091025 19:33:53 [Warning] option ‘thread_stack’: unsigned value 65536 adjusted to 131072
OK

在此处安装wordpress,将wordpress数据库安装到blog库中,如下
[[email protected] mysql]# ll /data/mysql/
total 20572
drwx—— 2 mysql mysql     4096 Oct 27 17:26 blog
-rw-rw—- 1 mysql mysql 10485760 Oct 27 17:25 ibdata1
-rw-rw—- 1 mysql mysql  5242880 Oct 27 17:25 ib_logfile0
-rw-rw—- 1 mysql mysql  5242880 Oct 27 17:25 ib_logfile1
-rw-rw—- 1 mysql root      1247 Oct 27 17:25 linux01.err
-rw-rw—- 1 mysql mysql        5 Oct 27 17:25 linux01.pid
drwx—— 2 mysql root      4096 Oct 27 17:25 mysql
drwx—— 2 mysql root      4096 Oct 27 17:25 test

卸载/data分区
[[email protected] ~]# umount /data/

2,备份分区第一扇区

[[email protected] mysql]# dd if=/dev/VG/data of=/root/back bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.049084 seconds, 21.4 MB/s

3,清空分区第一扇区

[[email protected] mysql]# dd if=/dev/zero of=/dev/VG/data bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.0131647 seconds, 79.7 MB/s

4,创建drbd.conf文件,内容如下

global {

    usage-count yes;
}

common {
  syncer { rate 10M; }
}

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/heartbeat/drbd-peer-outdater -t 5";

  }

  disk {
    on-io-error   detach;
  }

  net {

    after-sb-0pri disconnect;

    after-sb-1pri disconnect;

    after-sb-2pri disconnect;

    rr-conflict disconnect;

  }

  syncer {
    rate 10M;
    al-extents 257;

  }

  on linux01 {
    device     /dev/drbd0;
    disk       /dev/VG/data;
    address    192.168.211.128:7788;
    flexible-meta-disk  internal;
  }

  on linux02 {
    device    /dev/drbd0;
    disk      /dev/VG/data;
    address   192.168.211.129:7788;
    meta-disk internal;
  }
}

5,初始化drbd磁盘

[[email protected] mysql]# drbdadm create-md r0
Writing meta data…
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
success

[[email protected] mysql]# /etc/init.d/drbd start
Starting DRBD resources: [ d(r0) s(r0) n(r0) ].

确认drbd是否启动
[[email protected] mysql]# cat /proc/drbd
version: 8.3.4 (api:88/proto:86-91)
GIT-hash: 70a645ae080411c87b4482a135847d69dc90a6a2 build by [email protected], 2009-10-22 21:01:53
0: cs:WFConnection ro:Secondary/Unknown ds:UpToDate/DUnknown C r—-
    ns:0 nr:589768 dw:589768 dr:0 al:0 bm:36 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0

将当前机器上的drbd设为primary
[[email protected] mysql]# drbdsetup /dev/drbd0 primary –o

查看当前状态

[[email protected] mysql]# cat /proc/drbd
version: 8.3.4 (api:88/proto:86-91)
GIT-hash: 70a645ae080411c87b4482a135847d69dc90a6a2 build by [email protected], 2009-10-22 21:01:53
0: cs:WFConnection ro:Primary/Unknown ds:UpToDate/DUnknown C r—-
    ns:0 nr:589768 dw:589768 dr:0 al:0 bm:36 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0

6,恢复分区第一分区,这也是保证数据还在的关键一步

[[email protected] mysql]# dd if=/root/back of=/dev/drbd0
2048+0 records in
2048+0 records out
1048576 bytes (1.0 MB) copied, 0.113655 seconds, 9.2 MB/s

7,重启drbd服务

[[email protected] mysql]# /etc/init.d/drbd restart
Restarting all DRBD resources: .

到此为止,linux01上的准备工作就做好了,下面看下linux02上的操作

因为linux02是备份用的,基本不用怎么设置,如果分区里面含有数据的话,就直接执行下面的操作就行

拷贝linux01上/etc/drbd.conf到linux02上的/etc下

[[email protected] ~]# dd if=/dev/zero of=/dev/VG/data bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.0141756 seconds, 74.0 MB/s

[[email protected] ~]# drbdadm create-md r0
Writing meta data…
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
success

[[email protected] ~]# /etc/init.d/drbd start        
Starting DRBD resources: [ d(r0) ].

查看一下状态

[[email protected] ~]# cat /proc/drbd
version: 8.3.4 (api:88/proto:86-91)
GIT-hash: 70a645ae080411c87b4482a135847d69dc90a6a2 build by [email protected], 2009-10-22 21:01:33
0: cs:SyncTarget ro:Secondary/Secondary ds:Inconsistent/UpToDate C r—-
    ns:0 nr:184320 dw:184320 dr:0 al:0 bm:11 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:405448
        [=====>…………..] sync’ed: 32.0% (405448/589768)K
        finish: 0:00:34 speed: 11,696 (10,240) K/sec

现在两台机器上的准备工作都完毕了,进入到下面一个阶段

二,drbd,Heartbeat和mysql整合工作

两台机器上的公共操作

yum -y install heartbeat heartbeat-devel

编辑ha.cf文件,内容如下

debugfile /var/log/ha-debug
logfacility     local0
keepalive 2
deadtime 5

warntime 10
initdead 10
ucast eth0 192.168.211.129    #在两台机器上的内容是不一样的,都是指向对方的ip地址
auto_failback on
node    linux01
node    linux02
ping 192.168.211.2
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster

编辑authkeys文件,内容如下

auth 1
1 crc

修改此文件权限为600

编辑haresources,内容如下

linux01 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext3 192.168.211.120 mysqld

第一个字段是主机名,是uname -n得到的
第二个字段作用是当前主机设置为primary
第三个字段作用将/dev/drbd0以ext3格式mount到/data目录上
第四个字段作用是启动VIP
第五个字段启动mysql进程

这个文件在启动的时候是由左往右启动,停止就是相反的由右往左停止。

分配权限:

chgrp haclient /sbin/drbdsetup
chmod o-x /sbin/drbdsetup
chmod u+s /sbin/drbdsetup
chgrp haclient /sbin/drbdmeta
chmod o-x /sbin/drbdmeta
chmod u+s /sbin/drbdmeta

三,启动测试阶段

到现在,全部配置就已经完成了,可以进行测试了

现在两台机器上都启动heartbeat,可以看到第一台机器的状态

VIP已经启动
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:71:2E:11 
          inet addr:192.168.211.120  Bcast:192.168.211.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Base address:0x2000

mysql已经顺利启动
2845 ?        S      0:00 /bin/sh ./bin/mysqld_safe –datadir=/data/mysql –pid-file=/data/mysql/linux01.pid
2945 ?        Sl     0:00 /usr/local/mysql/bin/mysqld –basedir=/usr/local/mysql –datadir=/data/mysql –user=mysql –log-error=/data/mysql/linux01.err –pid-file=/data/mysql/linux01.pid –socket=/tmp/mysql.sock –port=3306

drbd0设备mount上了
/dev/drbd0            567M   38M  501M   8% /data

这时停止linux01上的heartbeat,看下linux02上的状态

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:50:E4:62 
          inet addr:192.168.211.120  Bcast:192.168.211.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:169 Base address:0x2000

[[email protected] ~]# ps ax|grep mysql|grep -v grep
2582 ?        S      0:00 /bin/sh ./bin/mysqld_safe –datadir=/data/mysql –pid-file=/data/mysql/linux02.pid
2682 ?        Sl     0:00 /usr/local/mysql/bin/mysqld –basedir=/usr/local/mysql –datadir=/data/mysql –user=mysql –log-error=/data/mysql/linux02.err –pid-file=/data/mysql/linux02.pid –socket=/tmp/mysql.sock –port=3306

[[email protected] ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VG-root    6919612   1866388   4696056  29% /
/dev/sda1               101086     12072     83795  13% /boot
tmpfs                   127772         0    127772   0% /dev/shm
/dev/drbd0              580540     38872    512180   8% /data

这时如果在linux02上也启动nginx的话,使用vip是可以正常访问wordpress的页面的,这里蚊子就不上图了。到此这篇文章就全部完成了,需要的朋友可以亲自去试试,切记,如果需要在正式环境中应用并保证原有数据存在,请做好相应的备份工作。

Related Post

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *