Mysql+Keepalived双主热备高可用操作记录
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双主热备高可用已经配置完毕.