Keepalived+Mysql双主

一、安装keepalived

#安装keepalived源码安装的依赖包

yum -y install openssl openssl-devel
yum -y install popt-devel



#源码编译安装keepalived

tar xf keepalived-1.2.7.tar.gz 
cd keepalived-1.2.7
./configure --prefix=/usr/local/keepalived
make && make install


#拷贝keepavlied的一些文件到相关目录中

mkdir /etc/keepalived
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/
cp/usr/local/keepalived/etc/keepalived/keepalived.conf    /etc/keepalived/


二、修改Keepalived配置文件:

 (1) Server1:

 

  vi /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived
global_defs {
 router_id mysqlmha1
}
vrrp_script check_run {
 script "/etc/keepalived/check_mysql.sh"  # Mysql监测脚本路径
 interval 5
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   track_script {
 check_run
   }
   virtual_ipaddress {
        192.168.235.16 dev eth0
    }
}
virtual_server 192.168.235.16 3306 {
    delay_loop 6   #每个6秒检查一次real_server状态
    lb_algo rr   #LVS算法,用不到,我们就关闭了
   lb_kind DR  #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
    persistence_timeout 20 #同一IP的连接60秒内被分配到同一台真实服务器
    protocol TCP
    real_server 192.168.235.149 3306 {
    weight 3
    TCP_CHECK {
 connect_timeout 3  #连接超时
  nb_get_retry 3   #重试次数
 delay_before_retry 3  #间隔时间
 connect_port 3306
       }
}

(2)Server 2:

 

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
 router_id mysqlmha1
}
vrrp_script check_run {
 script "/etc/keepalived/check_mysql.sh"
 interval 10
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   track_script {
 check_run
 }
    virtual_ipaddress {

        192.168.235.16 dev eth0

    }
}
virtual_server 192.168.235.16 3306 {
    delay_loop 6
    #lb_algo rr
    #lb_kind DR
    persistence_timeout 20
    protocol TCP
    real_server 192.168.235.134 3306 {
        weight 3
  TCP_CHECK {
  connect_timeout 3
   nb_get_retry 3
  delay_before_retry 3
  connect_port 3306
        }
    }
(3)创建mysql监测脚本
  vi /etc/keepalived/check_mysql.sh
 #!/bin/bash
source /etc/profile
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=127.0.0.1

MYSQL_USER=root #用户名
MYSQL_PASSWORD=redhat #密码
CHECK_TIME=3

#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0

MYSQL_OK=1
function check_mysql_helth (){
    $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
    if [ $? = 0 ] ;then
    MYSQL_OK=1
    else
    MYSQL_OK=0
    fi
    return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"

    check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    exit 0
fi

if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
then
    /etc/init.d/keepalived stop
    exit 1
fi
sleep 1
done
chmod 755 /etc/keepalived/killkeepalived.sh            #加执行权限
chkconfig keepalived on                        #设置开机自动启动

三、测试

 Server1:

  

[root@test ~]# ip addr show
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:72:21:e8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.235.149/24 brd 192.168.235.255 scope global eth0
    inet 192.168.235.16/32 scope global eth0
    inet6 fe80::20c:29ff:fe72:21e8/64 scope link 
       valid_lft forever preferred_lft forever
       
  关闭server1的mysql,在server2上查看ip是否漂移
  
  server1: service mysql stop
  server2: ip addr show


你可能感兴趣的:(local,双主)