天天看点

Mysql+keepalived主主切换

Mysql+keepalived主主切换

一,环境介绍

网络结构:

VIP :192.168.1.30

MYSQL A:192.168.1.21

MYSQL B:192.168.1.22

二、mysql主主同步

三,配置keepalived

1,安装yum -y install keepalived

master A配置文件

[root@master ~]# cat /etc/keepalived/keepalived.conf

#! Configuration File for keepalived

global_defs {

router_id mysql-cluster #修改为自己的主机名

}

##################第一部分###################

vrrp_instance VI_1 {

state BACKUP #都修改成BACKUP

interface eth0

virtual_router_id 60 #默认51 主从都修改为60

priority 100 #优先级(1-254之间),另一台改为90,备用节点必须比主节点优先级低。

advert_int 1

nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来

authentication {

#设置验证信息,两个节点必须一致

auth_type PASS

auth_pass 1111

virtual_ipaddress {

192.168.1.30

##################第二部分###################

virtual_server 192.168.1.30 3306 {

delay_loop 6

lb_algo wrr #LVS算法

lb_kind DR#LVS模式

nat_mask 255.255.255.0

persistence_timeout 50 #会话保持时间

protocol TCP

real_server 192.168.1.21 3306 {

weight 1

notify_down /root/mysql.sh #检测到服务down后执行的脚本

TCP_CHECK {

connect_timeout 10 #连接超时时间

nb_get_retry 3#重连次数

connect_port 3306#健康检查端口

master B配置文件,去掉nopreempt,设置优先级,真实IP

[root@slave1 ~]# cat /etc/keepalived/keepalived.conf

priority 80 #在mysql-ha1上LVS上修改成100

virtual_server 192.168.1.30 3306 {

lb_algo wrr

lb_kind DR

persistence_timeout 50

real_server 192.168.1.22 3306 {

notify_down /root/mysql.sh

connect_timeout 10

nb_get_retry 3

connect_port 3306

3,建立mysql.sh脚本

vim /root/mysql.sh

#!/bin/bash

pkill keepalived

#chmod +x /root/mysql.sh

4,需要安装ipvsadm与设置arp协议的响应,在两台机器都操作

# vim /etc/sysctl.conf 在最后添加,也可以直接修改cat /proc/sys/net/ipv4/conf/eth0/

############arp##############

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

#sysctl -p

四,查看虚拟ip在哪,哪台优先级高,虚拟IP就会在哪

[root@master ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

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 state UP qlen 1000

link/ether 00:0c:29:af:00:a6 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.21/24 brd 192.168.1.255 scope global eth0

inet 192.168.1.30/32 scope global eth0

inet6 fe80::20c:29ff:feaf:a6/64 scope link

[root@slave1 ~]# ip a

link/ether 00:0c:29:3d:8b:c7 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.22/24 brd 192.168.1.255 scope global eth0

inet6 fe80::20c:29ff:fe3d:8bc7/64 scope link

五,验证:通过window远程连接虚拟IP,在两台mysql上都需要存储相同的一个用户和密码用于远程连接,如果能连接表示成功,将master A上的mysql停止,测试是否还能连接。中间如果有错误可以查看/var/log/message日志。

本文转自 deng304749970 51CTO博客,原文链接:http://blog.51cto.com/damondeng/1314549,如需转载请自行联系原作者