DRBD實現文件存儲高可用mysql
DRDB是一個用軟件實現的、無共享的、服務器之間鏡像塊設備內容的存儲複製解決方案。由內核模塊和相關應用層工具程序組成,主要被用於Linux平臺下的高可用(HA)方案之中,實現不一樣機器上數據的同步,保持數據的一致性。當本地節點的主機出現故障時,遠程節點的主機上還會保留有一份徹底相同的數據,能夠繼續使用,以達到高可用的目的。在高可用(HA)解決方案中使用DRBD,能夠代替使用一個共享盤陣列存儲設備。由於數據同時存在於本地主機和遠程主機上,在遇到須要切換的時候,遠程主機只須要使用它上面的那份備份數據,就能夠繼續提供服務了。算法
Drbd的配置(主從模式)sql
實驗環境:vim
2個節點:bash
192.168.10.132 fjw132服務器
192.168.10.135 fjw135網絡
一、修改兩臺節點的hosts文件,使其能夠互相訪問dom
[root@fjw132 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.132 fjw132 192.168.10.135 fjw135
二、使用ssh讓兩臺機能夠互相鏈接ssh
root@fjw132 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 03:9a:6b:18:10:22:06:fb:1e:81:e4:d0:ce:da:11:05 root@fjw132 The key's randomart p_w_picpath is: +--[ RSA 2048]----+ |B+Eo. | |B+o | |++.. . | | o+. o . | | o+.o S | |...+ . . | | o o | | . | | | +-----------------+ [root@fjw132 ~]# ssh-copy-id 192.168.10.135
三、設置時鐘同步:ide
[root@fjw132 ~]# crontab -l */5 * * * * ntpdate cn.pool.ntp.org
四、安裝drbd
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm yum install -y kmod-drbd84 drbd84-utils
五、修改drbd配置文件
vim /etc/drbd.d/global_common.conf global { usage-count no; #是否參加DRBD使用統計,默認爲yes。 } common { protocol C; #使用DRBD的同步協議 handlers { pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f"; pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f"; local-io-error "/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f"; } disk { on-io-error detach; #配置I/O錯誤處理策略爲分離 } syncer { rate 1024M; #設置主備節點同步時的網絡速率 } }
六、建立配置文件
[root@fjw132 ~]# cat /etc/drbd.d/mysql.res resource mysql { #資源名稱 protocol C; #使用協議 meta-disk internal; device /dev/drbd1; #DRBD設備名稱 syncer { verify-alg sha1; # 加密算法 } net { allow-two-primaries; } on fjw132 { disk /dev/sdb1; address 192.168.10.132:7789; #設置DRBD監聽地址與端口 } on fjw135 { disk /dev/sdb1; address 192.168.10.135:7789; } }
七、裝一個硬盤並分好區爲/dev/sdb1
fdisk -l #查看新磁盤 fdisk /dev/sdb 添加分區
以上步驟兩臺節點都作相同操做
八、在fjw132上啓動
[root@fjw135 ~]# drbdadm create-md mysql initializing activity log initializing bitmap (160 KB) to all zero Writing meta data... New drbd meta data block successfully created. [root@fjw135 ~]# modprobe drbd [root@fjw135 ~]# drbdadm up mysql [root@fjw135 ~]# drbdadm -- --force primary mysql
九、在對端節點192.168.10.135執行
drbdadm create-md mysql modprobe drbd drbdadm up mysql
十、在fjw132上格式化和掛載到建立的/data目錄上
[root@fjw132 ~]# mkfs.ext4 /dev/drbd1 [root@fjw132 ~]# mount /dev/drbd1 /data [root@fjw132 ~]# drbd-overview NOTE: drbd-overview will be deprecated soon. Please consider using drbdtop. 1:mysql/0 Connected Primary/Secondary UpToDate/UpToDate [root@fjw135 ~]# drbd-overview NOTE: drbd-overview will be deprecated soon. Please consider using drbdtop. 1:mysql/0 Connected Secondary/Secondary UpToDate/UpToDate
十一、測試,在/data目錄上建立一個文件1.txt
[root@fjw132 ~]# echo "123" > /data/1.txt
十二、先把主結點降爲從結點(先卸載才能變爲從)
[root@fjw132 ~]# umount /data [root@fjw132 ~]# drbdadm secondary mysql [root@fjw132 ~]# drbd-overview NOTE: drbd-overview will be deprecated soon. Please consider using drbdtop. 1:mysql/0 Connected Secondary/Secondary UpToDate/UpToDate
1三、到fjw135上升級
[root@fjw135 ~]# drbdadm primary mysql [root@fjw135 ~]# drbd-overview NOTE: drbd-overview will be deprecated soon. Please consider using drbdtop. 1:mysql/0 Connected Primary/Secondary UpToDate/UpToDate
1四、掛載並查看
[root@fjw135 ~]# mount /dev/drbd1 /data [root@fjw135 ~]# ls /data 1.txt lost+found [root@fjw135 ~]# cat /data/1.txt 123
完!