keepalived搭建主从架构,主主复制架构

实例拓扑图:

复制其最终指标是让一台服务器的数目和其它的服务器的数码保持同步,已落得数据冗余或许服务的载重均衡。一台主服务器能够接连多台从服务器,并且从服务器也可以反过来作为主服务器。主从服务器能够置身差别的互联网拓扑中,由于mysql的雄强复制功效,其复制指标能够是独具的数据库,也能够是少数数据库,甚至是有些数据库中的某个表展开复制。

keepalived+MySQL双主搭建进度

图片 1

图片 2

MySQL援助的二种复制方案:依照语句复制,基于行复制
遵照语句复制基于行复制,那二种复制形式都以经过记录主服务器的二进制日志中别的有大概导致数据库内数据产生转移的SQL语句到联网日志,并且在从服务器上进行以下中继日志内的SQL语句,而实现与主服务器的数目同步。不一样的是,当主服务器上执行了2个依据变量的数额并将其履新到数据库中,如now()函数,而那时根据语句复制时记下的就是该SQL语句的全方位语法,而依照行复制正是将now()更新到数据库的数值记录下来。
例如:在主服务器上执行以下语句:
mysql>update user set createtime=now() where sid=16;
一旦此时now()重临的值是:2013-04-16 20:46:35
依据语句的复制就会将其记录为:update user set createtime=now() where
sid=16;
依照行复制的就会将其记录为:update user set createtime=’二零一二-04-16
20:46:35′ where sid=16;

首先要不难询问一下keepalived:

此章节描述主从架构

核心架构  — 近期曾经不建议利用,推荐应用复制集

主题配置能够在配备文件中布署
从节点能够在起步之后选拔命令追加主节点,db.source.insert({“host”:”IP+端口”})

一而再节点:mongo 127.0.0.1:29991,主节点允许读写
a、从节点能够一而再,不过区别意读写,倘若想读 能够设置
 db.getMongo().setSlaveOk()

b、在主节点运维 db.getMongo().setSlaveOk()

壹 、设置文件夹目录

图片 3

二 、各种目录的层级 均为 DB目录,日志目录,配置文件1个

图片 4

叁 、配置文件内容

架构:
a、主节点配置文件
dbpath=D:\MS\Master\DB 
logpath=D:\MS\Master\LOG\s.log 
logappend=true 
port=29991 
master=true

b、从节点配置文件
节点1
dbpath=D:\MS\Slave2\DB 
logpath=D:\MS\Slave2\LOG\s.log 
logappend=true 
port=29993 
slave=true
source=127.0.0.1:29991

节点2
dbpath=D:\MS\Slave2\DB 
logpath=D:\MS\Slave2\LOG\s.log 
logappend=true 
port=29993 
slave=true
source=127.0.0.1:29991

④ 、配置文件处理好今后,使用CMD命令行形式,mongod –config 配置文件路径
 启动伍个实例。运行成功的界面如下,需求种种打开多个指令shell运维。

图片 5

五 、使用mongo 127.0.0.1:29991
 链接到主节点的实例,然后在实例之中创设集合和笔录,将直接同步到别的1个从节点上边。

图片 6

到达此步骤 将是打响搭建最简便的测试
主从架构。(因为里面并不曾动用auth登录认证等,使用安顿文件也是老的本子写法,YAML版本配置文件写法如下:)

# Where and how to store data.
storage:
  dbPath: D:\MS\Master\DB
  journal:
    enabled: true

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: D:\MS\Master\LOG\m.log

# network interfaces
net:
  port: 29991

#processManagement:

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

类型文件

DTiggo1和D中华V2布置keepalived和lvs作主从架构或主主架构,宝马X3S1和RubiconS2安顿nginx搭建web站点。

实行主从复制运行的四个线程
Binlog dump线程:将二进制日志的剧情发送给从服务器
I/O从线程:将接受的的多少写入到联网日志
SQL线程:一遍从中继日志中读出一句SQL语句在从服务器上实行

Keepalived是Linux下三个轻量级其他高可用解决方案。高可用(High
Avalilability,HA),其实三种不相同的意思:广义来讲,是指任何连串的高可用行,狭义的来讲正是之主机的冗余和接管,

留神:各节点的岁月须要一起(ntpdate
ntp1.aliyun.com);关闭firewalld(systemctl stop
firewalld.service,systemctl disable
firewalld.service),设置selinux为permissive(setenforce
0);同时保障各网卡支持MULTICAST(多播)通信。

① 、主从复制:
防微杜渐干活:
1.修改配置文件(server_id一定要修改)
2.起家复制用户
3.开发银行从服务器的从劳动进程

keepalived搭建主从架构,主主复制架构。它与HeartBeat 罗丝HA
实现均等类似的效劳,都能够完毕服务仍旧互连网的高可用,但是又有反差,HeartBeat是一个正规的、成效完善的高可用软件,它提供了HA
软件研商所需的基本功用,比如:心跳检查和测试、财富接管,检查和测试集群中的服务,在集群节点转移共享IP地址的持有者等等。HeartBeat功能强大,不过配置和动用相对相比麻烦,

经过命令ifconfig能够查看到是还是不是打开了MULTICAST:

规划:
Master:IP地址:172.16.4.11    版本:mysql-5.5.20
Slave:IP地址:172.16.4.12    版本:mysql-5.5.20
此间需注意,mysql复制半数以上皆未来向包容,所以,从服务器的版本一定要压倒或等于主服务器的本子。
1、Master
修改配置文件,将其设为mysql主服务器
#vim /etc/my.cnf
server_id=11                #修改server_id=11
log_bin=mysql-bin            #敞开二进制日志
sync_binlog=1              
#任何3个政工提交之后就立马写入到磁盘中的二进制文件
innodb_flush_logs_at_trx_commit=1      
#其他3个事物提交之后就立时写入到磁盘中的日志文件

与HeartBeat比较,Keepalived主假若因此虚拟路由冗余来落到实处高可用效能,即使它没有HeartBeat效能强大,不过Keepalived安插和应用万分的简练,全体配置只要求贰个配备文件即能够做到。

     
 图片 7

封存退出
#service mysql reload             #重复载入mysql的配置文件

keepalived的干活原理如下:

keepalived的着力架构

② 、Master上创制用户,授予复制权限
mysql>grant replication client,replication slave on *.* to
repl@172.16.4.12 identified by ‘135246’;
mysql>flush privileges;

Keepalived工作在TCP/IP 参考模型的
三层、四层、五层,也正是个别为:网络层,

搭建RS1:

[root@RS1 ~]# yum -y install nginx   #安装nginx
[root@RS1 ~]# vim /usr/share/nginx/html/index.html   #修改主页
    <h1> 192.168.4.118 RS1 server </h1>
[root@RS1 ~]# systemctl start nginx.service   #启动nginx服务
[root@RS1 ~]# vim RS.sh   #配置lvs-dr的脚本文件
    #!/bin/bash
    #
    vip=192.168.4.120
    mask=255.255.255.255
    case $1 in
    start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig lo:0 $vip netmask $mask broadcast $vip up
        route add -host $vip dev lo:0
        ;;
    stop)
        ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ;;
    *) 
        echo "Usage $(basename $0) start|stop"
        exit 1
        ;;
    esac
[root@RS1 ~]# bash RS.sh start

3、Slave
修改配置文件,将其安装为一个mysql从服务器
#vim /etc/my.cnf
server_id=12                #修改server_id=12
#log-bin               
#诠释掉log-bin,从服务器不供给二进制日志,因而将其倒闭
relay-log=mysql-relay               
#概念中继日志名,开启从服务器中继日志
relay-log-index=mysql-relay.index    
#概念中继日志索引名,开启从服务器中继索引
read_only=1                   
#设定从服务器只能进展读操作,不可能拓展写操作

传输层和应用层,依据TCP、IP参数模型隔层所能完结的效应,Keepalived运营机制如下:

参考大切诺基S1的配置搭建WranglerS2。

保留退出
#service mysql reload             #再次载入mysql的布局文件

在互联网层:大家精通运营这多少个根本的说道,互联网络IP协议,互联互连网可决定报中华全国文艺界抗击敌人组织议ICMP、地址转换协议A瑞虎P、反向地址转换协议RATiguanP,在互连网层Keepalived在网络层选拔最普遍的劳作措施是透过ICMP协议向服务器集群中的每四个节点发送贰个ICMP数据包(有点类似与Ping的效益),若是有个别节点没有回来响应数据包,那么认为该节点爆发了故障,Keepalived将告诉以此节点失效,并从服务器集群中除去故障节点。

搭建DR1:

[root@DR1 ~]# yum -y install ipvsadm keepalived   #安装ipvsadm和keepalived
[root@DR1 ~]# vim /etc/keepalived/keepalived.conf   #修改keepalived.conf配置文件
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id 192.168.4.116
       vrrp_skip_check_adv_addr
       vrrp_mcast_group4 224.0.0.10
    }

    vrrp_instance VIP_1 {
        state MASTER
        interface eno16777736
        virtual_router_id 1
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass %&hhjj99
        }
        virtual_ipaddress {
          192.168.4.120/24 dev eno16777736 label eno16777736:0
        }
    }

    virtual_server 192.168.4.120 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        protocol TCP

        real_server 192.168.4.118 80 {
            weight 1
            HTTP_GET {
                url {
                  path /index.html
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
        real_server 192.168.4.119 80 {
            weight 1
            HTTP_GET {
                url {
                  path /index.html
                  status_code 200
                }
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
         }
    }
[root@DR1 ~]# systemctl start keepalived
[root@DR1 ~]# ifconfig
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.116  netmask 255.255.255.0  broadcast 192.168.4.255
            inet6 fe80::20c:29ff:fe93:270f  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)
            RX packets 14604  bytes 1376647 (1.3 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 6722  bytes 653961 (638.6 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.4.120  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:93:27:0f  txqueuelen 1000  (Ethernet)
[root@DR1 ~]# ipvsadm -ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.4.120:80 rr
      -> 192.168.4.118:80             Route   1      0          0         
      -> 192.168.4.119:80             Route   1      0          0

④ 、验证Slave上衔接日志以及server_id是或不是均生效
mysql>show variables like ‘relay%’;
+———————–+—————–+
| Variable_name         | Value           |
+———————–+—————–+
| relay_log             | relay-bin       |
| relay_log_index       | relay-bin.index |
| relay_log_info_file   | relay-log.info  |
| relay_log_purge       | ON              |
| relay_log_recovery    | OFF             |
| relay_log_space_limit | 0               |
+———————–+—————–+
mysql>show variables like ‘server_id’;
+—————+——-+
| Variable_name | Value |
+—————+——-+
| server_id     | 12    |
+—————+——-+

在传输层:提供了多少个十分重要的情商:传输控制协议TCP和用户数据协议UDP,传输控制协议TCP能够提供可相信的数码输劳、IP地址和端口,代表TCP的3个连接端,要获取TCP服务,需求在发送机的三个端口和接收机的1个端口上创设连接,而Keepalived在传输层里使用了TCP协议的端口连接和围观技术来判定集群节点的端口是不是正规,比如对于广泛的WEB服务器80端口。也许SSH服务22端口,Keepalived一旦在传输层探测到那一个端口号没有多少响应和数目再次来到,就觉得那些端口暴发尤其,然后强制将那个端口所对应的节点从服务器集群中剔除掉。

DCRUISER2的搭建基本同DCR-V1,首要修改一下配备文件中/etc/keepalived/keepalived.conf的state和priority:state BACKUP、priority 90. 同时大家发现作为backup的DSportage2没有启用eno16777736:0的网口:

图片 8

五 、运行从服务器的从劳动进度
场景① 、假若主服务器和从服务器都以新成立的,并不曾新增其余数据,则实行以下命令:
mysql>change master to \
master_host=’172.16.4.11′,
master_user=’repl’,
master_password=’135246′;
mysql>show slave status\G
*************************** 1. row
***************************
Slave_IO_State:
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 25520
Relay_Log_Space: 2565465
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
mysql>start slave;
mysql>show slave status\G
*************************** 1. row
***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 360
Relay_Log_Space: 300
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11

在应用层:能够运转FTP,TELNET,SMTP,DNS等种种差别种类的高层协商,Keepalived的运维格局也特别全面化和复杂化,用户能够经过自定义Keepalived工作形式,例如:能够透过编写程序只怕脚本来运营Keepalived,而Keepalived将依据用户的设定参数检查和测试各类程序照旧服务是还是不是允许符合规律,假设Keepalived的检查和测试结果和用户设定的不平等时,Keepalived将把相应的服务器从服务器集群中删去

客户端进行测试:

[root@client ~]# for i in {1..20};do curl http://192.168.4.120;done   #客户端正常访问
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>

[root@DR1 ~]# systemctl stop keepalived.service   #关闭DR1的keepalived服务

[root@DR2 ~]# systemctl status keepalived.service   #观察DR2,可以看到DR2已经进入MASTER状态
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-09-04 11:33:04 CST; 7min ago
  Process: 12983 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 12985 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─12985 /usr/sbin/keepalived -D
           ├─12988 /usr/sbin/keepalived -D
           └─12989 /usr/sbin/keepalived -D

Sep 04 11:37:41 happiness Keepalived_healthcheckers[12988]: SMTP alert successfully sent.
Sep 04 11:40:22 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) Transition to MASTER STATE
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) Entering MASTER STATE
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) setting protocol VIPs.
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: VRRP_Instance(VIP_1) Sending/queueing gratuitous ARPs on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120
Sep 04 11:40:23 happiness Keepalived_vrrp[12989]: Sending gratuitous ARP on eno16777736 for 192.168.4.120

[root@client ~]# for i in {1..20};do curl http://192.168.4.120;done   #可以看到客户端正常访问
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>
<h1> 192.168.4.119 RS2 server</h1>
<h1> 192.168.4.118 RS1 server </h1>

场景贰 、如若主服务器已经运营过一段了,从服务器是新拉长的,则供给将主服务器之前的数码导入到从服务器中:
Master:
#mysqldump -uroot -hlocalhost -p123456 –all-databases
–lock-all-tables –flush-logs –master-data=2 >
/backup/alldatabase.sql
mysql>flush tables with read lock;
mysql>show master status;
+——————+———-+————–+——————+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000004 |      360 |              |                  |
+——————+———-+————–+——————+
mysql>unlock tables;
#scp /backup/alldatabase.sql 172.16.4.12:/tmp

看一下主导条件

keepalived的主主框架结构

Slave:
#mysql -uroot -p123456 < /tmp/alldatabase.sql
mysql>change master to \
master_host=’172.16.4.11′,
master_user=’repl’,
master_password=’135246′,
master_log_file=’mysql-bin.000004′,
master_log_pos=360;
mysql>show slave status\G
*************************** 1. row
***************************
Slave_IO_State:
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 360
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 360
Relay_Log_Space: 107
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
mysql>start slave;

server1:MySQL5.7.14+keepalived1.2+172.16.16.34
server2:MySQL5.7.14+keepalived1.2+172.16.16.35
VIP:172.16.16.20

 修改本田UR-VS1和LacrosseS2,添加新的VIP:

[root@RS1 ~]# cp RS.sh RS_bak.sh
[root@RS1 ~]# vim RS_bak.sh   #添加新的VIP
    #!/bin/bash
    #
    vip=192.168.4.121
    mask=255.255.255.255
    case $1 in
    start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig lo:1 $vip netmask $mask broadcast $vip up
        route add -host $vip dev lo:1
        ;;
    stop)
        ifconfig lo:1 down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ;;
    *)
        echo "Usage $(basename $0) start|stop"
        exit 1
        ;;
    esac
[root@RS1 ~]# bash RS_bak.sh start
[root@RS1 ~]# ifconfig
    ...
    lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.120  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback)

    lo:1: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.121  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback) 
[root@RS1 ~]# scp RS_bak.sh root@192.168.4.119:~
root@192.168.4.119's password: 
RS_bak.sh                100%  693     0.7KB/s   00:00

[root@RS2 ~]# bash RS_bak.sh   #直接运行脚本添加新的VIP 
[root@RS2 ~]# ifconfig
    ...
    lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.120  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback)

    lo:1: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 192.168.4.121  netmask 255.255.255.255
            loop  txqueuelen 0  (Local Loopback)

mysql>show slave status\G
*************************** 1. row
***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 360
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 360
Relay_Log_Space: 300
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11

我们两台机器是搭建的MySQL双主,我们日常只会由此VIP对MySQL实行读写,大家要促成的是,当VIP所在的主机的MySQLDOWN掉未来,VIP能够切换成此外一台机械上还要三番五次提供服务。