Mysql+Keepalived雙主熱備高可用操作記錄 環境: ubuntu18.04.2 mysql5.7.21 實現高可用之前確保自己的mysql已經是主備或者互為主備了,mysql互為主備操作請看我上篇文章: https://www.cnblogs.com/-admin-/p/11654318 ...
Mysql+Keepalived雙主熱備高可用操作記錄
環境:
ubuntu18.04.2
mysql5.7.21
1 #1)安裝keepalived並將其配置成系統服務。master1和master2兩台機器上同樣進行如下操作: 2 apt-get install libssl-dev 3 apt-get install openssl 4 apt-get install libpopt-dev 5 [root@master1 ~]# cd /usr/local/src/ 6 [root@master1 src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz 7 [root@master1 src]# tar -zvxf keepalived-1.3.5.tar.gz 8 [root@master1 src]# cd keepalived-1.3.5 9 [root@master1 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived 10 [root@master1 keepalived-1.3.5]# make && make install 11 mkdir /etc/sysconfig 12 cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 13 cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ 14 cp /usr/local/keepalived/sbin/keepalived /sbin/ 15 mkdir /etc/keepalived 16 cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
實現高可用之前確保自己的mysql已經是主備或者互為主備了,mysql互為主備操作請看我上篇文章: https://www.cnblogs.com/-admin-/p/11654318.html
本次設置的ip:
vip:192.168.85.142
01:192.168.85.140
02:192.168.85.141
請各位按照自己的需求更改相應ip
#01的keepalived.conf配置 ! Configuration File for keepalived global_defs { notification_email { 759571872@qq.com #報警郵件接收人的地址 } notification_email_from 759571872@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 ubuntu001 } vrrp_script chk_mysql_port { #檢測mysql服務是否在運行。有很多方式,比如進程,用腳本檢測等等 script "/opt/chk_mysql.sh" #這裡通過腳本監測 interval 2 #腳本執行間隔,每2s檢測一次 weight -5 #腳本結果導致的優先順序變更,檢測失敗(腳本返回非0)則優先順序 -5 fall 2 #檢測連續2次失敗才算確定是真失敗。會用weight減少優先順序(1-255之間) rise 1 #檢測1次成功就算成功。但不修改優先順序 } vrrp_instance VI_1 { state MASTER interface ens33 #指定虛擬ip的網卡介面 mcast_src_ip 192.168.85.140 virtual_router_id 51 #路由器標識,MASTER和BACKUP必須是一致的 priority 101 #定義優先順序,數字越大,優先順序越高,在同一個vrrp_instance下,MASTER的優先順序必須大於BACKUP的優先順序。這樣MASTER故障恢復後,就可以將VIP資源再次搶回來 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.85.142 } track_script { chk_mysql_port } }
01伺服器配置完畢.
接下來配置02伺服器:
#02的keepalived.conf配置 ! Configuration File for keepalived global_defs { notification_email { 759571872@qq.com #報警郵件接收人的地址 } notification_email_from 759571872@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 ubuntu002 } vrrp_script chk_mysql_port { #檢測mysql服務是否在運行。有很多方式,比如進程,用腳本檢測等等 script "/opt/chk_mysql.sh" #這裡通過腳本監測 interval 2 #腳本執行間隔,每2s檢測一次 weight -5 #腳本結果導致的優先順序變更,檢測失敗(腳本返回非0)則優先順序 -5 fall 2 #檢測連續2次失敗才算確定是真失敗。會用weight減少優先順序(1-255之間) rise 1 #檢測1次成功就算成功。但不修改優先順序 } vrrp_instance VI_1 { state BACKUP interface ens33 #指定虛擬ip的網卡介面 mcast_src_ip 192.168.85.141 virtual_router_id 51 #路由器標識,MASTER和BACKUP必須是一致的 priority 99 #定義優先順序,數字越大,優先順序越高,在同一個vrrp_instance下,MASTER的優先順序必須大於BACKUP的優先順序。這樣MASTER故障恢復後,就可以將VIP資源再次搶回來 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.85.142 } track_script { chk_mysql_port } }
編寫腳本:
#vi /opt/chk_mysql.sh #!/bin/bash counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "${counter}" -eq 0 ]; then /etc/init.d/keepalived stop fi
至此,Mysql+Keepalived雙主熱備高可用已經配置完畢.