Keepalived + LVS + MySQL 主从复制实现读写分离及高可用

本文介绍了一种结合LVS、Keepalived和MySQL半同步复制技术实现数据库高可用性的方案。该方案通过搭建双节点MySQL主从复制架构,并利用Keepalived监控MySQL状态,实现在主节点故障时自动切换到备用节点,确保服务连续性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、架构设计

二、安装配置

1. 配置 MySQL 半同步复制

1.1 半同步复制的基本概念

1.2 半同步复制的潜在问题

1.3 半同步复制的安装部署

2. 下载安装 LVS

3. 下载安装 Keepalived

4. iptables 配置

5. Keepalived 配置

5. 编写 RealServer 的网络配置脚本

6. 启动 RealServer 和 Keepalived

三、测试

参考:


        通过前面两个实验,已经基本了解了 Keepalived 及 LVS 的工作原理。本篇我们要进行一个有别于前面的新实验:使用 Keepalived + LVS + MySQL 主从复制实现读写分离及高可用。

一、架构设计

        具体架构如图1 所示。

图1


        从图1 中看到,使用两台主机做 MySQL 主从复制,实现读写分离,用于提高查询性能。采用 MySQL 5.6.x 的半同步实现数据复制和同步。使用 Keepalived 来监控 MySQL,并提供读写 VIP 漂移。Keepalived 在这里主要用作 RealServer 的健康状态检查以及 LoadBalance 主机和 Backup 主机之间 failover 的实现。任何一台主机宕机都不会影响对外提供服务(读写 VIP 可以漂移),保持 MySQL 数据库服务的高可用性。

        Keepalived 基于 VRRP 协议来实现高可用解决方案,利用其避免单点故障。通常这个解决方案中,至少有 2 台服务器运行 Keepalived,一台为 Master,另一台为 Backup,但对外表现为一个或一组 VIP。Master 会发送特定消息给 Backup,当 Backup 收不到该消息时,则认为 Master 出现故障,Backup 会接管 VIP,继续提供服务,从而保证了高可用性。LVS 在本例的作用是提供读负载均衡。整体架构的设计原理和异常处理可描述如下:

  1. RealServer A 和 B,配置为 MySQL 半同步复制的 Master 和 Slave。
  2. 通过 Keepalived 启用两个虚 IP:W-VIP/R-VIP,一个负责写入,一个负责读取,实现读写分离。
  3. A 和 B 都存在时,LVS 通过 W-VIP 将写请求转发至主机 A,通过 R-VIP 将读请求转发给 A 和 B,实现读负载均衡。
  4. 当主机 A 异常时,B 接管服务,W-VIP/R-VIP 此时都漂移到主机 B 上。
  5. 当主机 B 异常时,R-VIP 会将 B 踢出,其它不变。

二、安装配置

        环境:
        172.16.1.126:Keepalived + LVS Master + MySQL Semisync-Replication Master 
        172.16.1.127:Keepalived + LVS Backup + MySQL Semisync-Replication Slave
        172.16.1.100:R-VIP
        172.16.1.210:W-VIP

1. 配置 MySQL 半同步复制

1.1 半同步复制的基本概念

        从 5.5 版本开始,MySQL 以插件的形式支持半同步复制。如何理解半同步呢?首先我们来看看 MySQL 中异步、全同步和半同步复制的概念。

  • 异步复制(Asynchronous replication):MySQL 默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返回给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题,主如果 crash 掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。
  • 全同步复制(Fully synchronous replication):指当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。因为需要等待所有从库执行完该事务才能返回,所以全同步复制的性能必然会受到严重的影响。
  • 半同步复制(Semisynchronous replication):介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到 relay log 中才返回给客户端。相对于异步复制,半同步复制提高了数据的安全性,同时它也造成了一定程度的延迟,这个延迟最少是一个 TCP/IP 往返的时间。所以,半同步复制最好在低延时的网络中使用。

        图2 是半同步复制的原理图:

图2

1.2 半同步复制的潜在问题

        假设客户端事务在存储引擎层提交后,在得到从库确认的过程中,主库宕机了。此时,可能存在以下两种情况:

  • 事务还没发送到从库上:此时,客户端会收到事务提交失败的信息,客户端会重新提交该事务到新的主上,当宕机的主库重新启动后,以从库的身份重新加入到该主从结构中,会发现,该事务在从库中被提交了两次,一次是之前作为主的时候,一次是被新主同步过来的。
  • 事务已经发送到从库上:此时,从库已经收到并应用了该事务,但是客户端仍然会收到事务提交失败的信息,重新提交该事务到新的主上。

1.3 半同步复制的安装部署

        1. 先决条件

        要想使用半同步复制,必须满足以下几个条件:

  • MySQL 5.5 及以上版本
  • 变量 have_dynamic_loading 为 YES
  • 异步复制已经存在

        2. 加载插件

        因用户需执行 INSTALL PLUGIN, SET GLOBAL, STOP SLAVE和START SLAVE 操作,所以用户需有 SUPER 权限。在 172.16.1.126 和 172.16.1.127 上执行:

install plugin rpl_semi_sync_master soname 'semisync_master.so';
install plugin rpl_semi_sync_slave soname 'semisync_slave.so';

        这里考虑到以后修复问题后主从可能角色互换,所以在两个 MySQL 主机上都加载了半同步的两个插件。查看插件是否加载成功:

mysql> select plugin_name, plugin_status from information_schema.plugins  where plugin_name like '%semi%';
+----------------------+---------------+
| plugin_name          | plugin_status |
+----------------------+---------------+
| rpl_semi_sync_master | ACTIVE        |
| rpl_semi_sync_slave  | ACTIVE        |
+----------------------+---------------+
2 rows in set (0.00 sec)

        3. 启动半同步复制

        (1)准备配置文件

        172.16.1.126(master)上的 my.cnf 文件内容如下:

[mysqld]
basedir = /home/mysql/mysql-5.6.14
datadir=/data
skip-name-resolve 
user=mysql
binlog_format=row
default-storage-engine=InnoDB
transaction_isolation = READ-COMMITTED
log-bin=/data/mysql-bin
log-bin-index = /data/mysql-bin.index 
tmpdir = /data
server-id = 126
innodb_data_home_dir = /data
innodb_log_group_home_dir=/data

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1

        172.16.1.127(slave)上的 my.cnf 文件内容如下:

[mysqld]
basedir = /home/mysql/mysql-5.6.14
datadir=/data
skip-name-resolve 
user=mysql
binlog_format=row
default-storage-engine=InnoDB
transaction_isolation = READ-COMMITTED
log-bin=/data/mysql-bin
log-bin-index = /data/mysql-bin.index 
tmpdir = /data
server-id = 127
innodb_data_home_dir = /data
innodb_log_group_home_dir=/data

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1
read-only  # 设为只读模式,只能从master同步,不能直接写入

        (2)重启主、从库

service mysql restart

        (3)在主库上授权

grant replication slave on *.* to 'repl'@'%' identified by '123456';
reset master;
show master status;

        (4)在从库上启动复制

change master to
master_host='172.16.1.126',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=120;

start slave;
show slave status \G

        (5)测试

        在主上建立测试表,插入数据。

mysql> use test;
Database changed
mysql> create table t1 (a int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values (1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

        在从上查询。

mysql> select * from test.t1;
+------+
| a    |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

        在从上执行数据更新则报错。

mysql> delete from test.t1;
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement

        至此半同步复制配置完成。

2. 下载安装 LVS

        在 172.16.1.126 和 172.16.1.127 上用 root 用户执行以下命令:

yum -y install ipvsadm

3. 下载安装 Keepalived

        在 172.16.1.126 和 172.16.1.127 上安装 Keepalived,详细步骤参见“使用 Keepalived 实现 MySQL 主从高可用”。

4. iptables 配置

        在本架构中,由于要实现 MySQL 读写主机 VIP 的漂移,Keepalived、LVS 和 MySQL 需要部署到相同主机上。当 LVS 架构中的 director 与 realserver 是同一台机器时,会出现两台 director 无限循环转发请求的情况。具体分析如下:

  1. director1 收到目的地址为 VIP 数据包。
  2. director1 经过 ip_vs 对数据包做负载均衡,部分包转发给 realserver2(director2)。
  3. director2 收到的转发包,目的地址也是 VIP,再经过 ip_vs 做负载均衡,部分包转发给 realserver1(director1)。

        解决办法是,director 使用 iptables,对数据包做标记,LVS 对 fwm 转发而不是直接转发 TCP。在每台 director 上,对非其它 director 发送的数据包,做标记,ip_vs 转发这些被标记的包。亦即其它 director 发送的数据包,不再做 ip_vs 转发,直接交给上层监听程序。

        在 172.16.1.126(主库)上执行:

iptables -t mangle -I PREROUTING -d 172.16.1.100 -p tcp -m tcp --dport 3306 -m mac ! --mac-source 00:50:56:a5:49:7f -j MARK --set-mark 0x1

        在 172.16.1.127(备库)上执行:

iptables -t mangle -I PREROUTING -d 172.16.1.100 -p tcp -m tcp --dport 3306 -m mac ! --mac-source 00:50:56:a5:0f:77 -j MARK --set-mark 0x2

        其中 172.16.1.100 是读 VIP,3306 是 VPORT,00:50:56:a5:49:7f 是 172.16.1.127 主机网卡的 MAC 地址,00:50:56:a5:0f:77 是 172.16.1.126 主机网卡的 MAC 地址。

5. Keepalived 配置

        172.16.1.126 初始为 keepalived 的 Master,其上的 keepalived 配置文件如下:

[root@hdp3~]#more /etc/keepalived/keepalived.conf 
global_defs {
   router_id LVS_DEVEL
} 
 
vrrp_sync_group VG1 {
group {
VI_1
}
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens32 
    virtual_router_id 51
    priority 100  
    notify_master "/home/mysql/remove_slave.sh"
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        172.16.1.100
	    172.16.1.210
    }
}

# 写VIP virtual_server,只配置本地机器
virtual_server 172.16.1.210 3306 {# 定义虚拟服务器,地址与上面的virtual_ipaddress相同
    delay_loop 3		  # 健康检查时间间隔,3秒
    lb_algo rr			  # 负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc
    lb_kind DR			  # 负载均衡转发规则:NAT|DR|TUN
    # persistence_timeout 5	  # 会话保持时间5秒,动态服务建议开启
    protocol TCP		  # 转发协议protocol,一般有tcp和udp两种
    
	real_server 172.16.1.126 3306 {
        weight 1		  # 权重越大负载分越大,0表示失效
        notify_down /home/mysql/mysql_down.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

# 读VIP virtual_server,配置fwmark转发
virtual_server fwmark 1 {	
    delay_loop 3			       
    lb_algo rr				       
    lb_kind DR				       
    # persistence_timeout 5	       
    protocol TCP			      
 
    real_server 172.16.1.126 3306 {
        weight 1			        
	    notify_down /home/mysql/mysql_down.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 172.16.1.127 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
 
[root@hdp1~]#

        R-VIP 绑定 172.16.1.126、172.16.1.127,W-VIP 绑定 172.16.1.126。

        172.16.1.127 初始为 keepalived 的 Backup,其上的 keepalived 配置文件如下:

[root@hdp4~]#more /etc/keepalived/keepalived.conf
global_defs {
   router_id LVS_DEVEL
} 
 
vrrp_sync_group VG1 {
group {
VI_1
}
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens160 
    virtual_router_id 51
    priority 90  
    notify_master "/home/mysql/remove_slave.sh"
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1234
    }
    virtual_ipaddress {
        172.16.1.100
	    172.16.1.210
    }
}

# 写VIP virtual_server,只配置本地机器
virtual_server 172.16.1.210 3306 {# 定义虚拟服务器,地址与上面的virtual_ipaddress相同
    delay_loop 3		  # 健康检查时间间隔,3秒
    lb_algo rr			  # 负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc
    lb_kind DR			  # 负载均衡转发规则:NAT|DR|TUN
    # persistence_timeout 5	  # 会话保持时间5秒,动态服务建议开启
    protocol TCP		  # 转发协议protocol,一般有tcp和udp两种
    
	real_server 172.16.1.127 3306 {
        weight 1		  # 权重越大负载分越大,0表示失效
	    notify_down /home/mysql/mysql_down.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

# 读VIP virtual_server,配置fwmark转发
virtual_server fwmark 2 {	
    delay_loop 3			       
    lb_algo rr				       
    lb_kind DR				       
    # persistence_timeout 5	       
    protocol TCP			      
 
    real_server 172.16.1.126 3306 {
        weight 1			        
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 172.16.1.127 3306 {
        weight 1
        notify_down /home/mysql/mysql_down.sh
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

[root@hdp4~]#

        Master 与 Backup 的 keepalived 配置文件中有以下四点不同:

  • priority 设置不同,Master 为 100,Slave 为 90。
  • 写 VIP 172.16.1.210 绑定的是本机地址。
  • /home/mysql/mysql_down.sh 脚本在本机地址下定义。
  • 根据 iptables 的配置,Master 的 fwmark 标记为 1,Backup 的 fwmark 标记为 2。

        配置文件中各段及其参数的配置说明参见“Keepalived + LVS + MySQL 双主复制实现读写负载均衡及高可用”。

        /home/mysql/mysql_down.sh 文件内容如下:

#!/bin/bash
/etc/init.d/keepalived stop

        当 LVS 检测到主库宕机,执行 mysql_down.sh 脚本,停止主库上的 keepalived 服务,从而使得 VIP 漂移到从库。

        /home/mysql/remove_slave.sh 文件内容如下:

#!/bin/bash
. /home/mysql/.bashrc

user=root
password=123456
log=/home/mysql/remove_slave.log

echo "`date`" >> $log
mysql -u$user -p$password -e "set global read_only=OFF;stop slave;reset master;reset slave all;" >> $log
/bin/sed -i 's#read-only#\#read-only#' /home/mysql/mysql-5.6.14/my.cnf

        当主库宕机,172.16.1.127 切换成为 Master 时执行 remove_slave.sh 脚本,停止复制,重置 MySQL 的 master、slave 状态,关闭 read_only,将原来的主从复制中的 slave 变为 master。

5. 编写 RealServer 的网络配置脚本

        在 172.16.1.126 和 172.16.1.127 上建立 /etc/init.d/realserver 文件,内容如下:

#!/bin/sh
RVIP=172.16.1.100
WVIP=172.16.1.210
. /etc/rc.d/init.d/functions
 
case "$1" in
# 禁用本地的ARP请求、绑定本地回环地址
start)
    /sbin/ifconfig lo down
    /sbin/ifconfig lo up
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
    /sbin/sysctl -p >/dev/null 2>&1
	
    # 在回环地址上绑定VIP,设定掩码,与Direct Server上自身的IP保持通信
    /sbin/ifconfig lo:0 $RVIP netmask 255.255.255.255 up 
    /sbin/ifconfig lo:1 $WVIP netmask 255.255.255.255 up 
	
    /sbin/route add -host $RVIP dev lo:0
    /sbin/route add -host $WVIP dev lo:1
	
    echo "LVS-DR real server starts successfully.\n"
    ;;
stop)
    /sbin/ifconfig lo:0 down
    /sbin/ifconfig lo:1 down
    /sbin/route del $RVIP >/dev/null 2>&1
    /sbin/route del $WVIP >/dev/null 2>&1
	
    echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
    echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
    echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "LVS-DR real server stopped.\n"
    ;;
status)
    isLoOn=`/sbin/ifconfig lo:0 | grep "$RVIP"`
    isRoOn=`/bin/netstat -rn | grep "$RVIP"`
    if [ "$isLoON" == "" -a "$isRoOn" == "" ]; then
        echo "LVS-DR real server has run yet."
    else
        echo "LVS-DR real server is running."
    fi
    exit 3
    ;;
*)
    echo "Usage: $0 {start|stop|status}"
    exit 1
esac
exit 0

        执行下面的命令将该脚本加入开机自启动:

chmod +x /etc/init.d/realserver
echo "/etc/init.d/realserver" >> /etc/rc.d/rc.local

6. 启动 RealServer 和 Keepalived

        在 172.16.1.126 和 172.16.1.127 上执行:

service realserver start
/etc/init.d/keepalived start

        命令执行后 172.16.1.126 和 172.16.1.127 上的 IP 地址分别如图3、4 所示。

图3

图4

        可以看到 172.16.1.126 和 172.16.1.127 两个主机的回环地址都绑定了读写两个 VIP,而只有 172.16.1.126 的本地 IP 绑定了读写两个 VIP。此时从 172.16.1.126 查看 LVS 集群状态如下:

[root@hdp3~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.1.210:3306 rr
  -> 172.16.1.126:3306            Route   1      0          0         
FWM  1 rr
  -> 172.16.1.126:3306            Route   1      0          0         
  -> 172.16.1.127:3306            Route   1      0          0         
[root@hdp3~]#

三、测试

        1. 验证通过 172.16.1.100 连接的读负载均衡转发策略。

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

        2. 验证通过 172.16.1.210 连接的读负载均衡转发策略。

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

        3. 模拟从库的 mysqld crash,在 172.16.1.127 上执行以下命令。

pkill -9 mysqld

        4. 再次使用两个 VIP 连接,数据库服务正常。

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

        此时从 172.16.1.127 查看 LVS 集群状态如下,可以看到读取地址只有 172.16.1.126。

[root@hdp4~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.1.210:3306 rr
  -> 172.16.1.126:3306            Route   1      0          0         
FWM  2 rr
  -> 172.16.1.126:3306            Route   1      0          0         
[root@hdp4~]#

        5. 重新启动从库的 mysql 服务,在 172.16.1.127 上执行以下命令。

service mysql start

        6. 再次验证通过 172.16.1.100 连接的读负载均衡转发策略。

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 126   |
+---------------+-------+

        此时从 172.16.1.127 查看 LVS 集群状态如下,可以看到读取地址 172.16.1.127 被自动加回。

[root@hdp4~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.1.210:3306 rr
  -> 172.16.1.126:3306            Route   1      0          0         
FWM  2 rr
  -> 172.16.1.126:3306            Route   1      0          0         
  -> 172.16.1.127:3306            Route   1      0          0     

        7. 模拟主库的 mysqld crash,在 172.16.1.126 上执行以下命令。

pkill -9 mysqld

        8. 再次验证通过 VIP 连接的读负载均衡转发策略。

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.100 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210 -e "show variables like 'server_id'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 127   |
+---------------+-------+

        此时查看 172.16.1.127 上绑定的 IP 如图5 所示。

图5

        可以看到读写两个 VIP 已经漂移到 172.16.1.127 上。

        9. 验证切换后的新主库可读写。

C:\WINDOWS\system32>mysql -utest -p123456 -h172.16.1.210
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2104
Server version: 5.6.14-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from test.t1;
+------+
| a    |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.00 sec)

mysql> delete from test.t1;
Query OK, 3 rows affected (10.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql>

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值