当前位置:Gxlcms > 数据库问题 > MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移

MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移

时间:2021-07-01 10:21:17 帮助过:46人阅读

192.168.1.152 CentOS 5.6 mysql 5.6.22 mysql从库 192.168.1.153 CentOS 5.6 mysql 5.6.22 VIP 192.168.1.150

 


mysql 主主同步都设置 auto-increment-offset,auto-increment-increment ,以避免自增列冲突。
参考:MySQL 高可用:主主复制(双主复制)

 

 

LVS 下载:
http://www.linuxvirtualserver.org/software/
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz


Keepalived 下载:
http://www.keepalived.org/software/

http://www.keepalived.org/software/keepalived-1.2.19.tar.gz

 

 

yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel  
cmake libnl* libpopt* popt-static openssl-devel   

 



 

# LVS 安装配置:(yum -y install ipvsadm)

 

cd /usr/local/src  
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz  
tar zxvf ipvsadm-1.24.tar.gz  
cd ipvsadm-1.24  
ln -s /usr/src/kernels/2.6.18-402.el5-i686/ /usr/src/linux  #找自己的目录看看/usr/src/kernels  
make && make install 

 


# Keepalived 安装配置

 

 

cd /usr/local/src  
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz  
tar zxvf keepalived-1.2.19.tar.gz  
cd keepalived-1.2.19  
./configure --prefix=/usr/local/keepalived --disable-fwmark  
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/  
  
mkdir -p /etc/keepalived/  
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 

 


#编辑或者新建(主备除两地方,其他一样.备库改为 :state BACKUP ,priority 50)

 

vi /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived  
  
global_defs {               #全局标识模块  
   notification_email {     #定义邮件通知  
     test@163.com           #目标邮箱  
   }  
   notification_email_from test@163.com #发送邮箱  
   smtp_server 127.0.0.1    #发送邮箱的smtp服务器  
   smtp_connect_timeout 30  #smtp服务器连接超时时间  
   router_id LVS_DEVEL  
}  
  
vrrp_instance VI_1 {  
    state MASTER            #本实例启动状态:MASTER/BACKUP  
    interface eth0          #监控的网络接口  
    virtual_router_id 51    #vrrp实例(同一个组主备服务器设置一样)  
    priority 100            #优先级高的为master,不能超过255。(BACKUP可设置为50)  
    advert_int 1            #均衡器检测间隔1秒(服务器设置都一样)  
    authentication {        #验证类型及密码(服务器设置都一样)  
        auth_type PASS      #认证方式,PASS或AH  
        auth_pass 123456    #认证密码  
    }  
    virtual_ipaddress {     #虚拟ip地址virtual_ipaddress,可以定义多个  
        192.168.1.150  
    }  
}  
  
virtual_server 192.168.1.150 3306 { #定义虚拟服务器,与上面的virtual_server一样  
    delay_loop 6            #健康检查时间间隔,6秒  
    lb_algo rr              #负载均衡调度算法:rr|wrr|lc|wlc|sh|dh|lblc  
    lb_kind NAT             #负载均衡转发规则:NAT|DR|TUN  
    nat_mask 255.255.255.0  
    persistence_timeout 50  #回话保持时间50秒,动态服务建议开启  
    protocol TCP            #转发协议protocol,一般有tcp和udp两种  
  
    #后端真实服务器,有几台就设置几个  
    real_server 192.168.1.152 3306 {  
        weight 1            #权重越大负载分越大,0表示失效  
        SSL_GET {           #健康检查方式:HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }  
    }  
    real_server 192.168.1.153 3306 {  
        weight 1  
        SSL_GET {  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }  
    }  
}  

 

#启动 keepalived 服务  
/etc/rc.d/init.d/keepalived start  
  
#添加到rc.local自启动中  
echo "/etc/rc.d/init.d/keepalived start" >> /etc/rc.local

 

 

#152服务器IP  
[root@centos152 keepalived]# ip a  
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  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000  
    link/ether 00:0c:29:3b:c4:a5 brd ff:ff:ff:ff:ff:ff  
    inet 192.168.1.152/24 brd 192.168.1.255 scope global eth0  
    inet 192.168.1.150/32 scope global eth0  

 

#现在在第三台mysql服务器中连接VIP测试  
  
#ping VIP 正常  
ping 192.168.1.150  
  
#连接到 VIP 正常  
mysql -umysqlproxy -pmysqlproxy -h192.168.1.150 --port3306  
  
#到 mysql 服务器 152153 查看进程,查看用户 mysqlproxy 连接到哪台服务器  
mysql> show processlist;  
  
#停止 mysqlproxy 所在的服务器keepalived 服务  
/etc/rc.d/init.d/keepalived stop  
  
此时在150 连接的mysql切换到另一台服务器的mysql数据库了。  
  
  
/etc/rc.d/init.d/keepalived start 

 


末尾有些测试还没有规范,仅供参考。

 

 

更多参考:

MySQL 高可用:主主复制(双主复制)

ipvsadm 安装配置

mysql高可用探究(二)Lvs+Keepalived+Mysql单点写入主主同步高可用方案

Lvs+keepalived+mysql 双主

keepalived配置主从备份

Linux下Keepalived 安装与配置

CentOS 5.9下安装配置keepalived

keepalived.conf内容说明

MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移

标签:init.d   aio   software   服务器   amp   mask   自增   wget   多个   

人气教程排行