Heartbeat + Drbd +Mysql 构建高可用的MYSQL数据库服务

一、什么是 DRBD
DRBD是由内核模块和相关脚本而构成,用以构建高可用性的集群。其实现方式是通过网络来镜像整个设备。您可以把它看作是一种网络RAID1。
Drbd 负责接收数据,把数据写到本地磁盘,然后发送给另一个主机。另一个主机再将数据存到自己的磁盘中。其他所需的组件有集群成员服务,如TurboHA 或 心跳连接,以及一些能在块设备上运行的应用程序


二、搭建实验环境
1,  两台CentOS5.5机器,没有环境的可以虚拟机搭建
CentOS 1
IP:192.168.1.251
CentOS 2
IP:192.168.1.252
2,修改两机的/etc/hosts文件,两个都改成一样
  1. [root@node1 ~]# cat /etc/hosts
  2. # Do not remove the following line, or various programs
  3. # that require network functionality will fail.
  4. 127.0.0.1               CentOS localhost.localdomain localhost
  5. ::1             localhost6.localdomain6 localhost6
  6. 192.168.1.251           node1
  7. 192.168.1.252           node2
复制代码
修改完成之后,重启,使得机器名分别是node1  node2
如果没有修改成功,请查看别的文件是否还有设定HOSTNAME选项,如/etc/sysconfig/network文件等。
在node1上ping node2;在node2上ping node1是相互能ping通的
三、安装 DRBD
1、需要安装 DRBDDRBD内核模块,如果机器联网,要以用YUM来安装
  1. [root@node1 ~]# yum search drbd
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * addons: centos.candishosting.com.cn
  5. * base: centos.ustc.edu.cn
  6. * extras: centos.candishosting.com.cn
  7. * updates: centos.ustc.edu.cn
  8. ================================ Matched: drbd =================================
  9. drbd.i386 : Distributed Redundant Block Device driver for Linux
  10. drbd82.i386 : Distributed Redundant Block Device driver for Linux
  11. drbd83.i386 : Distributed Redundant Block Device driver for Linux
  12. kmod-drbd.i686 : drbd kernel module(s)
  13. kmod-drbd-PAE.i686 : drbd kernel module(s)
  14. kmod-drbd-xen.i686 : drbd kernel module(s)
  15. kmod-drbd82.i686 : drbd82 kernel module(s)
  16. kmod-drbd82-PAE.i686 : drbd82 kernel module(s)
  17. kmod-drbd82-xen.i686 : drbd82 kernel module(s)
  18. kmod-drbd83.i686 : drbd83 kernel module(s)
  19. kmod-drbd83-PAE.i686 : drbd83 kernel module(s)
  20. kmod-drbd83-xen.i686 : drbd83 kernel module(s)
复制代码
可以看到列出相关的包,安装我们相要的,两台都要安装
  1. [root@node1 ~]# yum install -y drbd83 kmod-drbd83
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * addons: centos.candishosting.com.cn
  5. * base: centos.ustc.edu.cn
  6. * extras: centos.candishosting.com.cn
  7. * updates: centos.ustc.edu.cn
  8. Setting up Install Process
  9. Resolving Dependencies
  10. --> Running transaction check
  11. ---> Package drbd83.i386 0:8.3.8-1.el5.centos set to be updated
  12. ---> Package kmod-drbd83.i686 0:8.3.8-1.el5.centos set to be installed
  13. --> Finished Dependency Resolution
  14. ...............略
复制代码
  1. [root@node1 ~]# lsmod | grep drbd
  2. drbd                  228528  3
复制代码
可以看到 DRBD模块已加载成功
2、改写 DRBD的配置文件
分别是两个机器里生成 drbd的配置文件/etc/ drbd.conf,文件内容是相同的
  1. [root@node1 ~]# vi /etc/drbd.conf
  2. #
  3. # please have a a look at the example configuration file in
  4. # /usr/share/doc/drbd83/drbd.conf
  5. global { usage-count yes; }
  6. common { syncer { rate 10M; } }
  7. resource db {
  8.         protocol C;
  9.         net {
  10.         }
  11.         on node1 {
  12.                 device    /dev/drbd1;
  13.                 disk      /dev/sdb1;
  14.                 address   192.168.1.251:7789;
  15.                 meta-disk  internal;
  16.         }
  17.         on node2 {
  18.                 device    /dev/drbd1;
  19.                 disk      /dev/sdb1;
  20.                 address   192.168.1.252:7789;
  21.                 meta-disk  internal;
  22.         }
  23. }
复制代码
3、建立 drbd设备文件:
  1. for i in $(seq 0 15) ; do mknod /dev/drbd$i b 147 $i ; done
复制代码
初始化meta-data area:
  1. [root@node1 ~]# drbdadm create-md db
  2. md_offset 21467942912
  3. al_offset 21467910144
  4. bm_offset 21467254784

  5. Found ext3 filesystem
  6.     20964792 kB data area apparently used
  7.     20964116 kB left usable by current configuration

  8. Device size would be truncated, which
  9. would corrupt data and result in
  10. 'access beyond end of device' errors.
  11. You need to either
  12.    * use external meta data (recommended)
  13.    * shrink that filesystem first
  14.    * zero out the device (destroy the filesystem)
  15. Operation refused.

  16. Command 'drbdmeta 1 v08 /dev/sdb1 internal create-md' terminated with exit code 40
  17. drbdadm create-md db: exited with code 40
复制代码
可以看到,出现了错误
原因
  1. you created your filesystem before you created your DRBD resource, or
  2. you created your filesystem on your backing device, rather than your DRBD,
  3. neither of which is a problem by itself, except �C as the error message tries to hint �C you need to enlarge the device (e.g.

  4. lvextend), shrink the filesystem (e.g. resize2fs), or place the DRBD metadata somewhere else (external meta data).
  5. DRBD tries to detect an existing use of the block device in question. E.g. if it detects an existing file system that uses

  6. all the available space (as is default for most filesystems), and you try to use DRBD with internal meta data, there is no

  7. room for the internal meata data �C creating that would corrupt the last few MiB of the existing file system.
  8. If re-creating the filesystem on the DRBD is an option, one way to “zero out the device (destroy the filesystem)”, and then

  9. recreate it on the DRBD is
  10. 解决办法:初始化磁盘文件格式
  11. dd if=/dev/zero bs=1M count=1 of=/dev/sdXYZ; sync
  12. drbdadm create-md $r
  13. drbdadm -- -o primary $r
  14. mkfs /dev/drbdY
复制代码
有了方法,我们来解决
------------------------------------------------------------------
  1. [root@node1 ~]# dd if=/dev/zero bs=1M count=1 of=/dev/sdb1;sync
  2. 1+0 records in
  3. 1+0 records out
  4. 1048576 bytes (1.0 MB) copied, 0.0570659 seconds, 18.4 MB/s
  5. 再次来初始化meta-data area
  6. [root@node1 ~]# drbdadm create-md db
  7. Writing meta data...
  8. initializing activity log
  9. NOT initialized bitmap
  10. New drbd meta data block successfully created.
  11. success
复制代码
可以看到success,表示成功。以上操作,在两机都要做
-----------------------------------------------------------------
现在我们可以启动 DRBD了,分别在两台主机上执行,以下三种启动方法,都可以
  1. [root@node1 ~]# /etc/init.d/drbd start
  2. [root@node1 ~]# service drbd start
  3. [root@node1 ~]# drbdadm all up
复制代码
查下启动后的端口,在 drbd.conf中有指出
netstat -ant的输出结果里有一行
  1. [root@node1 ~]# netstat -ant
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address               Foreign Address             State
  4. tcp        0      0 192.168.1.251:7789          192.168.1.252:52601         ESTABLISHED
复制代码
3、分别在两机看 drbd当前的状态
  1. [root@node1 ~]# cat /proc/drbd
  2. version: 8.3.8 (api:88/proto:86-94)
  3. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16

  4. 1: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----
  5.     ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:20964116
  6. [root@node2 ~]# cat /proc/drbd
  7. version: 8.3.8 (api:88/proto:86-94)
  8. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
  9. 0: cs:Unconfigured
  10. 1: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----
  11.     ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:20964116
复制代码
"/proc/ drbd"中显示了 drbd当前的状态.ro:Secondary表示两台主机的状态,都是"备机"状态.
ds是磁盘状态,都是"不一致"状态.
这是由于, DRBD无法判断哪一方为主机,以哪一方的磁盘数据作为标准数据.所以,我们需要初始化
4、一个主机.在node1上执行:
  1. [root@node1 ~]#drbdsetup /dev/drbd1 primary -o
复制代码
分别再次在两机看 drbd当前的状态
  1. [root@node1 ~]# cat /proc/drbd
  2. version: 8.3.8 (api:88/proto:86-94)
  3. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16

  4. 1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----
  5.     ns:131140 nr:0 dw:0 dr:132096 al:0 bm:7 lo:0 pe:16 ua:30 ap:0 ep:1 wo:b oos:20833460
  6.         [>....................] sync'ed:  0.7% (20344/20472)M delay_probe: 16
  7.         finish: 0:49:08 speed: 7,040 (6,876) K/sec
复制代码
主备机状态分别是"主/备",主机磁盘状态是"实时",备机状态是"不一致".
在第3行,可以看到数据正在同步中,即主机正在将磁盘上的数据,传递到备机上.现在的进度是[>...................] sync'ed:  0.7%

(20344/20472)M
数据同步完成之后,再次分别看两机的状态
  1. [root@node1 ~]# cat /proc/drbd
  2. version: 8.3.8 (api:88/proto:86-94)
  3. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16

  4. 1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
  5.     ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
  6. [root@node2 ~]# cat /proc/drbd
  7. version: 8.3.8 (api:88/proto:86-94)
  8. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16

  9. 1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----
  10.     ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
复制代码
磁盘状态都是"实时",表示数据同步完成了.
你现在可以把主机上的 DRBD设备挂载到一个目录上进行使用.备机的 DRBD设备无法被挂载,因为它是
用来接收主机数据的,由 DRBD负责操作.
  1. [root@node1 ~]# mkfs.ext3 /dev/drbd1
  2. mke2fs 1.39 (29-May-2006)
  3. Filesystem label=
  4. OS type: Linux
  5. Block size=4096 (log=2)
  6. Fragment size=4096 (log=2)
  7. 2621440 inodes, 5241029 blocks
  8. 262051 blocks (5.00%) reserved for the super user
  9. First data block=0
  10. Maximum filesystem blocks=0
  11. 160 block groups
  12. 32768 blocks per group, 32768 fragments per group
  13. 16384 inodes per group
  14. Superblock backups stored on blocks:
  15.         32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  16.         4096000

  17. Writing inode tables:  45/160
  18. [root@node1 ~]# mount /dev/drbd1 /mnt
  19. [root@node1 ~]# touch /mnt/test.txt
  20. [root@node1 ~]# ls /mnt/
  21. lost+found  test.txt
复制代码
在主机node1上产生的文件test.txt,也完整的保存在备机node2的 DRBD分区上.
这就是 DRBD的网络RAID-1功能. 在主机上的任何操作,都会被同步到备机的相应磁盘分区上,达到数据备份的效果.


DRBD的主备机切换有时,你需要将 DRBD的主备机互换一下.可以执行下面的操作:
在主机上,先要卸载掉 DRBD设备
  1. [root@node1 ~]# umount /mnt
  2. [root@node1 ~]# drbdadm secondary db
  3. [root@node1 ~]# cat /proc/drbd
  4. version: 8.3.8 (api:88/proto:86-94)
  5. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16

  6. 1: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r----
  7.     ns:464580 nr:0 dw:464580 dr:165 al:368 bm:123 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
复制代码
现在,两台主机都是"备机".
在备机node2上,将它升级为"主机".
  1. [root@node2 ~]# drbdadm primary db
  2. [root@node2 ~]# cat /proc/drbd
  3. version: 8.3.8 (api:88/proto:86-94)
  4. GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16

  5. 1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
  6.     ns:0 nr:464580 dw:464580 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
  7. [root@node2 ~]# mount /dev/drbd1 /mnt
  8. [root@node2 ~]# ls /mnt/
  9. lost+found  test.txt
复制代码
现在node2成为主机了

DRBD相关切换命令
DRBD切换
  1. drbdadm secondary r0  //把主机切换成备机
  2. drbdadm primary r0   //把备机切换成主机
复制代码
注意,在我测试的情况。 DRBD只有主机可以读写,备机不能够挂载,也就是说不能够读。可能是我的 drbd.conf文件配置方面的原因

上面的 DRBD已经成功配置完成,可以正常切换,现在我们让两台成为高可用,所谓高可用,就是双机热备,互备。这里我不解释太多,相应搞过HA的人都清楚

四、安装heartbeat mysql mysql-server
1、主机node1和node2上安装heartbeat mysql mysql-server
  1. [root@node1 ~]# yum install -y heartbeat mysql mysql-server
  2. [root@node2 ~]# umount /mnt
  3. [root@node2 ~]# drbdadm secondary db
  4. [root@node1 ~]# drbdadm primary db
  5. [root@node1 ~]# mount /dev/drbd1 /mnt
复制代码
以下只在node1上进行操作
在node1上启动mysql初始化数据库数据文件
  1. [root@node1 ~]# service mysqld start
  2. [root@node1 ~]# service mysqld stop
  3. Stopping MySQL:                                            [  OK  ]
  4. [root@node1 ~]# cp /var/lib/mysql/* /mnt/ -ar
复制代码
---------------------------------------------------------------------
配置heartbeat
  1. [root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.3/haresources /etc/ha.d/
  2. [root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/
  3. [root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/
  4. [root@node1 ~]# chmod 600 /etc/ha.d/authkeys
复制代码
  1. [root@node1 ~]# vi /etc/ha.d/authkeys
  2. authkeys文件中下面两行前面的#号去掉
  3. auth 1
  4. 1 crc
复制代码
配置ha.cf 内容
  1. [root@node1 ~]# vi /etc/ha.d/ha.cf
  2. 看下配置后的文件内容
  3. [root@node1 ~]# grep -v "^#" /etc/ha.d/ha.cf
  4. debugfile /var/log/ha-debug
  5. logfile /var/log/ha-log
  6. logfacility     local0
  7. keepalive 2
  8. deadtime 10
  9. warntime 5
  10. initdead 120
  11. ucast eth0 192.168.1.252
  12. auto_failback on
  13. node    node1
  14. node    node2
  15. ping 192.168.1.1
  16. respawn hacluster /usr/lib/heartbeat/ipfail
  17. apiauth ipfail gid=haclient uid=hacluster
复制代码
添加自动加载 drbd 及持载文件系统的脚本,放在/etc/ha.d/resource.d 目录下面
  1. [root@node1 ~]# vi /etc/ha.d/resource.d/mysqld_umount
  2. #!/bin/sh
  3. unset LC_ALL; export LC_ALL
  4. unset LANGUAGE; export LANGUAGE

  5. prefix=/usr
  6. exec_prefix=/usr
  7. . /etc/ha.d/shellfuncs

  8. case "$1" in
  9. 'start')
  10. #/sbin/drbdadm -- --do-what-I-say primary all
  11. /sbin/drbdadm  primary all
  12. #drbdsetup /dev/drbd1 primary -o
  13. /bin/mount /dev/drbd1 /var/lib/mysql
  14.          ;;
  15. 'pre-start')
  16.          ;;
  17. 'post-start')
  18.          ;;
  19. 'stop')
  20. /bin/umount /var/lib/mysql
  21. /sbin/drbdadm  secondary all
  22. ;;
  23. 'pre-stop')
  24.          ;;
  25. 'post-stop')
  26.          ;;
  27. *)
  28.          echo "Usage: $0 { start | pre-start | post-start | stop | pre-stop | post-stop }"
  29.          ;;

  30. esac
  31. exit 0
  32. -----------------------------------------------------------------
  33. [root@node1 ~]# chmod +x  /etc/ha.d/resource.d/mysqld_umount
复制代码
配置haresource
添加如下一行
  1. node1 IPaddr::192.168.1.250/24/eth0:1 mysqld_umount mysqld
复制代码
-------------------------------------------------------------
工作完成,把/etc/ha.d目录全部覆盖到node2上面去
scp -r /etc/ha.d 192.168.1.252:/etc
  1. [root@node1 ~]# scp -r /etc/ha.d 192.168.1.252:/etc
  2. The authenticity of host '192.168.1.252 (192.168.1.252)' can't be established.
  3. RSA key fingerprint is 6f:e8:cd:f9:b6:8d:aa:4a:e3:28:41:63:86:aa:40:66.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added '192.168.1.252' (RSA) to the list of known hosts.
  6. [email protected]'s password:
复制代码
提示输入对方root用户的密码,全部转输完以后,注意修改/etc/ha.d/ha.cf 中的ucast eth0 192.168.1.252 为对方的IP也就是node1主机的

eth0的IP 改为ucast eth1 192.168.1.251
node1启动heartbeat服务并看日志:
  1. [root@node1 ~]# service heartbeat start
  2. Starting High-Availability services:
  3. 2010/08/20_11:30:18 INFO:  Resource is stopped
  4.                                                            [  OK  ]
  5. [root@node1 ~]# tail /var/log/ha-log -f
  6. heartbeat[6466]: 2010/08/20_11:30:32 info: Status update for node node2: status up
  7. heartbeat[6466]: 2010/08/20_11:30:32 info: Comm_now_up(): updating status to active
  8. heartbeat[6466]: 2010/08/20_11:30:32 info: Local status now set to: 'active'
  9. heartbeat[6466]: 2010/08/20_11:30:32 info: Starting child client "/usr/lib/heartbeat/ipfail" (498,496)
  10. heartbeat[6479]: 2010/08/20_11:30:33 info: Starting "/usr/lib/heartbeat/ipfail" as uid 498  gid 496 (pid 6479)
  11. harc[6475]:     2010/08/20_11:30:33 info: Running /etc/ha.d/rc.d/status status
  12. heartbeat[6466]: 2010/08/20_11:30:33 info: Status update for node node2: status active
  13. harc[6495]:     2010/08/20_11:30:34 info: Running /etc/ha.d/rc.d/status status
  14. ipfail[6479]: 2010/08/20_11:30:37 info: Status update: Node node2 now has status active
  15. ipfail[6479]: 2010/08/20_11:30:40 info: Asking other side for ping node count.
  16. ipfail[6479]: 2010/08/20_11:30:43 info: No giveup timer to abort.
  17. heartbeat[6466]: 2010/08/20_11:30:43 info: remote resource transition completed.
  18. heartbeat[6466]: 2010/08/20_11:30:43 info: remote resource transition completed.
  19. heartbeat[6466]: 2010/08/20_11:30:43 info: Initial resource acquisition complete (T_RESOURCES(us))
  20. IPaddr[6548]:   2010/08/20_11:30:45 INFO:  Resource is stopped
  21. heartbeat[6512]: 2010/08/20_11:30:46 info: Local Resource acquisition completed.
  22. harc[6599]:     2010/08/20_11:30:46 info: Running /etc/ha.d/rc.d/ip-request-resp ip-request-resp
  23. ip-request-resp[6599]:  2010/08/20_11:30:46 received ip-request-resp IPaddr::192.168.1.250/24/eth0:1 OK yes
  24. ResourceManager[6620]:  2010/08/20_11:30:46 info: Acquiring resource group: node1 IPaddr::192.168.1.250/24/eth0:1

  25. mysqld_umount mysqld
  26. IPaddr[6647]:   2010/08/20_11:30:48 INFO:  Resource is stopped
  27. ResourceManager[6620]:  2010/08/20_11:30:48 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.250/24/eth0:1 start
  28. IPaddr[6745]:   2010/08/20_11:30:50 INFO: Using calculated netmask for 192.168.1.250: 255.255.255.0
  29. IPaddr[6745]:   2010/08/20_11:30:50 INFO: eval ifconfig eth0:0 192.168.1.250 netmask 255.255.255.0 broadcast 192.168.1.255
  30. IPaddr[6716]:   2010/08/20_11:30:51 INFO:  Success
  31. ResourceManager[6620]:  2010/08/20_11:30:52 info: Running /etc/ha.d/resource.d/mysqld_umount  start
  32. ResourceManager[6620]:  2010/08/20_11:30:53 info: Running /etc/init.d/mysqld  start
复制代码
node2启动服务并看日志
  1. [root@node2 ~]# service heartbeat start
  2. Starting High-Availability services:
  3. 2010/08/20_11:30:30 INFO:  Resource is stopped
  4.                                                            [  OK  ]
  5. [root@node2 ~]# tail /var/log/ha-log -f
  6. heartbeat[6149]: 2010/08/20_11:30:33 info: Local status now set to: 'active'
  7. heartbeat[6149]: 2010/08/20_11:30:33 info: Starting child client "/usr/lib/heartbeat/ipfail" (498,496)
  8. heartbeat[6149]: 2010/08/20_11:30:33 info: Status update for node node1: status active
  9. heartbeat[6170]: 2010/08/20_11:30:33 info: Starting "/usr/lib/heartbeat/ipfail" as uid 498  gid 496 (pid 6170)
  10. harc[6178]:     2010/08/20_11:30:34 info: Running /etc/ha.d/rc.d/status status
  11. ipfail[6170]: 2010/08/20_11:30:42 info: Ping node count is balanced.
  12. heartbeat[6149]: 2010/08/20_11:30:43 info: local resource transition completed.
  13. heartbeat[6149]: 2010/08/20_11:30:43 info: Initial resource acquisition complete (T_RESOURCES(us))
  14. heartbeat[6194]: 2010/08/20_11:30:43 info: No local resources [/usr/share/heartbeat/ResourceManager listkeys node2] to acquire.
  15. heartbeat[6149]: 2010/08/20_11:30:43 info: remote resource transition completed.
复制代码
分别看两机的端口,看下MYSQL是否随heartbeat启动
  1. [root@node1 ~]# netstat -ant
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address               Foreign Address             State
  4. tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN
  5. tcp        0      0 192.168.1.252:7789          192.168.1.251:41577         ESTABLISHED
  6. tcp        0      0 192.168.1.252:48782         192.168.1.251:7789          ESTABLISHED

  7. [root@node2 ~]# netstat -ant
  8. Active Internet connections (servers and established)
  9. Proto Recv-Q Send-Q Local Address               Foreign Address             State
  10. tcp        0      0 192.168.1.251:7789          192.168.1.252:48782         ESTABLISHED
  11. tcp        0      0 192.168.1.251:41577         192.168.1.252:7789          ESTABLISHED
  12. tcp        0      0 :::22                       :::*                        LISTEN
复制代码
建表并加记录,验证是否数据同步
  1. [root@node1 ~]# mysql
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.
  3. Your MySQL connection id is 2
  4. Server version: 5.0.77 Source distribution

  5. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  6. mysql> create database db;
  7. Query OK, 1 row affected (0.00 sec)

  8. mysql> use db;
  9. Database changed
  10. mysql> create table t (id int(10),name char(10));
  11. Query OK, 0 rows affected (0.09 sec)

  12. mysql> insert into t values(001,"ganxing"),(002,"boobooke"),(003,"bbk"),(004,"abc");
  13. Query OK, 4 row affected (0.00 sec)
  14. Records: 4  Duplicates: 0  Warnings: 0
  15. mysql> select * from t;
  16. +------+----------+
  17. | id   | name     |
  18. +------+----------+
  19. |    1 | ganxing  |
  20. |    2 | boobooke |
  21. |    3 | bbk      |
  22. |    4 | abc      |
  23. +------+----------+
  24. 4 rows in set (0.01 sec)
复制代码
停止node1上的heartbeat服务,看node2是否能接管,并同步数据
  1. [root@node1 ~]# service heartbeat stop
  2. [root@node1 ~]# tail /var/log/ha-log -f
  3. heartbeat[6466]: 2010/08/20_11:36:08 info: killing HBWRITE process 6470 with signal 15
  4. heartbeat[6466]: 2010/08/20_11:36:08 info: killing HBREAD process 6471 with signal 15
  5. heartbeat[6466]: 2010/08/20_11:36:08 info: killing HBWRITE process 6472 with signal 15
  6. heartbeat[6466]: 2010/08/20_11:36:08 info: killing HBREAD process 6473 with signal 15
  7. heartbeat[6466]: 2010/08/20_11:36:08 info: Core process 6471 exited. 5 remaining
  8. heartbeat[6466]: 2010/08/20_11:36:08 info: Core process 6470 exited. 4 remaining
  9. heartbeat[6466]: 2010/08/20_11:36:09 info: Core process 6469 exited. 3 remaining
  10. heartbeat[6466]: 2010/08/20_11:36:09 info: Core process 6473 exited. 2 remaining
  11. heartbeat[6466]: 2010/08/20_11:36:09 info: Core process 6472 exited. 1 remaining
  12. heartbeat[6466]: 2010/08/20_11:36:09 info: node1 Heartbeat shutdown complete.
复制代码
查看node2接管日志
  1. [root@node2 ~]# tail /var/log/ha-log -f
  2. heartbeat[6149]: 2010/08/20_11:36:06 info: Received shutdown notice from 'node1'.
  3. heartbeat[6149]: 2010/08/20_11:36:06 info: Resources being acquired from node1.
  4. heartbeat[6208]: 2010/08/20_11:36:06 info: acquire local HA resources (standby).
  5. heartbeat[6208]: 2010/08/20_11:36:07 info: local HA resource acquisition completed (standby).
  6. heartbeat[6149]: 2010/08/20_11:36:07 info: Standby resource acquisition done [all].
  7. heartbeat[6209]: 2010/08/20_11:36:07 info: No local resources [/usr/share/heartbeat/ResourceManager listkeys node2] to acquire.
  8. harc[6234]:     2010/08/20_11:36:08 info: Running /etc/ha.d/rc.d/status status
  9. mach_down[6250]:        2010/08/20_11:36:08 info: Taking over resource group IPaddr::192.168.1.250/24/eth0:1
  10. ResourceManager[6276]:  2010/08/20_11:36:09 info: Acquiring resource group: node1 IPaddr::192.168.1.250/24/eth0:1 mysqld_umount mysqld
  11. IPaddr[6303]:   2010/08/20_11:36:10 INFO:  Resource is stopped
  12. ResourceManager[6276]:  2010/08/20_11:36:11 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.250/24/eth0:1 start
  13. IPaddr[6401]:   2010/08/20_11:36:12 INFO: Using calculated netmask for 192.168.1.250: 255.255.255.0
  14. IPaddr[6401]:   2010/08/20_11:36:13 INFO: eval ifconfig eth0:0 192.168.1.250 netmask 255.255.255.0 broadcast 192.168.1.255
  15. IPaddr[6372]:   2010/08/20_11:36:13 INFO:  Success
  16. ResourceManager[6276]:  2010/08/20_11:36:14 info: Running /etc/ha.d/resource.d/mysqld_umount  start
  17. ResourceManager[6276]:  2010/08/20_11:36:15 info: Running /etc/init.d/mysqld  start
  18. heartbeat[6149]: 2010/08/20_11:36:18 WARN: node node1: is dead
  19. heartbeat[6149]: 2010/08/20_11:36:18 info: Dead node node1 gave up resources.
  20. heartbeat[6149]: 2010/08/20_11:36:18 info: Link node1:eth0 dead.
  21. ipfail[6170]: 2010/08/20_11:36:18 info: Status update: Node node1 now has status dead
  22. mach_down[6250]:        2010/08/20_11:36:19 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired
  23. heartbeat[6149]: 2010/08/20_11:36:19 info: mach_down takeover complete.
  24. mach_down[6250]:        2010/08/20_11:36:19 info: mach_down takeover complete for node node1.
  25. ipfail[6170]: 2010/08/20_11:36:19 info: NS: We are still alive!
  26. ipfail[6170]: 2010/08/20_11:36:19 info: Link Status update: Link node1/eth0 now has status dead
  27. ipfail[6170]: 2010/08/20_11:36:20 info: Asking other side for ping node count.
  28. ipfail[6170]: 2010/08/20_11:36:20 info: Checking remote count of ping nodes.
复制代码
来验证一下数据同步情况
  1. [root@node2 ~]# mysql
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.
  3. Your MySQL connection id is 2
  4. Server version: 5.0.77 Source distribution

  5. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

  6. mysql> use db
  7. Reading table information for completion of table and column names
  8. You can turn off this feature to get a quicker startup with -A

  9. Database changed
  10. mysql> select * from t;
  11. +------+----------+
  12. | id   | name     |
  13. +------+----------+
  14. |    1 | ganxing  |
  15. |    2 | boobooke |
  16. |    3 | bbk      |
  17. |    4 | abc      |
  18. +------+----------+
  19. 4 rows in set (0.00 sec)
复制代码
服务启动的顺序
drbd让系统自动加载
heartbeat 放在/etc/rc.local里面MYSQLD不要开机自动加载
http://51CTO提醒您,请勿滥发广告!/bbs/thread-47791-1-1.html

你可能感兴趣的:(数据库,mysql,heartbeat,drbd,休闲)