mysql主从双机keepalived配置

1.关闭selinux

SELINUX=disabled

2.yum安装keepalived

yum install -y keepalived

3.修改配置文件

主库配置文件:

! Configuration File for keepalived
global_defs {
    notification_email {
        lr@isadba.com
    }
    notification_email_from lvs@isadba.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id mysql01
    ! lvs_sync_daemon eth0 VI_1 1
}
vrrp_instance VI_1 {
    state master
    priority 150
    nopreempt
    virtual_router_id 241
    interface eno16777728
    advert_int 1
    ! smtp_alert
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.12.4
    }
}
virtual_server 192.168.12.4 3306 {
    delay_loop 6
    lb_algo wlc
    lb_kind TUN
    protocol TCP
    real_server 192.168.12.60 3306 {
        weight 1
        notify_down /mysql/killkeepalived.sh
        TCP_CHECK {
            connect_port    3306
            connect_timeout 3
        }
    }
    real_server 192.168.12.2 3306 {
        weight 1
        TCP_CHECK {
            connect_port    3306
            connect_timeout 3
        }
    }
}
 
从库配置文件:
! Configuration File for keepalived
global_defs {
    notification_email {
        lr@isadba.com
    }
    notification_email_from lvs@isadba.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id mysql02
    ! lvs_sync_daemon eth0 VI_1 1
}
vrrp_instance VI_1 {
    state backup
    priority 100
    nopreempt
    virtual_router_id 241
    interface eno16777728
    advert_int 1
    ! smtp_alert
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.12.4
    }
}
virtual_server 192.168.12.4 3306 {
    delay_loop 6
    lb_algo wlc
    lb_kind TUN
    protocol TCP
    real_server 192.168.12.60 3306 {
        weight 1
        TCP_CHECK {
            connect_port    3306
            connect_timeout 3
        }
    }
    real_server 192.168.12.2 3306 {
        weight 1
        TCP_CHECK {
            connect_port    3306
            connect_timeout 3
        }
    }
}
 
主库脚本/mysql/killkeepalived.sh
#!/bin/sh
systemctl stop keepalived
 
4.主从切换过程
  1.主库停库
  2.keepalived执行关闭keepalived脚本
  3.恢复主库,启动主库keepalived,关闭备库keepalived,vip即可回到主库。
 
5.扩展
 如果使用keepalived+mycat搭建主从+多个只读从库,将vip写入mycat配置schema.xml文件,业务使用vip连接数据库。
 
 
上一篇:MYSQL主从库同步配置过程


下一篇:mysql主从备份功能配置与測试