mysql基于keepalived的双主热备(HA)

时间:2022-09-15 07:29:59

应用场景:

     网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,*部门网站系统,医院实时报医系统,*局在线报案系统,股市后台网站系统等等,他们的压力不是很大,但是对可靠性要求是非常高的.

特性:

  通过vip飘移,实现故障切换。只能支持2个节点,不支持更多节点管理;容易发生脑裂.

优点:

     数据同步非常简单,不像负载均衡对数据一致性要求非常高,实现起来相对复杂维护也颇为不便,双机热备用rsync就可以实现了操作和维护非常简单.

缺点:

      脑裂无法解决,需要引入第三方仲裁节点,有一个节点空闲.

前提:

    存在互为主从的mysql实例(A:192.168.255.134 B:192.168.255.135)

A实例:192.168.255.134
1.首先安装keepalived
    a.源码安装
    b.yum安装(yum install keepalived -y)
2.安装MySQL-python(检测mysql是否启动是基于python的)
    yum install MySQL-python
3.修改keepalived.conf(/etc/keepalived/keepalived.conf)
    vrrp_script vs_mysql_134 {
        script "/home/keepalived/checkMySQL.py -h 192.168.255.134 -P 3306"   //监控脚本
        interval 60  // 监控时间
    }
    vrrp_instance VI_134 {
        state BACKUP
        nopreempt
        interface eth0  //绑定虚拟IP的网络接口
        virtual_router_id 134  //VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
        priority 100
        advert_int 5
        authentication {
            auth_type PASS  // 加密
            auth_pass 1111  // 加密的密码,两台服务器一定要一样,不然会出错
        }
        track_script {
           vs_mysql_134  // 执行监控的服务
        }
        virtual_ipaddress {
            192.168.255.150 //VIP 地址
        }
    }
4.创建/home/keepalived/checkMySQL.py文件,内容如下:
    #!/usr/bin/python
    #coding: utf-8
    #

    import sys
    import os
    import getopt
    import MySQLdb
    import logging



    dbhost='localhost'
    dbport=3306
    dbuser='monitor'
    dbpassword='m0n1tor'


    def checkMySQL():
        global dbhost
        global dbport
        global dbuser
        global dbpassword

        shortargs='h:P:'
        opts, args=getopt.getopt(sys.argv[1:],shortargs)
        for opt, value in opts:
            if opt=='-h':
                dbhost=value
            elif opt=='-P':
                dbport=value
        #print "host : %s, port: %d, user: %s, password: %s" % (dbhost, int(dbport), dbuser, dbpassword)
        db = instanceMySQL(dbhost, dbport, dbuser, dbpassword)
        st = db.ishaveMySQL()
        #if ( db.connect() != 0 ):
        #   return 1
        #db.disconnect()
        return st

    class instanceMySQL:
        conn = None
        def __init__(self, host=None,port=None, user=None, passwd=None):
            self.dbhost= host
            self.dbport = int(port)
            self.dbuser = user
            self.dbpassword = passwd

        def ishaveMySQL(self):
            cmd="ps -ef | egrep -i \"mysqld\" | egrep -iv \"mysqld_safe\" | grep -v grep | wc -l"
            mysqldNum = os.popen(cmd).read()
            cmd ="netstat -tunlp | grep \":%s\" | wc -l" % self.dbport
            mysqlPortNum= os.popen(cmd).read()
            #print mysqldNum, mysqlPortNum
            if ( int(mysqldNum) <= 0):
                print "error"
                return 1
            if ( int(mysqldNum) > 0 and  mysqlPortNum <= 0):
                return 1
            return 0

        def connect(self):
        #   print "in db conn"
        #  print "host : %s, port: %d, user: %s, password: %s" % (self.dbhost, self.dbport, self.dbuser, self.dbpassword)
            try:
                self.conn=MySQLdb.connect(host="%s"%self.dbhost, port=self.dbport,user="%s"%dbuser, passwd="%s"%self.dbpassword)
            except Exception, e:
        #       print " Error"
                print e
                return 1
            return 0
        def disconnect(self):
            if (self.conn):
                self.conn.close()
                self.conn = None


    if __name__== "__main__":
        st=checkMySQL()
        sys.exit(st)

5. 将步骤4中创建的文件赋予可自行的权限
    chmod 755 /home/keepalived/checkMySQL.py
6.在mysql创建一个监控用户:
    GRANT REPLICATION CLIENT ON *.* TO 'monitor'@'%' IDENTIFIED BY 'm0n1tor';
7.启动keepalived,提示如下(tail -f /var/log/messages):
    mysql基于keepalived的双主热备(HA)

  则表示成功。

B实例:192.168.255.135
1.同实例A
2.同实例A
3.修改keepalived.conf(/etc/keepalived/keepalived.conf)
    vrrp_script vs_mysql_135 {
        script "/home/keepalived/checkMySQL.py -h 192.168.255.135 -P 3306"
        interval 60
    }
    vrrp_instance VI_135 {
        state BACKUP
        nopreempt
        interface eth0
        virtual_router_id 134
        priority 100
        advert_int 5
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        track_script {
           vs_mysql_135
        }
        virtual_ipaddress {
            192.168.255.150
        }
    }
4.同实例A
5.同实例A
6.同实例A
7.启动keepalived,提示如下(tail -f /var/log/messages):
    mysql基于keepalived的双主热备(HA)
   则表示成功