一. 环境描述
OS环境:CentOS 5.5 x86_64 (development环境)
DRBD版本:
drbd83-8.3.13-2.el5.centos.x86_64
kmod-drbd83-8.3.13-1.el5.centos.x86_64
MySQL版本:MySQL 5.5.28
Keepalived版本:keepalived-1.1.19-1.i386.rpm
drbd3:192.168.0.96
drbd4:192.168.0.97
drbd_vip:192.168.0.100
二. DRBD部署
a) 定义主机名
# vim /etc/hosts
192.168.0.96 drbd3
192.168.0.97 drbd4
b) 创建分区
在drbd3和drbd4上分别创建两个10G大小空间的分区,并不对分区进行文件系统格式化
c) yum部署DRBD
yum -y install drbd83* kmod-drbd83
d) 配置DRBD
# vim /etc/drbd.conf
global{
usage-count no; #这个问你让不让官网统计
}
common {
syncer {rate 100M;} #传输速度
}
resource r0 {
protocol C; #传输协议(下面进行协议说明)
startup {
}
disk {
on-io-error detach;
}
net { #处理脑裂方法(下面进行说明)
cram-hmac-alg "sha1"; #两台服务器通信间的算法
shared-secret "FooFunFactory";
# after-sb-0pri disconnect;
# after-sb-1pri disconnect;
# after-sb-2pri disconnect;
after-sb-0pri discard-younger-primary;
after-sb-1pri discard-secondary;
after-sb-2pri call-pri-lost-after-sb;
rr-conflict disconnect;
}
syncer {
rate 100M;
al-extents 257;
}
on drbd3{ #这里一定要是hosts定义的名称
device /dev/drbd0; #逻辑设备路径
disk /dev/hda3; #真实设备路径
address 192.168.0.96:7789;
meta-disk internal;
}
on drbd4{
device /dev/drbd0;
disk /dev/hda3;
address 192.168.0.97:7789;
meta-disk internal;
}
}
在drbd3 上执行:
drbdadm create-md r0 (创建drbd记录信息的数据块,正常会有下面提示)
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
在drbd4 上执行:drbdadm create-md r0
#/etc/init.d/drbd start 启动服务,2台机子都启动,这个启动有时候要你填YES
在drbd3上执行:drbdadm -- --overwrite-data-of-peer primary r0 设为主节点
mkfs.ext3 /dev/drbd0
mount /dev/drbd0 /data
测试:
在drbd3上:
cd /data/
touch test
umount /dev/drbd0
drbdadm secondary r0
在drbd4上执行:
drbdadm primary r0
mount /dev/drbd0 /data/
DRBD 协议说明:
A协议:数据一旦写入磁盘并发送到网络中就认为完成了写入操作
B协议:收到接收确认就认为完成了写入操作
C协议:收到写入确认就认为完成了写入操作
DRBD net处理脑裂说明:
DRBD设备的三个进程:
每个drbd设备都有三个进程:
1) drbd0_worker是drbd0的主进程
2) drbd0_asender是primary上的drbd0的数据发送进程
3) drbd0_receiver是secondary上的drdb0的数据写入进程
DRBD几点注意的地方:
1) mount drbd设备以前必须把设备切换到primary状态
2) 两个节点中,同一时间只能有一台处于primary状态,另一台是secondary状态
3) 处于secondary状态的节点不可以挂载
4) 主备最好使用大小一样的分区。
DRBD使用时建议使用Innodb存储引擎,同时打开binlog日志,设置innodb_flush_log_at_commit=1。MyISAM存储引擎在DRBD上容易造成数据丢失,及文件损坏。
DRBD脑裂后处理:
在drdb4上
drbdadm secondary r0
drbdadm — –discard-my-data connect r0
drbdadm connect r0
在drbd3上
drbdam disconnect r0
drbdadm connect r0
三. MySQL部署
添加mysql用户:
useradd mysql –s /sbin/nologin
安装cmake工具:
wget http://www.cmake.org/files/v2.8/cmake-2.8.7.tar.gz
tar zxfv cmake-2.8.7.tar.gz
cd cmake-2.8.7
./configure && make && make install
安装MySQL
tar zxfv mysql-5.5.28.tar.gz
cd ../mysql-5.5.28
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/3306 -DWITH_INNOBASE_STORAGE_ENGINE=on -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/data/3306/my
sqld.sock -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DSYSCONFDIR=/etc/mysql/3306 && make && make install
cd /usr/local/mysql/
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql --datadir=/data/3306
创建PID并授权
cp support-files/mysql.server /etc/init.d/mysqld
四. Keepalived部署
部署keepalived:
rpm -ivh keepalived-1.1.19-1.i386.rpm
在drbd3上配置文件如下:
-----------------/etc/keepalived/keepalived.conf-----------------
! Configuration File for keepalived
global_defs {
router_id 192.168.0.96
}
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 5
weight -10
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.100
}
track_script {
chk_mysql
}
}
--------------------------------- check_mysql.sh ----------------------------------------
#!/bin/bash
A=`ps -C mysqld --no-header |wc -l`
if [ $A -eq 0 ];then
/bin/umount /data/
drbdadm secondary r0
killall keepalived
fi
------------------------------------------to_master.sh--------------------------------------------
#!/bin/bash
drbdadm primary r0
/bin/mount /dev/drbd0 /data/
/etc/init.d/mysqld start
在drbd4上配置文件如下:
-----------------/etc/keepalived/keepalived.conf-----------------
! Configuration File for keepalived
global_defs {
router_id 192.168.0.97
}
vrrp_sync_group VI{
group {
VI_1
}
notify_master /etc/keepalived/to_master.sh
notify_backup /etc/keepalived/to_backup.sh
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.100
}
}
------------------------------------------to_master.sh--------------------------------------------
#!/bin/bash
drbdadm primary r0
/bin/mount /dev/drbd0 /data/
/etc/init.d/mysqld start
------------------------------------------to_backup.sh-------------------------------------------
#!/bin/bash
/etc/init.d/mysqld stop
/bin/umount /dev/drbd0
drbdadm secondary r0
来源:oschina
链接:https://my.oschina.net/u/2408078/blog/522596