一. 環境描述mysql
OS環境:CentOS 5.5 x86_64 (development環境)算法
DRBD版本:sql
drbd83-8.3.13-2.el5.centos.x86_64vim
kmod-drbd83-8.3.13-1.el5.centos.x86_64centos
MySQL版本:MySQL 5.5.28bash
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