1、環境介紹
OS:centos 6.5
kernel:2.6.32-431.11.2.el6.x86_64
node1:nginxha001 10.0.15.31 Master
node2:nginxha002 10.0.15.32 Slave
vip:10.0.15.30
說明:本例主要以drbd為主,涉及到mysql,corosync,pacemaker程序的安裝請參考其他資料。程序兩台都要安裝,定義drbd資源可在一台上配置完畢復制到另外一台,創建磁盤需兩台都創建,定義corosync資源只需在一台上配置即可,他會自動同步到另外一台。若程序連接mysql用虛擬ip即vip連接,最好是把程序也部署在drbd磁盤上,這樣程序配置為localhost即可。
2、安裝依賴包
yum -y install gcc rpm-build kernel-devel kernel-headers flex ncurses-devel
wget
http://elrepo.org/linux/elrepo/el5/x86_64/RPMS/drbd84-utils-8.4.3-1.el5.elrepo.x86_64.rpm
wget
http://elrepo.org/linux/elrepo/el6/x86_64/RPMS/kmod-drbd84-8.4.3-1.el6_4.elrepo.x86_64.rpm
yum -y install drbd84-utils-8.4.3-1.el5.elrepo.x86_64.rpm
yum install -y kmod-drbd84-8.4.3-1.el6_4.elrepo.x86_64.rpm
3、定義drbd資源
vi /etc/drbd.d/mysql.res #定義mysql資源,效果一樣
resource mysql {
protocol C;
device /dev/drbd0;
disk /dev/sdc1;
meta-disk internal;
on nginxha001 {
address 10.0.15.31:7789;
}
on nginxha002 {
address 10.0.15.32:7789;
}
}
4、啟動服務,並格式化磁盤
創建drbd數據同步所需的分區,兩台電腦的空間要一樣大
fdisk /dev/sdc
drbdadm create-md mysql
service drbd start
設置為主幾點,僅在主節點上操作
drbdadm -- --overwrite-data-of-peer primary mysql
創建文件系統
mkfs -t ext4 /dev/drbd0
5、配置corosync資源
crm(live)# configure
property stonith-enabled=false
property no-quorum-policy=ignore
定義Mysql服務資源
primitive Mysqld lsb:mysqld
配置DRBD主從資源
primitive Drbd ocf:linbit:drbd params drbd_resource="mysql" op start interval="0" timeout="240" op stop interval="0" timeout="100"
master My_Drbd Drbd meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
定義一個文件系統資源
primitive FileSys ocf:heartbeat:Filesystem params device="/dev/drbd0" directory="/mysqldata" fstype="ext4" op start timeout="60s" op stop timeout="60s"
定將資源之間的位置和啟動順序約束
#讓文件系統與DRBD主節點運行在一起
colocation FileSys_on_My_Drbd inf: FileSys My_Drbd
#讓DRBD服務比文件系統先啟動
order FileSys_after_My_Drbd inf: My_Drbd:promote FileSys:start
#讓Mysql服務與文件系統運行在一起
colocation Mysqld_on_FileSys inf: Mysqld FileSys
#讓文件系統比Mysql服務先運行
order Mysqld_after_FileSys inf: FileSys Mysqld:start
#讓虛擬IP與Mysql服務運行在一起
colocation vip_on_Mysqld inf: vip Mysqld
執行完畢後,最後的配置文件如下:
node nginxha001 \
attributes standby="off"
node nginxha002 \
attributes standby="off"
primitive Drbd ocf:linbit:drbd \
params drbd_resource="mysql" \
op monitor interval="10s" role="Master" \
op monitor interval="20s" role="Slave" \
op start timeout="240s" interval="0" \
op stop timeout="100" interval="0"
primitive FileSys ocf:heartbeat:Filesystem \
params device="/dev/drbd0" directory="/mysqldata" fstype="ext4" \
op start timeout="60s" interval="0" \
op stop timeout="60s" interval="0"
primitive Mysqld lsb:mysqld
primitive vip ocf:heartbeat:IPaddr \
params ip="10.1.15.30" nic="eth0" cidr_netmask="24" \
op monitor interval="10s"
ms My_Drbd Drbd \
meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
colocation FileSys_on_My_Drbd inf: FileSys My_Drbd:Master
colocation Mysqld_on_FileSys inf: Mysqld FileSys
colocation vip_on_Mysqld inf: vip Mysqld
order FileSys_after_My_Drbd inf: My_Drbd:promote FileSys:start
order Mysqld_after_FileSys inf: FileSys Mysqld:start
property $id="cib-bootstrap-options" \
dc-version="1.1.10-14.el6_5.2-368c726" \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes="4" \
stonith-enabled="false" \
no-quorum-policy="ignore" \
last-lrm-refresh="1396576217"
rsc_defaults $id="rsc-options" \
resource-stickiness="100"
#vim:set syntax=pcmk
若有有錯誤,執行如下命令清除錯誤即可
crm resource cleanup Mysqld
crm resource cleanup vip
crm resource cleanup FileSys
6、其他命令
掛載drbdo
mount /dev/drbd0 /mysqldata/
卸載
umount /mysqldata/
設置drbd為主,只有設置為主的才能掛載磁盤
drbdadm primary mysql
設置drbd為備用
drbdadm secondary mysql
watch -n1 'drbd-overview' 觀察數據同步情況,ctrl+c退出。
drbd-overview #查看狀態,當為下面內容時,進行下一步操作
cat /proc/drbd 查看同步進度
7、修改my.cnf配置,主要更改數據目錄為drbd的目錄
vi /etc/my.cnf
[mysqld]
datadir=/mysqldata
socket=/mysqldata/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/mysqldata/mysqld.log
pid-file=/mysqldata/mysqld.pid
[client]
socket=/mysqldata/mysql.sock