tar xf drbd-8.4.2.tar.gz
cd drbd-8.4.2
./configure --prefix=/usr/local/drbd --with-km
make KDIR=/usr/src/kernels/2.6.32-431.11.2.el6.x86_64/ #很多童鞋无法加载drbd模块,多半是正在运行的内核版本和新安装的不相符
make install
mkdir -p /usr/local/drbd/var/run/drbd
cp /usr/local/drbd/etc/rc.d/init.d/drbd /etc/rc.d/init.d
chmod 755 /etc/init.d/drbd
cd drbd
make clean
make KDIR=/usr/src/kernels/2.6.32-431.11.2.el6.x86_64/
cp drbd.ko /lib/modules/`uname -r`/kernel/lib/
modprobe drbd
[root@192.168.0.10 ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G 2.6G 16G 15% /
tmpfs tmpfs 121M 0 121M 0% /dev/shm
/dev/sda1 ext4 204M 52M 141M 27% /boot
/dev/sda5 ext4 34G 185M 32G 1% /data
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# umount /data/
[root@192.168.0.10 ~]# mkfs.ext4 /dev/sda5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2048000 inodes, 8185344 blocks
409267 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
250 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000eb0ff
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 2321 18432000 83 Linux
/dev/sda3 2321 2451 1048576 82 Linux swap / Solaris
/dev/sda4 2451 6528 32742400 5 Extended
/dev/sda5 2451 6528 32741376 83 Linux
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# cat /usr/local/drbd/etc/drbd.d/global_common.conf
global { usage-count yes; }
common { syncer { rate 30M; } } #同步速率,视带宽而定
resource r0 { #创建一个资源,名字叫"r0"
protocol C; #选择的是drbd的C 协议(数据同步协议,C为收到数据并写入后返回,确认成功)
startup {
}
disk {
on-io-error detach;
}
net {
}
on db-server-01 { #设定一个节点,分别以各自的主机名命名
device /dev/drbd0; #设定资源设备/dev/drbd0 指向实际的物理分区 /dev/sda5
disk /dev/sda5;
address 192.168.0.10:7888; #设定监听地址以及端口
meta-disk internal;
}
on db-server-02 {
device /dev/drbd0;
disk /dev/sda5;
address 192.168.0.20:7888;
meta-disk internal; #internal表示是在同一个局域网内
}
}
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# drbdadm create-md r0
Writing meta data...
initializing activity log
NOT initializing bitmap
New drbd meta data block successfully created.
success
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# /etc/init.d/drbd start
Starting DRBD resources: [
create res: r0
prepare disk: r0
adjust disk: r0
adjust net: r0
]
.....
[root@192.168.0.10 ~]#
[root@192.168.0.20 ~]# /etc/init.d/drbd start
Starting DRBD resources: [
create res: r0
prepare disk: r0
adjust disk: r0
adjust net: r0
]
.
[root@192.168.0.20 ~]#
[root@192.168.0.10 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-01, 2014-04-18 21:15:57
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Secondary Inconsistent/Inconsistent C
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-01, 2014-04-18 21:15:57
m:res cs ro ds p mounted fstype
... sync'ed: 13.7% (27596/31972)M
0:r0 SyncSource Primary/Secondary UpToDate/Inconsistent C /data ext4
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# tar xf mysql-5.5.37-linux2.6-x86_64.tar.gz -C /usr/local/
[root@192.168.0.10 ~]# cd /usr/local/
[root@192.168.0.10 local]# ln -s mysql-5.5.37-linux2.6-x86_64/ mysql
[root@192.168.0.10 local]# groupadd mysql
[root@192.168.0.10 local]# useradd -r -g mysql mysql
[root@192.168.0.10 local]# cd mysql
[root@192.168.0.10 mysql]# chown -R mysql .
[root@192.168.0.10 mysql]# chgrp -R mysql .
[root@192.168.0.10 mysql]# mkdir /data/mysql
[root@192.168.0.10 mysql]# chown -R mysql.mysql /data/mysql/
[root@192.168.0.10 mysql]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/ --basedir=/usr/local/mysql
[root@192.168.0.10 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-01, 2014-04-18 21:15:57
m:res cs ro ds p mounted fstype
0:r0 Connected Primary/Secondary UpToDate/UpToDate C /data ext4
[root@192.168.0.10 ~]#
[root@192.168.0.20 ~]# drbdadm primary all
[root@192.168.0.20 ~]# mount /dev/drbd0 /data/
[root@192.168.0.20 ~]# ll /data/
total 20
drwx------ 2 root root 16384 Apr 18 22:16 lost+found
drwxr-xr-x 5 mysql mysql 4096 Apr 18 23:01 mysql
[root@192.168.0.20 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-02, 2014-04-18 21:22:55
m:res cs ro ds p mounted fstype
0:r0 Connected Primary/Secondary UpToDate/UpToDate C /data ext4
[root@192.168.0.20 ~]#
[root@192.168.0.20 ~]# chmod 755 /etc/ha.d/resource.d/drbddisk
[root@192.168.0.20 ~]# cat /etc/ha.d/resource.d/drbddisk
#!/bin/bash
#
# This script is inteded to be used as resource script by heartbeat
#
# Copright 2003-2008 LINBIT Information Technologies
# Philipp Reisner, Lars Ellenberg
#
###
DEFAULTFILE="/etc/default/drbd"
DRBDADM="/sbin/drbdadm"
if [ -f $DEFAULTFILE ]; then
. $DEFAULTFILE
fi
if [ "$#" -eq 2 ]; then
RES="$1"
CMD="$2"
else
RES="all"
CMD="$1"
fi
## EXIT CODES
# since this is a "legacy heartbeat R1 resource agent" script,
# exit codes actually do not matter that much as long as we conform to
# http://wiki.linux-ha.org/HeartbeatResourceAgent
# but it does not hurt to conform to lsb init-script exit codes,
# where we can.
# http://refspecs.linux-foundation.org/LSB_3.1.0/
#LSB-Core-generic/LSB-Core-generic/iniscrptact.html
####
drbd_set_role_from_proc_drbd()
{
local out
if ! test -e /proc/drbd; then
ROLE="Unconfigured"
return
fi
dev=$( $DRBDADM sh-dev $RES )
minor=${dev#/dev/drbd}
if [[ $minor = *[!0-9]* ]] ; then
# sh-minor is only supported since drbd 8.3.1
minor=$( $DRBDADM sh-minor $RES )
fi
if [[ -z $minor ]] || [[ $minor = *[!0-9]* ]] ; then
ROLE=Unknown
return
fi
if out=$(sed -ne "/^ *$minor: cs:/ { s/:/ /g; p; q; }" /proc/drbd); then
set -- $out
ROLE=${5%/**}
: ${ROLE:=Unconfigured} # if it does not show up
else
ROLE=Unknown
fi
}
case "$CMD" in
start)
# try several times, in case heartbeat deadtime
# was smaller than drbd ping time
try=6
while true; do
$DRBDADM primary $RES && break
let "--try" || exit 1 # LSB generic error
sleep 1
done
;;
stop)
# heartbeat (haresources mode) will retry failed stop
# for a number of times in addition to this internal retry.
try=3
while true; do
$DRBDADM secondary $RES && break
# We used to lie here, and pretend success for anything != 11,
# to avoid the reboot on failed stop recovery for "simple
# config errors" and such. But that is incorrect.
# Don't lie to your cluster manager.
# And don't do config errors...
let --try || exit 1 # LSB generic error
sleep 1
done
;;
status)
if [ "$RES" = "all" ]; then
echo "A resource name is required for status inquiries."
exit 10
fi
ST=$( $DRBDADM role $RES )
ROLE=${ST%/**}
case $ROLE in
Primary|Secondary|Unconfigured)
# expected
;;
*)
# unexpected. whatever...
# If we are unsure about the state of a resource, we need to
# report it as possibly running, so heartbeat can, after failed
# stop, do a recovery by reboot.
# drbdsetup may fail for obscure reasons, e.g. if /var/lock/ is
# suddenly readonly. So we retry by parsing /proc/drbd.
drbd_set_role_from_proc_drbd
esac
case $ROLE in
Primary)
echo "running (Primary)"
exit 0 # LSB status "service is OK"
;;
Secondary|Unconfigured)
echo "stopped ($ROLE)"
exit 3 # LSB status "service is not running"
;;
*)
# NOTE the "running" in below message.
# this is a "heartbeat" resource script,
# the exit code is _ignored_.
echo "cannot determine status, may be running ($ROLE)"
exit 4 # LSB status "service status is unknown"
;;
esac
;;
*)
echo "Usage: drbddisk [resource] {start|stop|status}"
exit 1
;;
esac
exit 0
[root@192.168.0.20 ~]#
[root@192.168.0.10 ~]# cat /etc/ha.d/ha.cf
logfile /var/log/ha-log
#定义Heartbeat的日志名字及位置
logfacility local0
keepalive 2
#设定心跳(监测)时间为2秒
deadtime 15
#设定死亡时间为15秒
ucast eth1 192.168.0.20
#采用单播的方式,IP地址指定为对方IP
auto_failback off
#当Primary机器发生故障切换到Secondary机器后Primary恢复后是否进行切回操作 (最好是我们有需求手动进行切换)
node db-server-01
node db-server-02
[root@192.168.0.10 ~]#
[root@192.168.0.20 ~]# cat /etc/ha.d/ha.cf
logfile /var/log/ha-log
#定义Heartbeat的日志名字及位置
logfacility local0
keepalive 2
#设定心跳(监测)时间为2秒
deadtime 15
#设定死亡时间为15秒
ucast eth1 192.168.0.10
#采用单播的方式,IP地址指定为对方IP
auto_failback off
#当Primary机器发生故障切换到Secondary机器后Primary恢复后是否进行切回操作(一般我们可以看需求,否则不用自动切换)
node db-server-01
node db-server-02
[root@192.168.0.20 ~]#
[root@192.168.0.10 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
modprobe drbd #必须先加载模块,这也是因为将启动命令放在这里的原因
/etc/init.d/drbd start
/etc/init.d/heartbeat start
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-01, 2014-04-18 21:15:57
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Primary UpToDate/UpToDate C
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# drbdadm primary all
[root@192.168.0.10 ~]# mount /dev/drbd0 /data/
[root@192.168.0.10 ~]# ll /data/
total 20
drwx------ 2 root root 16384 Apr 18 22:16 lost+found
drwxr-xr-x 5 mysql mysql 4096 Apr 18 23:01 mysql
[root@192.168.0.10 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-01, 2014-04-18 21:15:57
m:res cs ro ds p mounted fstype
0:r0 Connected Primary/Secondary UpToDate/UpToDate C /data ext4
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# /etc/init.d/mysqld start
Starting MySQL....... [ OK ]
[root@192.168.0.10 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@192.168.0.10 ~]# tail -n 20 /var/log/ha-log
harc(default)[5598]: 2014/04/19_00:25:21 info: Running /etc/ha.d//rc.d/status status
Apr 19 00:25:22 db-server-01 heartbeat: [5591]: info: Comm_now_up(): updating status to active
Apr 19 00:25:22 db-server-01 heartbeat: [5591]: info: Local status now set to: 'active'
Apr 19 00:25:22 db-server-01 heartbeat: [5591]: info: Status update for node db-server-02: status active
harc(default)[5618]: 2014/04/19_00:25:22 info: Running /etc/ha.d//rc.d/status status
Apr 19 00:25:33 db-server-01 heartbeat: [5591]: info: remote resource transition completed.
Apr 19 00:25:33 db-server-01 heartbeat: [5591]: info: remote resource transition completed.
Apr 19 00:25:33 db-server-01 heartbeat: [5591]: info: Initial resource acquisition complete (T_RESOURCES(us))
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.0.88)[5671]: 2014/04/19_00:25:33 INFO: Resource is stopped
Apr 19 00:25:33 db-server-01 heartbeat: [5635]: info: Local Resource acquisition completed.
harc(default)[5752]: 2014/04/19_00:25:33 info: Running /etc/ha.d//rc.d/ip-request-resp ip-request-resp
ip-request-resp(default)[5752]: 2014/04/19_00:25:33 received ip-request-resp IPaddr::192.168.0.88/24/eth1 OK yes
ResourceManager(default)[5775]: 2014/04/19_00:25:33 info: Acquiring resource group: db-server-01 IPaddr::192.168.0.88/24/eth1 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4 mysqld
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.0.88)[5803]: 2014/04/19_00:25:33 INFO: Resource is stopped
ResourceManager(default)[5775]: 2014/04/19_00:25:33 info: Running /etc/ha.d/resource.d/IPaddr 192.168.0.88/24/eth1 start
IPaddr(IPaddr_192.168.0.88)[5926]: 2014/04/19_00:25:34 INFO: Adding inet address 192.168.0.88/24 with broadcast address 192.168.0.255 to device eth1
IPaddr(IPaddr_192.168.0.88)[5926]: 2014/04/19_00:25:34 INFO: Bringing device eth1 up
IPaddr(IPaddr_192.168.0.88)[5926]: 2014/04/19_00:25:34 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.0.88 eth1 192.168.0.88 auto not_used not_used
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.0.88)[5900]: 2014/04/19_00:25:34 INFO: Success
/usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/drbd0)[6030]: 2014/04/19_00:25:34 INFO: Running OK
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G 3.5G 15G 20% /
tmpfs tmpfs 121M 0 121M 0% /dev/shm
/dev/sda1 ext4 204M 52M 141M 27% /boot
/dev/drbd0 ext4 33G 216M 32G 1% /data
[root@192.168.0.10 ~]#
[root@192.168.0.10 ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G 3.5G 15G 20% /
tmpfs tmpfs 121M 0 121M 0% /dev/shm
/dev/sda1 ext4 204M 52M 141M 27% /boot
[root@192.168.0.10 ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.4.2 (api:1/proto:86-101)
GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@db-server-01, 2014-04-18 21:15:57
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Primary UpToDate/UpToDate C
[root@192.168.0.10 ~]#
[root@192.168.0.20 ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G 4.9G 13G 28% /
tmpfs tmpfs 121M 0 121M 0% /dev/shm
/dev/sda1 ext4 204M 52M 141M 27% /boot
/dev/drbd0 ext4 33G 216M 32G 1% /data
[root@192.168.0.20 ~]# netstat -nltp | grep 3306 | grep -v grep
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 5542/mysqld
[root@192.168.0.20 ~]#
[root@192.168.0.20 ~]# ip addr | grep eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.0.20/24 brd 192.168.0.255 scope global eth1
inet 192.168.0.88/24 brd 192.168.0.255 scope global secondary eth1
[root@192.168.0.20 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@192.168.0.20 ~]# tail -n 10 /var/log/ha-log
ResourceManager(default)[4768]: 2014/04/19_00:36:42 info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /data ext4 start
Filesystem(Filesystem_/dev/drbd0)[5131]: 2014/04/19_00:36:42 INFO: Running start for /dev/drbd0 on /data
/usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/drbd0)[5122]: 2014/04/19_00:36:42 INFO: Success
ResourceManager(default)[4768]: 2014/04/19_00:36:43 info: Running /etc/init.d/mysqld start
mach_down(default)[4741]: 2014/04/19_00:36:46 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired
mach_down(default)[4741]: 2014/04/19_00:36:46 info: mach_down takeover complete for node db-server-01.
Apr 19 00:36:46 db-server-02 heartbeat: [4637]: info: mach_down takeover complete.
Apr 19 00:36:58 db-server-02 heartbeat: [4637]: WARN: node db-server-01: is dead
Apr 19 00:36:58 db-server-02 heartbeat: [4637]: info: Dead node db-server-01 gave up resources.
Apr 19 00:36:58 db-server-02 heartbeat: [4637]: info: Link db-server-01:eth1 dead.
[root@192.168.0.20 ~]#
[root@192.168.0.20 ~]# cat mysqlmon.sh
#!/bin/bash
trap 'echo PROGRAM INTERRUPTED; exit 1' INT
username=root
password=123456
n=0
log='/var/log/mysqlmon.log'
while true
do
if /usr/local/mysql/bin/mysql -u${username} -p${password} -e "use test" >&/dev/null
then
echo `date +"%Y-%m-%d %H:%M:%S"` mysqld is alive! >> ${log}
n=0
else
echo "`date +"%Y-%m-%d %H:%M:%S"` mysqld cannot be connected!" >> ${log}
n=$[n + 1]
if [ $n -eq 3 ]
then
/etc/init.d/heartbeat stop
echo "`date +"%Y-%m-%d %H:%M:%S"` mysqld switched to backup!" >> ${log}
echo "`date +"%Y-%m-%d %H:%M:%S"` mysqld switched to backup" | mutt -s "mysqld switched to backup" saltstack@163.com
break
fi
fi
sleep 10
done
[root@192.168.0.20 ~]#
[root@192.168.0.20 ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G 4.9G 13G 28% /
tmpfs tmpfs 121M 0 121M 0% /dev/shm
/dev/sda1 ext4 204M 52M 141M 27% /boot
/dev/drbd0 ext4 33G 216M 32G 1% /data
[root@192.168.0.20 ~]# netstat -nltp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 13771/mysqld
[root@192.168.0.20 ~]#
作者:Atlas
出处:Atlas的博客 http://www.cnblogs.com/gomysql
欢迎光临 168大数据 (http://www.bi168.cn/) | Powered by Discuz! X3.2 |