Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6330163
  • 博文数量: 162
  • 博客积分: 3600
  • 博客等级: 中校
  • 技术积分: 10366
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-19 11:23
个人简介

专注系统运维、网络架构,研究技术解决方案,记录我的思想轨迹、工作学习、生活和关注的领域

文章分类
文章存档

2014年(2)

2013年(16)

2012年(27)

2011年(117)

分类: Mysql/postgreSQL

2013-01-25 16:48:22

承上一篇文章:“MySQL之Master-Master配置 ”已经实现了MySQL双主模式,即互相备份。接下来我们用Keepavlied来实现单点故障切换。

一、配置虚拟IP-VIP

首先我们先配置VIP节点IP地址,把每台物理地址执行到虚拟IP

这里我们使用的虚拟IP-VIP为: 192.168.15.181

#vim /etc/sysconfig/network-scripts/ifcfg-eth0:0

DEVICE=eth0:0

IPADDR=192.168.15.181

# ip addr
1: lo: 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:d8:b3:ca brd ff:ff:ff:ff:ff:ff
    inet 192.168.15.178/24 brd 192.168.15.255 scope global eth0
    inet 192.168.15.181/32 scope global eth0
    inet 192.168.15.181/24 brd 192.168.15.255 scope global secondary eth0:0
    inet6 fe80::20c:29ff:fed8:b3ca/64 scope link
       valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
    link/sit 0.0.0.0 brd 0.0.0.0

二、安装配置keepalived

官网下载地址:

1)安装:

#wget 

#tar -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-128.el5-x86_64/   #kernel目录根据实际修改。

预编译后出现:

Keepalived configuration

------------------------

Keepalived version       : 1.2.7

Compiler                 : gcc

Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd

Extra Lib                : -lpopt -lssl -lcrypto 

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

IPVS use libnl           : No

Use VRRP Framework       : Yes

Use VRRP VMAC            : No

SNMP support             : No

Use Debug flags          : No

注意如果你没有用到LVS可以把lvs去掉即 --disable-lvs-syncd --disable-lvs

#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/

建立配置文件目录(注意:keepalived的配置文件默认在/etc/keepalived/目录)

#mkdir /etc/keepalived

两台服务器(两个节点)都这样安装即可

2配置

关于配置文件的说明,可以详见这里:


节点A192.168.15.178配置如下:
#vim /etc/keepalived/keepalived.conf

global_defs {

    notification_email

        {

        roc@example.com

        }

    notification_email_from roc@example.com

    smtp_server 127.0.0.1

    stmp_connect_timeout 30

    router_id MySQL-ha

    }

vrrp_script chk_mysql {

        script "killall -0 mysqld"            #检测mysqld进程,也可以替换成检测脚本

        interval 5

        }

vrrp_instance VI_1 {  

     state master   #从库配置此处均是slave  

     interface eth0  

     virtual_router_id 51  

     priority 100   #优先级,另一台改为90  

     advert_int 1  

     nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置  

     authentication {  

        auth_type PASS  

        auth_pass 1111  

        }  

     virtual_ipaddress {  

             192.168.15.181

        }  

     track_script {

        chk_mysql        #执行上面的命令

        }

     }

virtual_server 192.168.15.181 3306 {

    delay_loop 6

    lb_algo wcl

    lb_kind DR

    #nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

    real_server 192.168.15.178 3306 {

        weight 1

        TCP_CHECK {

          connect_timeout 10       #(10秒无响应超时)

          nb_get_retry 3

          delay_before_retry 3

          connect_port 3306

        }

    }

    real_server 10.8.10.185 3306 {

        weight 1

        TCP_CHECK {

            connect_timeout 10       #(10秒无响应超时)

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}

节点192.168.15.185配置如下:

#vim /etc/keepalived/keepalived.conf

global_defs {

    notification_email

        {

        roc@example.com

        }

    notification_email_from roc@example.com

    smtp_server 127.0.0.1

    stmp_connect_timeout 30

    router_id MySQL-ha

    }

vrrp_script chk_mysql {

        script "killall -0 mysqld"            #检测mysqld进程,也可以替换成检测脚本

        interval 5

        }

vrrp_instance VI_1 {  

     state slave   #主库配置此处均是master  

     interface eth0  

     virtual_router_id 51  

     priority 90

     advert_int 1  

     authentication {  

        auth_type PASS  

        auth_pass 1111  

        }  

     virtual_ipaddress {  

             192.168.15.181

        }  

     track_script {

        chk_mysql        #执行上面的命令

        }

     } 

virtual_server 192.168.15.181 3306 {

    delay_loop 6

    lb_algo wcl

    lb_kind DR

    #nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

    real_server 192.168.15.178 3306 {

        weight 1

        TCP_CHECK {

          connect_timeout 10       #(10秒无响应超时)

          nb_get_retry 3

          delay_before_retry 3

          connect_port 3306

        }

    }

    real_server 10.8.10.185 3306 {

        weight 1

        TCP_CHECK {

            connect_timeout 10       #(10秒无响应超时)

            nb_get_retry 3

            delay_before_retry 3

            connect_port 3306

        }

    }

}


3开启服务

#/usr/local/keepalived/sbin/keepalived -D 

#ps -aux | grep keepalived

root     15416  0.0  0.0  61148   736 pts/1    S+   16:43   0:00 grep keepalived
root     31121  0.0  0.0  35832   492 ?        Ss   Jan22   0:00 keepalived -D
root     31122  0.0  0.0  37924  1372 ?        S    Jan22   0:00 keepalived -D
root     31125  0.0  0.0  37924  1000 ?        S    Jan22   0:03 keepalived -D

三、测试

MySQL远程登录测试

客户端登录VIP,看是否能登录,在登录之两台MySQL服务器都要授权允许从远程登录

mysql>  grant all privileges on phpcmsv9.* to 'dbuser_do'@'192.168.10.6' identified by '123456';            

No connection. Trying to reconnect...

Connection id:    1168

Current database: phpcmsv9

Query OK, 0 rows affected (0.11 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

客户端登录结果:


此时查询的是192.168.15.185的数据库信息,关闭192.168.15.185 Mysql,继续联系数据库。


此时连接的是192.168.15.178的数据库。

Keepavlied完成后,接下来要测试的就是如果让master服务器和slave 也能够实现自动切换,同步保证数据同步正常。

阅读(7631) | 评论(0) | 转发(5) |
给主人留下些什么吧!~~