MySQL dual master + keepalived to achieve high availability cluster

1. Install and configure keepalived

master-a端:

1)安装keepalived
[root@CentOS ~]#
[root@CentOS ~]# yum install keepalived 安装keepalived
[root@CentOS ~]#
[root@CentOS ~]# rpm -qa keepalived
keepalived-1.3.5-16.el7.x86_64
[root@CentOS ~]#

2) Modify the keepalived configuration file
[root @ CentOS ~] #
[root @ CentOS ~] #> /etc/keepalived/keepalived.conf #Clear the original configuration file
[root @ CentOS ~] #
[root @ CentOS ~] # vim /etc/keepalived/keepalived.conf #Custom configuration file
! Configuration File for keepalived

global_defs {
notification_email {br/>[email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id MySQL-HA
}

vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 192.168.18.103
virtual_router_id 55
priority 100
advert_int 1
nopreempt

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
192.168.18.110/24
}

track_script {
chk_mysql
}
}

[root @ CentOS ~] #
[root @ CentOS ~] # cat /etc/keepalived/keepalived.conf #View configuration
MySQL dual master + keepalived to achieve high availability cluster

3) Write mysql status detection script
[root @ CentOS ~] #
[root @ CentOS ~] # vim /etc/keepalived/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

[root @ CentOS ~] #
[root @ CentOS ~] # cat /etc/keepalived/chk_mysql.sh #View script content
MySQL dual master + keepalived to achieve high availability cluster
[root @ CentOS ~] #
[root @ CentOS ~] # chmod + x / etc / keepalived / chk_mysql .sh #Give the script executable permissions

4)启动keepalived
[root@CentOS ~]#
[root@CentOS ~]# systemctl start keepalived
[root@CentOS ~]# systemctl enable keepalived

master-b end:
1) Install keepalived
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # yum install keepalived

2) Modify the keepalived configuration file
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] #> /etc/keepalived/keepalived.conf #Clear the original configuration
[root @ CentOS-2 ~] # vim / etc / keepalived / keepalived.conf #Custom configuration file

! Configuration File for keepalived

global_defs {
notification_email {br/>[email protected]
}
smtp_server 192.168.23.1
smtp_connect_timeout 30
router_id MySQL-HA
}

vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
mcast_src_ip 192.168.18.104
virtual_router_id 55
priority 95
advert_int 1

authentication {
auth_type PASS
auth_pass 123456
}

virtual_ipaddress {
192.168.18.110/24
}

track_script {
chk_mysql
}
}

[root@CentOS-2 ~]#
[root@CentOS-2 ~]# cat /etc/keepalived/keepalived.conf
MySQL dual master + keepalived to achieve high availability cluster

3) Write mysql status detection script
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # vim /etc/keepalived/chk_mysql.sh

#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi

[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # cat /etc/keepalived/chk_mysql.sh
MySQL dual master + keepalived to achieve high availability cluster
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # chmod + x / etc /keepalived/chk_mysql.sh #Grant executable permissions to the script

4)启动keepalived
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl start keepalived
[root@CentOS-2 ~]# systemctl enable keepalived

2. Test the availability of keepalived + MySQL dual master

1) View the mysql status

master-a end:
[root @ CentOS ~] #
[root @ CentOS ~] # systemctl status mysqld #Check if mysql is running
MySQL dual master + keepalived to achieve high availability cluster

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# systemctl status mysqld
MySQL dual master + keepalived to achieve high availability cluster

2) Check which server the VIP is on

Master-a end:
[root @ CentOS ~] #
[root @ CentOS ~] # ip a #It is found that the VIP is now on the master-a end
MySQL dual master + keepalived to achieve high availability cluster

master-b端:
[root@CentOS-2 ~]#
[root@CentOS-2 ~]# ip a
MySQL dual master + keepalived to achieve high availability cluster

3) Stop the mysql service on the master-a side and check which server the VIP is on

master-a端:
[root@CentOS ~]#
[root@CentOS ~]# systemctl stop mysqld
[root@CentOS ~]# ip a
MySQL dual master + keepalived to achieve high availability cluster

master-b end:
[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # ip a #Go to master-b end to view
MySQL dual master + keepalived to achieve high availability cluster

4) Start the mysql service and keepalived service on the master-a side, stop the mysql service on the master-b side, and check which server the VIP is on
[root @ CentOS ~] #
[root @ CentOS ~] # systemctl start mysqld #Start mysql on the master-a
[root @ CentOS ~] # systemctl start keepalived #start keepalived on the master-a
[root @ CentOS ~] #

[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # systemctl stop mysqld #stop master-b mysql
[root @ CentOS-2 ~] #

[root @ CentOS ~] #
[root @ CentOS ~] # ip a #Check the master-a network card
MySQL dual master + keepalived to achieve high availability cluster

[root @ CentOS-2 ~] #
[root @ CentOS-2 ~] # ip a #Check the network card status on the master-b side
MySQL dual master + keepalived to achieve high availability cluster

3. Experimental conclusion

Through verification and comparison, it is found that the MySQL dual-master cluster successfully achieved high availability through keepalived

Guess you like

Origin blog.51cto.com/14783377/2486669