mysql主主同步复制中keepavlied故障切换

发布时间:2019-12-05编辑:脚本学堂
本文介绍了mysql主主同步复制中,使用keepalived进行故障切换的配置方法,配置虚拟IP-VIP,安装配置keepalived,完成单点故障转换切换配置。

在“mysql主主同步复制master-master配置”一文中实现了mysql双主模式,即互相备份。

本节用keepavlied实现单点故障切换。

一、配置虚拟IP-VIP
首先,配置VIP节点IP地址,把每台物理地址执行到虚拟IP。

虚拟IP-VIP为: 192.168.15.181
 

#vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
IPADDR=192.168.15.181
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:d8:b3:ca brd ff:ff:ff:ff:ff:ff
    inet 192.168.15.178/24 brd 192.168.15.255 scope global eth0
    inet 192.168.15.181/32 scope global eth0
    inet 192.168.15.181/24 brd 192.168.15.255 scope global secondary eth0:0
    inet6 fe80::20c:29ff:fed8:b3ca/64 scope link
       valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
    link/sit 0.0.0.0 brd 0.0.0.0

二、安装配置keepalived
下载地址:http://www.keepalived.org/software

1)、安装:
 

#wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
#tar -zxvf keepalived-1.2.7.tar.gz
#cd keepalived-1.2.7
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-128.el5-x86_64/   #kernel目录根据实际修改。

预编译后出现:
 

Keepalived configuration
------------------------
Keepalived version       : 1.2.7
Compiler     : gcc
Compiler flags     : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib    : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl     : No
Use VRRP Framework       : Yes
Use VRRP VMAC      : No
SNMP support       : No
Use Debug flags    : No
 

注意,如果没有用到LVS可以把lvs去掉即:--disable-lvs-syncd --disable-lvs
 

#make && make install

整理管理文件:
 

#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
 

建立配置文件目录(注意:keepalived的配置文件默认在/etc/keepalived/目录)
 

#mkdir /etc/keepalived
 

两台服务器(两个节点)都这样安装即可

2)配置
节点A192.168.15.178配置:
 

#vim /etc/keepalived/keepalived.conf
global_defs {
    notification_email
  {
  roc@example.com
  }
    notification_email_from roc@example.com
    smtp_server 127.0.0.1
    stmp_connect_timeout 30
    router_id MySQL-ha
    }
vrrp_script chk_mysql {
  script "killall -0 mysqld" #检测mysqld进程,也可以替换成检测脚本
  interval 5
  }
vrrp_instance VI_1 { 
     state master   #从库配置此处均是slave 
     interface eth0 
     virtual_router_id 51 
     priority 100   #优先级,另一台改为90 
     advert_int 1 
     nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置 
     authentication { 
  auth_type PASS 
  auth_pass 1111 
  } 
     virtual_ipaddress { 
       192.168.15.181
  } 
     track_script {
  chk_mysql  #执行上面的命令
  }
     }
virtual_server 192.168.15.181 3306 {
    delay_loop 6
    lb_algo wcl
    lb_kind DR
    #nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 192.168.15.178 3306 {
  weight 1
  TCP_CHECK {
    connect_timeout 10  #(10秒无响应超时)
    nb_get_retry 3
    delay_before_retry 3
    connect_port 3306
  }
    }
    real_server 10.8.10.185 3306 {
  weight 1
  TCP_CHECK {
      connect_timeout 10  #(10秒无响应超时)
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
  }
    }
}

节点192.168.15.185配置:
 

#vim /etc/keepalived/keepalived.conf
global_defs {
    notification_email
  {
  roc@example.com
  }
    notification_email_from roc@example.com
    smtp_server 127.0.0.1
    stmp_connect_timeout 30
    router_id MySQL-ha
    }
vrrp_script chk_mysql {
  script "killall -0 mysqld"  #检测mysqld进程,也可以替换成检测脚本
  interval 5
  }
vrrp_instance VI_1 { 
     state slave   #主库配置此处均是master 
     interface eth0 
     virtual_router_id 51 
     priority 90
     advert_int 1 
     authentication { 
  auth_type PASS 
  auth_pass 1111 
  } 
     virtual_ipaddress { 
       192.168.15.181
  } 
     track_script {
  chk_mysql  #执行上面的命令
  }
     }
virtual_server 192.168.15.181 3306 {
    delay_loop 6
    lb_algo wcl
    lb_kind DR
    #nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 192.168.15.178 3306 {
  weight 1
  TCP_CHECK {
    connect_timeout 10       #(10秒无响应超时)
    nb_get_retry 3
    delay_before_retry 3
    connect_port 3306
  }
    }
    real_server 10.8.10.185 3306 {
  weight 1
  TCP_CHECK {
      connect_timeout 10       #(10秒无响应超时)
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
  }
    }
}

3)、开启服务
 

#/usr/local/keepalived/sbin/keepalived -D
#ps -aux | grep keepalived
root     15416  0.0  0.0  61148   736 pts/1    S+   16:43   0:00 grep keepalived
root     31121  0.0  0.0  35832   492 ?  Ss   Jan22   0:00 keepalived -D
root     31122  0.0  0.0  37924  1372 ?  S    Jan22   0:00 keepalived -D
root     31125  0.0  0.0  37924  1000 ?  S    Jan22   0:03 keepalived -D

三、测试单点故障切换是否成功
mysql远程登录测试
客户端登录vip,看是否能登录,在登录之两台mysql服务器都要授权允许从远程登录
 

mysql>  grant all privileges on phpcmsv9.* to 'dbuser_do'@'192.168.10.6' identified by '123456';     
No connection. Trying to reconnect...
Connection id:    1168
Current database: phpcmsv9
Query OK, 0 rows affected (0.11 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

客户端登录结果:
mysql主主同步复制中keepavlied故障切换

此时查询的是192.168.15.185的数据库信息,关闭192.168.15.185 Mysql,继续联系数据库。
mysql主主同步复制中keepavlied故障切换2

此时连接的是192.168.15.178的数据库。

keepavlied完成后,测试当master服务器和slave也能自动切换,同步保证数据同步正常。