keepalived 实现mysql主从自动切换

来源:互联网 发布:时时彩源码程序出售 编辑:程序博客网 时间:2024/04/25 06:19
3.1安装KEEPALIVED软件:wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gztar zxvf keepalived-1.2.7.tar.gz cd keepalived-1.2.7./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-x86_64  3.2设置KEEPALIVED开机自启动:cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/  chkconfig keepalived on  3.3编写主从库上的配置文件:mkdir /etc/keepalivedvi /etc/keepalived/keepalived.conf数据库角色配置文件内容192.168.137.212 VIP 192.168.137.3  master192.168.137.4  slave主! Configuration File for keepalivedglobal_defs {   router_id MySQL-ha}#global_defs {#notification_email {#xxxx@126.com}#当主、备份设备发生改变时,通过邮件通知#notification_email_from lzyangel@126.com#smtp_server stmp.126.com#smtp_connect_timeout 30#router_id MySQL-ha#}vrrp_instance VI_1{# 在初始化状态下定义为主设备state BACKUP# 注意网卡接口interface eth0virtual_router_id 51# 优先级,另一台改为90priority 100advert_int 1# 不主动抢占资源nopreemptauthentication {# 认证方式,可以是PASS或AH两种认证方式auth_type PASS# 认证密码auth_pass 1111}virtual_ipaddress {# 虚拟IP地址,随着state的变化而增加删除192.168.137.212}}virtual_server 192.168.137.212 3306 {# 每个2秒检查一次real_server状态delay_loop 2# LVS算法lb_algo wrr# LVS模式lb_kind DR# 会话保持时间persistence_timeout 60protocol TCPreal_server 192.168.137.3 3306 {# 权重weight 3# 检测到服务down后执行的脚本notify_down /etc/keepalived/keepalived.shTCP_CHECK {# 连接超时时间connect_timeout 10# 重连次数nb_get_retry 3# 重连间隔时间delay_before_retry 3# 健康检查端口connect_port 3306}}} 从! Configuration File for keepalivedglobal_defs {   router_id MySQL-ha}vrrp_instance VI_1{# 在初始化状态下定义为主设备state BACKUP# 注意网卡接口interface eth0virtual_router_id 51# 优先级,另一台改为90priority 90advert_int 1# 不主动抢占资源nopreemptauthentication {# 认证方式,可以是PASS或AH两种认证方式auth_type PASS# 认证密码auth_pass 1111}virtual_ipaddress {# 虚拟IP地址,随着state的变化而增加删除192.168.137.212}notify_master /etc/keepalived/modifyreadonly.sh}virtual_server 192.168.137.212 3306 {# 每个2秒检查一次real_server状态delay_loop 2# LVS算法lb_algo wrr# LVS模式lb_kind DR# 会话保持时间persistence_timeout 60protocol TCPreal_server 192.168.137.4 3306 {# 权重weight 3# 检测到服务down后执行的脚本notify_down /etc/keepalived/keepalived.shTCP_CHECK {# 连接超时时间connect_timeout 10# 重连次数nb_get_retry 3# 重连间隔时间delay_before_retry 3# 健康检查端口connect_port 3306}}}/etc/keepalived/keepalived.sh 内容:/get_ssh.exp 192.168.137.4 xxxx "/apps/svr/mysql5.6/bin/mysql -uroot -pxxxxxxx -S /apps/dbdat/mysql_3306data/mysql_3306.sock -e 'set global read_only=0;'"/etc/keepalived/modifyreadonly.sh 内容:/usr/local/mysql/bin/mysql -uroot -pxxxxx -S /data/mysqldata/3306/mysql.sock -e 'set global read_only=0;'192.168.137.212 192.168.137.3 主192.168.137.4 从  3.4配置检测主库DOWN后执行的脚本:脚本一#!/bin/sh/etc/init.d/keepalived stop #在停止KEEPALIVED后远程将第二节点改为可写/get_ssh.exp 192.168.137.4 xxxx "/apps/svr/mysql5.6/bin/mysql -uroot -pxxxxxxx -S /apps/dbdat/mysql_3306data/mysql_3306.sock -e 'set global read_only=0;'"脚本二get_ssh.exp 的内容如下: 要先安装软件:expect#!/usr/bin/expect -fset timeout -1if { [llength $argv] < 3} {  puts "usage: $argv0 ip pass cmd"    exit 1    }set ip [lindex $argv 0 ]set password [lindex $argv 1 ]set cmd [lindex $argv 2 ]spawn ssh $ip $cmdexpect {   "*yes/no" { send "yes\r"; exp_continue}   "*password:" { send "$password\r" }   }   expect eof脚本三当从库的状态变为MASTER后所要执行的脚本:#!/bin/bash/usr/local/mysql/bin/mysql -uroot -pxxxxx -S /data/mysqldata/3306/mysql.sock -e 'set global read_only=0;'

0 0