1:准备工作:
CentOS release 6.7 (Final)
master(slave)-192.168.0.171
slave(master)-192.168.0.170
VIP=192.168.0.200
2:开始配置mysql主从配置:
参考:  http://linuxgentoo.blog.51cto.com/7678232/1556647
我是用的这个账号:
  mysql> grant all on *.* to 'ab'@'192.168.0.%' identified by '123';
  mysql>  change master to master_host='192.168.0.170',master_port=3306,master_user='ab',master_password='123',master_log_file='mysql-bin.000012',master_log_pos=1192;
你们需咬根据需求来设置权限

3:安装配置keepalived
A:配置192.168.0.171
    [root@test3 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
  state BACKUP     #两台配置此处均是BACKUP
  interface eth0
  virtual_router_id 51
  priority 100      #优先级,另一台改为90
  advert_int 1
  nopreempt          #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
      192.168.0.200
  }
}

virtual_server 192.168.0.200 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  persistence_timeout 50        #会话保持时间
  protocol TCP

real_server 192.168.0.171 3306 {
      weight 3
      notify_down /usr/local/keepalived/mysql.sh
      TCP_CHECK {
      connect_timeout 10        #连接超时时间
      nb_get_retry 3            #重连次数
      delay_before_retry 3      #重连间隔时间
      connect_port 3306        #健康检查端口
        }

      }

}

配置:192.168.0.170
  [root@mysql ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
  state BACKUP   #两台配置此处均是BACKUP
  interface eth0
  virtual_router_id 51
  priority 90      #优先级,另一台改为90
  advert_int 1
  nopreempt          #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
      192.168.0.200
  }
}

virtual_server 192.168.0.200 3306 {
  delay_loop 6
  lb_algo wrr
  lb_kind DR
  persistence_timeout 50        #会话保持时间
  protocol TCP

real_server 192.168.0.170 3306 {
      weight 3
      notify_down /usr/local/keepalived/mysql.sh
      TCP_CHECK {
      connect_timeout 10        #连接超时时间
      nb_get_retry 3            #重连次数
      delay_before_retry 3      #重连间隔时间
      connect_port 3306        #健康检查端口
        }

      }

}

配置完成之后,定义脚本
mkdir  /usr/local/keepalived
[root@mysql ~]# cat /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
chmod +x  /usr/local/keepalived/mysql.sh


线上最好用这个

#!/bin/bash


# 环境变量

PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH


# 暂停5秒执行,防止数据库人工正常重启

sleep 5


# mysql_id(存活=1 死掉=0)

mysql_id=`ps -C mysqld --noheader |wc -l`


# 判断mysql_id若死掉,则重启mysql一次,若仍然无法启动mysql则杀掉keepaliaved进程实现VIP切换

if [ $mysql_id -eq 0 ];then

/etc/init.d/mysqld restart

sleep 5

if [ $mysql_id -eq 0 ];then

/etc/init.d/keepalived stop

fi

fi


这样就可以启动mysql和keepalived来测试了
我们使用Navicat 来连接测试
 
keepalived高可用mysql(主从)——HA_mysql

发现数据是一致的

我们关闭主mysql(192.168.0.171)来看看是不是可以切换到从mysql(192.168.0.170)



参考:http://database.51cto.com/art/201012/237204.htm






配置keepalived高可用mysql主从
  192.168.0.171  mysql-master
  192.168.0.170  mysql-master
  192.168.0.171的keepalived的配置
    [root@test3 ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived


global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.0.171
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 100
    advert_int 2
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 123456
    }


    virtual_ipaddress {
        192.168.0.192
    }
}
virtual_server 192.168.0.192 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR
     persistence_timeout 60
     protocol TCp
     real_server 192.168.0.171 3306 {
         weigit 3
         notify_down /data/sh/mysql.sh
          TCP_CHECK {
     connect_timeout 10
           nb_get_retry 3
     delay_before_retry 3
     connect_port 3306
}
}
~
vim /data/sh/mysql.sh(配置)
  [root@test3 ~]# vim /data/sh/mysql.sh 
#!/bin/sh  
pkill keepalived
#chmod +x /data/sh/mysql.sh








192.168.0.170的keepalived的配置
   [root@mysql sh]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived


global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
     root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.0.171
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 90
    advert_int 2
    nopreempt 
    authentication {
        auth_type PASS
        auth_pass 123456
    }


    virtual_ipaddress {
        192.168.0.192/24
    }
}
virtual_server 192.168.0.192 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR
     persistence_timeout 60
     protocol TCp
     real_server 192.168.0.170 3306 {
         weigit 3
         notify_down /data/sh/mysql.sh
          TCP_CHECK {  
     connect_timeout 10
           nb_get_retry 3    
     delay_before_retry 3   
     connect_port 3306
}
}
[root@mysql sh]# cat /data/sh/mysql.sh 
#!/bin/sh  
pkill keepalived  
#chmod +x /data/sh/mysql.sh