標籤:mysql drbd
DRBD是一個用軟體實現的、無共用的、伺服器之間鏡像塊裝置內容的儲存複製解決方案。DRBD的功能是通過linux核心實現的,它是通過tcp/ip協議,和網卡通訊保持資料同步的。
DRBD有三種協議:
協議A:資料在本地完成寫操作且資料已經發送到TCP/IP協議棧的隊列中,則認為寫操作完成。如果本地節點的寫操作完成,此時本地節點發生故障,而資料還處在TCP/IP隊列中,則資料不會發送到對端節點上。因此,兩個節點的資料將不會保持一致。這種協議雖然高效,但是並不能保證資料的可靠性。
協議B:資料在本地完成寫操作且資料已到達對端節點則認為寫操作完成。如果兩個節點同時發生故障,即使資料到達對端節點,這種方式同樣也會導致在對端節點和本地節點的資料不一致現象,也不具有可靠性。
協議C:只有當本地節點的磁碟和對端節點的磁碟都完成了寫操作,才認為寫操作完成。這是叢集流行的一種方式,應用也是最多的,這種方式雖然不高效,但是最可靠。
環境:
192.168.40.12 centosA
192.168.40.19 centosB
修改hosts檔案保持兩台伺服器能互相通訊,然後建立ssh互信:
[[email protected] ~]# ssh-keygen[[email protected] ~]# ssh-copy-id centosB
設定時間同步:
[[email protected] ~]# crontab -l*/5 * * * * /usr/sbin/ntpdatecn.pool.ntp.org[[email protected] ~]# crontab -l*/5 * * * * /usr/sbin/ntpdatecn.pool.ntp.org
安裝DRBD:
[[email protected] ~]# rpm --importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.org[[email protected] ~]# rpm -Uvhhttp://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm[[email protected] ~]# yum install -ykmod-drbd84 drbd84-utils
兩個節點都安裝。
修改設定檔:
[[email protected] ~]# cat/etc/drbd.d/global_common.conf# DRBD is the result of over a decade ofdevelopment by LINBIT.# In case you need professional servicesfor DRBD or have# feature requests visithttp://www.linbit.com global { usage-countno; #是否參加DRBD使用統計,預設為yes。官方統計drbd的裝機量 #Decide what kind of udev symlinks you want for "implicit" volumes #(those without explicit volume <vnr> {} block, implied vnr=0): #/dev/drbd/by-resource/<resource>/<vnr> (explicit volumes) #/dev/drbd/by-resource/<resource> (default for implict) udev-always-use-vnr;# treat implicit the same as explicit volumes #minor-count dialog-refresh disable-ip-verification #cmd-timeout-short 5; cmd-timeout-medium 121; cmd-timeout-long 600;} common { protocol C; #使用DRBD的同步協議 handlers{ #These are EXAMPLE handlers only. #They may have severe implications, #like hard resetting the node under certain circumstances. #Be careful when choosing your poison. 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"; #fence-peer "/usr/lib/drbd/crm-fence-peer.sh"; #split-brain "/usr/lib/drbd/notify-split-brain.sh root"; #out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root"; #before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 ---c 16k"; #after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh; #quorum-lost "/usr/lib/drbd/notify-quorum-lost.sh root"; } startup{ #wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb } options{ #cpu-mask on-no-data-accessible #RECOMMENDED for three or more storage nodes with DRBD 9: #quorum majority; #on-no-quorum suspend-io | io-error; } disk{ on-io-errordetach; #配置I/O錯誤處理策略為分離 #size on-io-error fencing disk-barrier disk-flushes #disk-drain md-flushes resync-rate resync-after al-extents # c-plan-ahead c-delay-targetc-fill-target c-max-rate # c-min-rate disk-timeout } net{ #protocol timeout max-epoch-size max-buffers #connect-int ping-int sndbuf-size rcvbuf-size ko-count #allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri #after-sb-1pri after-sb-2pri always-asbp rr-conflict #ping-timeout data-integrity-alg tcp-cork on-congestion #congestion-fill congestion-extents csums-alg verify-alg #use-rle } syncer{ rate 1024M; #設定主備節點同步時的網路速率 }}
建立設定檔:
[[email protected] ~]# cat /etc/drbd.d/mysql.resresource mysql { ##資源名稱protocol C; ##使用的協議meta-disk internal;device /dev/drbd1; ##DRBD裝置名稱syncer {verify-alg sha1; ##密碼編譯演算法}net {allow-two-primaries;}on centosa {disk /dev/sdb1; ##drbd使用的磁碟分割address 192.168.40.12:7789; ##設定drbd監聽地址與連接埠}on centosb {disk /dev/sdb1;address 192.168.40.19:7789;}}
將設定檔複製到對端節點:
[[email protected] ~]# scp -rp /etc/drbd.d/*centosb:/etc/drbd.d/
在centosa上啟動:
[[email protected] ~]# drbdadm create-md mysqlinitializing activity loginitializing bitmap (64 KB) to all zeroWriting meta data...New drbd meta data block successfullycreated.
[[email protected] ~]# modprobe drbd
查看核心是否已載入了模組:
[[email protected] ~]# lsmod | grep drbddrbd 396875 0libcrc32c 12644 2 xfs,drbd
用drbd啟動mysql並設定centosa為主mysql:
[[email protected] ~]# drbdadm up mysql[[email protected] ~]# drbdadm -- --forceprimary mysql
查看狀態:
[[email protected] ~]# cat /proc/drbdversion: 8.4.10-1 (api:1/proto:86-101)GIT-hash:a4d5de01fffd7e4cde48a080e2c686f9e8cebf4c build by [email protected], 2017-09-1514:23:22 1:cs:WFConnection ro:Primary/Unknown ds:UpToDate/DUnknown C r----s ns:0 nr:0 dw:0 dr:912 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:2096028
在對端節點執行操作:
[[email protected] ~]# drbdadm create-md mysqlinitializing activity loginitializing bitmap (64 KB) to all zeroWriting meta data...New drbd meta data block successfullycreated.[[email protected] ~]# modprobe drbd[[email protected] ~]# drbdadm up mysql
在對端查看狀態:
[[email protected] ~]# cat /proc/drbdversion: 8.4.10-1 (api:1/proto:86-101)GIT-hash:a4d5de01fffd7e4cde48a080e2c686f9e8cebf4c build by [email protected], 2017-09-1514:23:22 1:cs:SyncTarget ro:Secondary/Primary ds:Inconsistent/UpToDate C r----- ns:0 nr:1748992 dw:1748992 dr:0 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:347036 [===============>....]sync‘ed: 83.6% (347036/2096028)K finish:0:00:08 speed: 39,832 (39,748) want: 59,560 K/sec
格式化並掛載:
[[email protected] ~]# mkfs.xfs /dev/drbd1[[email protected] ~]# mount /dev/drbd1 /data
在資料庫添加一個資料庫:
MariaDB [(none)]> create databasecentos;Query OK, 1 row affected (0.04 sec) MariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || centos || mysql || performance_schema || test |+--------------------+5 rows in set (0.00 sec)
查看資料庫存放位置:
[[email protected] ~]# ls /dataaria_log.00000001 aria_log_control centos ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
停止資料庫並卸載:
[[email protected]a ~]# systemctl stop mariadb[[email protected] ~]# umount /data
要想從能掛載必須將主降為從:
[[email protected] ~]# drbdadm secondary mysql
然後去到從,將從升為主然後掛載:
[[email protected] ~]# drbdadm primary mysql[[email protected] ~]# mount /dev/drbd1 /data
查看是否有資料庫檔案:
[[email protected] ~]# cd /data[[email protected] data]# lsaria_log.00000001 aria_log_control centos ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
本文出自 “營運小記” 部落格,請務必保留此出處http://lsfandlinux.blog.51cto.com/13405754/1975811
DRBD+mysql高可用