【MySQL進階】Keepalived1.4.0結合MySQL 5.7.19實現主備高可用
阿新 • • 發佈:2018-10-27
port 腳本 amp ado roo ins log openss net 1、基本環境
數據庫安裝及主備同步接上一篇文章:http://blog.51cto.com/13946719/2309514
JDK 1.8_171
MySQL 5.7.19
CentOS 7.4
Keepalived 1.4.0
DB1:192.168.200.180
DB2:192.168.200.181
VIP: 192.168.200.99
2、下載安裝Keepalived
2.1、keepalived下載
下載地址:wget http://www.keepalived.org/software/keepalived-1.4.0.tar.gz
2.2、keepalived安裝
2臺均按照此種方式進行安裝
yum install gcc gcc-c++ make openssl openssl-devel net-snmp-devel psmisc ipvsadm libnfnetlink-devel popt popt-devel popt-static openssl-devel kernel-devel libnl libnl-devel -y # 安裝依賴 cd /usr/local/src tar -zvxf keepalived-1.4.0.tar.gz # 解壓 cd keepalived-1.4.0 ./configure --prefix=/usr/local/keepalived --enable-snmp # 編譯 make && make install # 安裝 mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ # 拷貝配置 ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived ln -s /usr/local/keepalived/sbin/keepalived /sbin/keepalived
2.3、Keepalived配置
DB1上面的配置:
[root@mysql01 keepalived-1.4.0]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak [root@mysql01 keepalived-1.4.0]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id Node_Master } vrrp_instance VI_1 { state BACKUP # 可配置master和backup模式,為了防止腦裂現象,主備均需要設置為backup模式,master模式會搶占VIP interface ens33 # 網卡名 virtual_router_id 43 # VRRP組名,兩個節點的設置必須一樣,以指明各個節點屬於同一VRRP組 priority 100 # 權重,主節點要大於備節點 unicast_src_ip 192.168.200.180 # 本地IP地址 unicast_peer { 192.168.200.181 # 對端IP地址,此地址一定不能忘記 } nopreempt # 配合backup,防止切換後,主庫服務恢復正常後,IP漂移過來 advert_int 1 # 組播信息發送間隔,兩個節點設置必須一樣 authentication { # 設置驗證信息,兩個節點必須一致 auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.200.99 # VIP地址 } } virtual_server 192.168.200.99 3306 { delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 192.168.200.180 3306 { weight 1 notify_down /etc/keepalived/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
DB2上面配置
[root@mysql02 keepalived-1.4.0]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak [root@mysql02 keepalived-1.4.0]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id Node_backup } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 43 priority 90 unicast_src_ip 192.168.200.181 unicast_peer { 192.168.200.180 } advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.200.99 } } virtual_server 192.168.200.99 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.200.181 3306{ weight 1 notify_down /etc/keepalived/mysql.sh echo ‘3‘ > /etc/keepalived/t.log TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
mysql.sh腳本配置內容(2臺一樣):
[root@mysql01 keepalived]# vim /etc/keepalived/mysql.sh
#!/bin/bash
run_status=`service keepalived status|grep running|wc -l`
pro_status=`ps -ef |grep keepalived|grep -v grep |wc -l`
service keepalived stop
if [ ${run_status} != 0 ] || [ ${pro_status} != 0 ]
then
pkill keepalived
fi
給mysql.sh賦可執行權限
[root@mysql01 keepalived]# chmod +x mysql.sh
都配置好後啟動keepalived進行測試:
service keepalived start # 啟動Keepalived
service keepalived restart # 重啟Keepalived
service keepalived stop # 停止Keepalived
service keepalived status # 查看Keepalived運行狀態
ps aux | grep keepalived # 查看Keepalived進程
3.3、測試Keepalived
2臺均啟動Keepalived後,分別查看網卡
從上圖可以看出,VIP地址在主節點
使用Navicat工具分別連接2個節點和VIP節點
這個時候我們將節點1的MySQL停止,模擬主節點數據庫故障來測試VIP是否會漂移到備節點
如上圖,主節點MySQL停止後,Keepalived進程也結束了,VIP地址不見了
查看備節點的網卡,發現VIP已經漂移過來了
用Navicat打開VIP,可以正常訪問,主節點無法打開
重新啟動主節點MySQL服務和Keepalived服務,在停止備節點測試(略過)
到此,結合前2篇文章,MySQL 5.7.19在CentOS 7.4操作系統上的安裝,同步配置,高可用配置已經全部完成!
【MySQL進階】Keepalived1.4.0結合MySQL 5.7.19實現主備高可用