要做mysql双机热备drbd是个很好的选择,成本低维护简单
drbd下载地址:
1、安装
[root@CNC-JN-P-57R yum.repos.d]# uname -a
Linux CNC-JN-P-57R 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux
[root@CNC-JN-P-57R /]#tar zxvf drbd-8.3.5.tar.gz
[root@CNC-JN-P-57R drbd-8.3.5]# make
[root@CNC-JN-P-57R drbd-8.3.5]# make install
[root@CNC-JN-P-57R /]# chkconfig --add drbd
[root@CNC-JN-P-57R /]# chkconfig --level 35 drbd on
[root@CNC-JN-P-57R /]#
2、加载DRBD模块
[root@CNC-JN-P-57R /]# modprobe drbd
[root@CNC-JN-P-57R /]# lsmod |grep drbd
drbd 236732 3
[root@CNC-JN-P-57R /]#
如果有,则表示成功了!!!
3、配置/etc/drbd.conf
#让linbit公司收集目前drbd的使用情况,yes为参加
global { usage-count yes; }
#drbd所管理的多个资源之间的common,主要是配置drbd的所有resource可以设置为相同的参数项,比如protocol,syncer等等.
common { syncer { rate 100M; } }
#创建一个资源,名字叫"r0"
resource r0 {
#使用协议C.表示收到远程主机的写入确认后,则认为写入完成。
protocol C;
startup {
wfc-timeout 0;
degr-wfc-timeout 120;
}
#由于实验环境下两台服务器硬盘大小可能不同,所以需要设置drbd的大小。
disk {
on-io-error detach;
}
net {
#设置网络传输加密算法
cram-hmac-alg sha1;
#认证字串
shared-secret "FooFunFactory";
}
on CNC-JN-P-57R {
#设定资源设备/dev/drbd1 指向实际的物理分区 /dev/sda7
device /dev/drbd1;
disk /dev/sda7;
#设定监听地址以及端口
address 192.168.241.101:7898;
#设定元数据保存方式:可以用 internal(即保存在同一个物理分区下)
#也可以保存在其他分区上
meta-disk internal;
}
on CNC-JN-P-57S {
device /dev/drbd1;
disk /dev/sda7;
address 192.168.241.100:7898;
meta-disk internal;
}
}
4、建立drbd设备,启动drbd服务
建立md设备
[root@CNC-JN-P-57R /]# drbdadm create-md r0
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
success
[root@CNC-JN-P-57R /]#
启动drbd服务程序
[root@CNC-JN-P-57R /]# /etc/init.d/drbd start
Starting DRBD resources: [ d(r0) s(r0) n(r0) ].
[root@CNC-JN-P-57R /]#
确认drbd状态
[root@CNC-JN-P-57R /]# cat /proc/drbd
version: 8.3.5 (api:88/proto:86-91)
GIT-hash: ded8cdf09b0efa1460e8ce7a72327c60ff2210fb build by root@CNC-JN-P-57R, 2010-08-03 11:32:51
1: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----
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:326342412
[root@CNC-JN-P-57R /]#
5、初始化Primary机
[root@CNC-JN-P-57R /]# drbdsetup /dev/drbd1 primary -o
再次确认host1的drbd状态
[root@CNC-JN-P-57R /]# cat /proc/drbd
version: 8.3.5 (api:88/proto:86-91)
GIT-hash: ded8cdf09b0efa1460e8ce7a72327c60ff2210fb build by root@CNC-JN-P-57R, 2010-08-03 11:32:51
1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----
ns:196064 nr:0 dw:0 dr:196064 al:0 bm:11 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:326146348
[>....................] sync'ed: 0.1% (318500/318692)M
finish: 7:24:30 speed: 12,200 (11,532) K/sec
[root@CNC-JN-P-57R /]#
[root@CNC-JN-P-57R /]# cat /proc/drbd
version: 8.3.5 (api:88/proto:86-91)
GIT-hash: ded8cdf09b0efa1460e8ce7a72327c60ff2210fb build by root@CNC-JN-P-57R, 2010-08-03 11:32:51
1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----
ns:874400 nr:0 dw:0 dr:874400 al:0 bm:53 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:325468012
[>....................] sync'ed: 0.3% (317836/318692)M
finish: 7:54:54 speed: 11,416 (11,068) K/sec
[root@CNC-JN-P-57R /]#
[root@CNC-JN-P-57R /]# cat /proc/drbd
version: 8.3.5 (api:88/proto:86-91)
GIT-hash: ded8cdf09b0efa1460e8ce7a72327c60ff2210fb build by root@CNC-JN-P-57R, 2010-08-03 11:32:51
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
ns:326342412 nr:0 dw:0 dr:326342412 al:0 bm:19919 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
[root@CNC-JN-P-57R /]#
6、验证磁盘镜像结果
主服务器设置
[root@CNC-JN-P-57R /]# mkfs -t ext3 /dev/sda7
[root@CNC-JN-P-57R /]# mkdir /data
[root@CNC-JN-P-57R /]# mount /dev/drbd1 /data
[root@CNC-JN-P-57R data]# df -h >testfile
[root@CNC-JN-P-57R data]# cat testfile
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 49G 12G 35G 25% /
/dev/sda1 99M 13M 82M 14% /boot
none 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 49G 85M 46G 1% /home
/dev/sda3 49G 549M 46G 2% /var
/dev/drbd1 307G 101M 291G 1% /data
[root@CNC-JN-P-57R data]#
备服务器验证
[root@CNC-JN-P-57S ~]# service drbd stop
Stopping all DRBD resources: .
[root@CNC-JN-P-57S ~]# ps -ef|grep drbd
root 24415 24264 0 09:37 pts/0 00:00:00 grep drbd
[root@CNC-JN-P-57S ~]# mount /dev/sda7 /data
[root@CNC-JN-P-57S ~]# cd /data
[root@CNC-JN-P-57S data]# ll
total 20
drwx------ 2 root root 16384 Aug 4 09:33 lost+found
-rw-r--r-- 1 root root 353 Aug 4 09:40 testfile
[root@CNC-JN-P-57S data]# more testfile
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 49G 12G 35G 25% /
/dev/sda1 99M 13M 82M 14% /boot
none 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 49G 85M 46G 1% /home
/dev/sda3 49G 549M 46G 2% /var
/dev/drbd1 307G 101M 291G 1% /data
[root@CNC-JN-P-57S data]#
7、遇到问题解决
[root@CNC-JN-P-57R drbd-8.3.6]# drbdadm create-md r0
--== Thank you for participating in the global usage survey ==--
The server's response is:
you are the 1983th user to install this version
md_offset 334184861696
al_offset 334184828928
bm_offset 334174629888
Found ext3 filesystem
326352408 kB data area apparently used
326342412 kB left usable by current configuration
Device size would be truncated, which
would corrupt data and result in
'access beyond end of device' errors.
You need to either
* use external meta data (recommended)
* shrink that filesystem first
* zero out the device (destroy the filesystem)
Operation refused.
Command 'drbdmeta 1 v08 /dev/sda7 internal create-md' terminated with exit code 40
解决方法:
[root@CNC-JN-P-57R /]# dd if=/dev/zero of=/dev/sda7 bs=1024K
318704+0 records in
318703+0 records out
参考:http://blog.yahunet.com/post-86.html