学习笔记5:mysql高可用(主从复制监控)

来源:互联网 发布:死而后已不亦远乎翻译 编辑:程序博客网 时间:2024/06/05 20:49
mysql复制拓扑
    mysql5.7前:一个从库只能有一个主库
    mysql5.7后:支持一从多主
    
    【一主多从的复制拓扑】:
            【优点】:    配置简单
                        可以多个从库分担读的负载
            【用途】:    为不同的业务使用不同的从库
                        将一个从库放到远程IDC中,用作灾备恢复
                        可以分担主库读负载
                        
    【主-主复制拓扑】:
            【主备模式的主主复制拓扑】:只有一台主服务器对外提供服务,其他的处于只读状态并且只做热备使用
                                        在对外提供服务的主数据库出现故障或计划性维护时才会进行切换
                                        使原来的备库成为主库,主库成为新的备库,并处理只读或是下线状态,待维护
                                        完成后重新上线
                                        1.确保初始化数据相同
                                        2.确保启动bin_log日志,server_id 不相同。
                                        3.启用 log_slave_updates参数
                                        4.初始化备库上启用read_only
                                        
            【主主模式的主主复制拓扑】:互为主从 , 两个主都对外提供服务
                                        产生数据冲突,而造成复制链路的中断,耗费大量时间,造成数据丢失
                                        1.两个主中所操作的表最好分开
                                        2.使用下面的两个参数,保证生成自增ID的生成
                                            auto_increment_incremenet = 2
                                            auto_increment_offset = 1|2
            
            【拥有备库的主主复制拓扑】:
            
            【级联复制】:
                    主db------------》分发主库
                                        |
                                        |
                        -------------------------            
                        |        |        |        |
                        从        从        从        从
                分发主库也是个从库,负责记录主库传递的二进制日志,并把二进制日志提供给从库
                避免了主库因为复制产生的额外的负载
                从库非常多的情况下,可以建立多个分发主库
                【分发主库】:一定要启动 slave_log_updates
                
                
                
    【影响主从延迟的因素】:
            1.主库写入二进制日志的时间:
                    控制主库事务的大小,分隔大事务
                    
            2.二进制日志的传输时间:
                    传输日志量的多少-》使用mixed(混合格式)  或row格式。
                    设置set binlog_row_image = minimal;
                    
            3.默认情况下,从库只有一个sql线程,主上的并发修改,在从库上成了串行处理。
                使用从库(mysql5.6版本后引入)多线程复制
                在mysql5.7中可以按照逻辑时钟的方式来分配sql线程
                【如何配置多线程复制】:在完成的主从环境中增加步骤
                        show variables like 'slave_parallel_type';
                        show variables like 'slave_parallel_workers';
                        1.stop    slave    (停止链路复制)
                        2.set global slave_parallel_type = 'logical_clock';    (如何使用多线程复制)
                        3.set global slave_parallel_workers = 4;        (设置线程数量,并发处理的线程数)
                        4.start slave    (开始链路复制)
    
                
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
mysql复制常见问题处理
    由数据损坏或丢失所引起的主从复制错误
    【1.主库或从库意外宕机引起的错误】:
            使用跳过二进制日志事件
            注入空事务的方式恢复中断的复制链路
            在使用其他方法来对比主从服务器上的数据
            
    【2.主库的二进制日志文件损坏】:
            通过change master命令来重新指定
            在使用其他方法来对比主从服务器上的数据
    
    【3.备库的中继日志损坏】:
            
    【4.在从库上进行数据修改造成主从复制错误】:
    
    【5.不唯一的server_id 或server_uuid】:
    
    【6.max_allow_packet设置引起主从复制错误】:
    

    
mysql复制无法解决的问题
    【分担主数据库的写负载】:
    【自动进行故障转移及主从切换】:
    【提供读写分离】:
    


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

什么是高可用:通过尽量缩短因日常维护和突发的系统崩溃所导致的停机时间,以提高系统和应用的可用性。
    全年不可用时间:(365*24*60)*(1-0.99999) = 5.256
    
如何实现高可用:
    【1.避免导致系统不可用的因素,减少系统不可用的时间】:
            服务器磁盘耗尽,性能糟糕的sql,表结构和索引没有优化,主从数据不一致,人为操作失误
            【建立完善的监控报警系统】
            【对备份的数据进行恢复测试】
            【正确配置数据库】
            【对不需要的数据归档,清理】
    【2.增加系统的冗余,保证发生系统不可用时可以尽快恢复】:
            【避免存在单点故障】:在一个系统中,提供相同功能的组件只有一个,如果这个组件失效,就会影响整个系统的使用。
                                    组成系统应用的各个组件都可能成为单点。
                                    【避免mysql单点】:利用SUN共享存储或DRDB磁盘复制解决
                                                【SUN】:    两台服务器能够正常的挂载相同的文件系统,但只有一台服务器能对其进行操作。
                                                        如果当前处理的服务器宕机了,那么备用的服务器可以继续的挂载文件系统,执行需要的恢复操作。
                                                        并在失效的服务器上的数据启用mysql服务
                                                        
                                                【磁盘镜像】:linux内核模块,通过网卡将主服务器的每一个块复制到另外一个服务器的块设备上,并且在主设备提交块之前,
                                                            就会被记录下来,当主设备失效时,可以把备用设备提升为主设备,从而实现故障转移
                                                            
                                                【利用多写集群或NDB集群来解决】:percona(公司)-》pxc(集群)        

                                                【利用主从服务器】:主服务器切换后,如何通知应用新的的主服务器ip地址
                                                                    如何检查mysql主服务器是否可用
                                                                    如何处理从服务器和新主服务器间的那种复制关系
                                                                    【MMM】:
                                                                        【优点】:    开源
                                                                                    使服务器的变更更加透明
                                                                                    从服务器的延迟监控
                                                                                    主服务器故障转移后从服务器对新主服务器的重新同步功能
                                                                                    很容易对发生故障的主数据库同步重新上线
                                                                        【缺点】:    发布时间较早,对gtip的复制不支持,仅支持日志点的复制,对5.6之后的多线程复制也不支持
                                                                                    不能对多个从服务器读负载均衡的功能
                                                                                    主从切换过程容易造成数据丢失,事务丢失
                                                                                    监控服务存在单点故障
                                                                                    
                                                                    【MMM】:监控和管理mysql的主主复制拓扑,并在当前的主服务器失效时,进行主和主备之间的主从切换
                                                                            和故障转移等工作
                                                                            【提供的功能】:mysql主从复制健康情况
                                                                                            在主库出现宕机时进行故障转移并自动配置其他从对新主的复制
                                                                                            
                                                                                            提供了主,写虚拟ip,在主服务器出现问题的时候可以自动迁移到虚拟ip
                                                                                    如何找到从库对应新的主库日志点的日志同步点
                                                                                    如果出现多个从库出现数据不一致的情况如何处理
                                                                    【MMM架构】    :管理主主复制        
                                                                    【MMM部署所需资源】:
                                                                            名称资源            数量                说明
                                                                            主DB服务器            2                    用于主备模式的主主复制配置
                                                                            从DB服务器            0-N                    0-多台从服务器
                                                                            监控服务器            1                    用于监控mysql复制集群
                                                                            IP地址                2*(n+1)            n位mysql服务器的数量
                                                                            监控用户            1                    用于监控mysql状态的mysql用户(replication client)
                                                                            代理用户            1                    用户MMM代理的mysql用户(super,replication client,process)
                                                                            复制用户            1                    用户配置mysql复制的mysql用户(replication slave)
                                                                                    
                                                                    【MMM部署步骤】
                                                                        【1.配置主主复制及主从同步集群】
                                                                        
                                                                        【2.安装主从节点所需要的安装包】
                                                                        
                                                                        【3.安装及配置MMM工具集】
                                                                        
                                                                        【4.运行MMM监控服务】
                                                                        
                                                                        【5.测试】
                                                                        
                                                                        两个主,一个从
                                                                    【1.建立主从服务账号】:
                                                                                grant replication slave on *.* to 'slave_user2'@'%' identified by '112358';
                                                                    【2.备份数据,初始化主服务器2,从服务器数据库信息】:
                                                                                主数据库下备份,发送sql文件
                                                                                mysqldump --master-data=2 --triggers  --routines  --single-transaction  --all-databases -uroot -p>> all3.sql            
                                                                                scp all3.sql root@192.168.164.137:/root
                                                                                scp all3.sql root@192.168.164.136:/root
                                                                                
                                                                                -------------------------------------------------------------------
                                                                                [root@localhost etc]# scp all3.sql root@192.168.164.137:/root
                                                                                root@192.168.164.137's password:
                                                                                all3.sql                        100%  866KB 866.3KB/s   00:00    
                                                                                [root@localhost etc]# scp all3.sql root@192.168.164.136:/root
                                                                                root@192.168.1.210's password:
                                                                                all3.sql                          100%  866KB 866.3KB/s   00:00
                                                                                -------------------------------------------------------------------
                                                                                cd /root
                                                                                mysql -u root -p < all3.sql
                                                                                ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
                                                                                已经设置过主从了,所以报错
                                                                                进入mysql下执行:reset master
                                                                                再次执行:mysql -u root -p < all3.sql
                                                                                OK
                                                                                -------------------------------------------------------------------
                                                                                
                                                                                
                                                                    (主1:135和主2:136互为主从)        
                                                                    主服务器:192.168.164.135    
                                                                    配置
                                                                    
                                                                    主2服务器:192.168.164.136
                                                                    配置:
                                                                    
                                                                    
                                                                    从服务器:192.168.164.137
                                                                    
                                                                    安装MMM:
                                                                        yum search mmm
                                                                        ===================================================================== N/S Matched: mmm ======================================================================
                                                                        mysql-mmm-agent.noarch : MMM Database Server Agent Daemon and Libraries
                                                                        mysql-mmm-monitor.noarch : MMM Monitor Server Daemon and Libraries
                                                                        mysql-mmm-tools.noarch : MMM Control Scripts and Libraries
                                                                        mysql-mmm.noarch : Multi-Master Replication Manager for MySQL

                                                                        yum install mysql-mmm-agent.noarch -y     【代理包:需要检测的每个DB环境都要安装】
                                                                        yum install mysql-mmm*                    【监控包:监控服务器上安装】
                                                                        
                                                                    配置MMM:
                                                                        在主服务器上创建账号
                                                                        监控用户            1                    用于监控mysql状态的mysql用户(replication client)
                                                                            grant replication client on *.* to 'jiankong'@'%' identified by '112358';
                                                                        
                                                                        代理用户            1                    用户MMM代理的mysql用户(super,replication client,process)
                                                                            改变read_only模式,过量转移和主从切换
                                                                            grant super,replication client,process on *.* to 'daili'@'%' identified by '112358';
                                                                        复制用户            1                    用户配置mysql复制的mysql用户(replication slave)
                                                                            集群中使用的,配置主从时已经有了,不需要重新创建
                                                                            grant replication slave on *.* to 'test123'@'%' identified by '112358';
                                                                        
                                                                        select user,host from mysql.user;
                                                                        
                                                                        flush privileges;
                                                                        
                                                                        ---------------------------------------------------------------------------
                                                                        /etc/mysql-mmm/
                                                                        
                                                                        
                                                                        ================监控服务器修改配置===============================
                                                                        mmm_mon.conf
                                                                            include mmm_common.conf

                                                                            <monitor>
                                                                                ip                  127.0.0.1
                                                                                pid_path            /var/run/mysql-mmm/mmm_mond.pid
                                                                                bin_path            /usr/libexec/mysql-mmm
                                                                                status_path         /var/lib/mysql-mmm/mmm_mond.status
                                                                                ping_ips            192.168.164.135,192.168.164.136,192.168.164.137
                                                                                auto_set_online     60

                                                                                # The kill_host_bin does not exist by default, though the monitor will
                                                                                # throw a warning about it missing.  See the section 5.10 "Kill Host
                                                                                # Functionality" in the PDF documentation.
                                                                                #
                                                                                # kill_host_bin     /usr/libexec/mysql-mmm/monitor/kill_host
                                                                                #
                                                                            </monitor>

                                                                            <host default>
                                                                                monitor_user        jiankong
                                                                                monitor_password    112358
                                                                            </host>

                                                                            debug 0

                                                                        
                                                                        ===============================================
                                                                        mmm_agent.conf  
                                                                        
                                                                        this db1

                                                                        ===============================================
                                                                        mmm_common.conf
                                                                            <host default>
                                                                                cluster_interface       eth0
                                                                                pid_path                /var/run/mysql-mmm/mmm_agentd.pid
                                                                                bin_path                /usr/libexec/mysql-mmm/
                                                                                replication_user        test123        #主从账户
                                                                                replication_password    112358        #主从密码
                                                                                agent_user              daili        #代理账户
                                                                                agent_password          112358        #代理密码
                                                                            </host>

                                                                            <host db1>
                                                                                ip      192.168.164.135        #主服务器ip
                                                                                mode    master
                                                                                peer    db2
                                                                            </host>

                                                                            <host db2>
                                                                                ip      192.168.164.136        #主2服务器ip
                                                                                mode    master
                                                                                peer    db1
                                                                            </host>

                                                                            <host db3>
                                                                                ip      192.168.164.137        #从服务器ip
                                                                                mode    slave
                                                                            </host>

                                                                            <role writer>                    #有写权限的ip,虚拟vip
                                                                                hosts   db1, db2
                                                                                ips     192.168.164.35
                                                                                mode    exclusive
                                                                            </role>

                                                                            <role reader>                    #有读权限的ip,虚拟vip
                                                                                hosts   db1,db2,db3
                                                                                ips     192.168.164.35, 192.168.164.36,192,168.164.37
                                                                                mode    balanced
                                                                            </role>

                                                                        ===============================================
                                                                        ifconfig eth0:1 192.168.164.35 netmask 255.255.255.0 up
                                                                        ifconfig eth0:1 192.168.164.36 netmask 255.255.255.0 up
                                                                        ifconfig eth0:1 192.168.164.37 netmask 255.255.255.0 up
                                                                                                              
                                                                        将配置信息发送到其他的服务器下
                                                                        scp mmm_common.conf root@192.168.164.136:/etc/mysql-mmm
                                                                        scp mmm_common.conf root@192.168.164.137:/etc/mysql-mmm
                                                                        
                                                                        --------------------------------------------------------
                                                                        将所有节点的代理先启动135,136,137
                                                                        /etc/init.d/mysql-mmm-agent     start        #启动代理
                                                                        
                                                                        Starting MMM Agent Daemon:    [  OK  ]
                                                                        
                                                                        
                                                                        所有节点的代理服务都启动了,可以在监控节点启动监控
                                                                        /etc/init.d/mysql-mmm-monitor    start     #启动监控
                                                                        Starting MMM Monitor Daemon:   [  OK  ]

                                                                        在监控节点查看命令
                                                                        mmm_control show
                                                                        db1(192.168.164.135) master/HARD_OFFLINE. Roles:
                                                                        db2(192.168.164.136) master/HARD_OFFLINE. Roles:
                                                                        db3(192.168.164.137) slave/HARD_OFFLINE. Roles:
                                                                        
                                                                =====================================================================================================
                                                                =====================================================================================================                                                                
                                                                    
                                                                    
                                                                    
                                                                    【MHA(mysql master high availability)】    :perl脚本开发,高效完成主从切换(30S内完成)
                                                                    【MHA功能】:    监控主数据库服务器是否可用
                                                                                    当主DB不可用时,从多个从服务器中选举出新的主数据库服务器
                                                                                    提供主从切换和故障转移
                                                                    【MHA进行主从切换过程】:
                                                                                    1.尝试从故障主数据库保存二进制日志
                                                                                    2.从多个备选从服务器中选举出新的备选主服务器(在多个从服务器中,找到和原主最接近的从库选为主库)
                                                                                        可以退认为设置一些服务器不参加选举
                                                                                    3.备选主服务器和其他从服务器之间同步差异二进制日志
                                                                                    4.应用从原主服务器保存二进制日志
                                                                                    5.提升备选主db服务器为新的主db服务器
                                                                                    6.迁移集群中的其他从服务器作为新的主db服务器的从服务器
                                                                    【MHA架构】一主多从
                                                                    【MHA配置步骤】:
                                                                                    1.配置集群内所有主机的ssh免认证登陆(比如故障转移过程中保存原主的二进制日志,配置虚拟ip地址等)
                                                                                    2.安装MHA-node软件包(集群中所有机器)和MHA-manager软件包(监控服务器)
                                                                                    3.(支持包)    yum -y install perl-Config-Tiny.noarch perl-Time-HiRes.x86_64
                                                                                        perl-Parallel-ForkManager perl-Log-Dispatch-Perl.noarch
                                                                                        perl-DBD-MySQL ncftp
                                                                                    4.建立主从复制集群(推荐GTID)
                                                                                    5.配置MHA管理节点
                                                                                    6.使用masterha_check_ssh(检测ssh免认证配置)和
                                                                                        mastersh_check_repl (检测集群中复制链路是否正确)对配置进行校验
                                                                                    7.启动和测试MHA服务
                                                                        
                                                                            【1.配置虚拟ip】:
                                                                                    ifconfig eth0:1 192.168.164.35 netmask 255.255.255.0 up
                                                                                    ip addr
                                                                                
                                                                                        
                                                                                    重启网络
                                                                                     CentOS / RHEL / Red Hat / Fedora
                                                                                    # service network stop
                                                                                    # service network start
                                                                                    # service network restart
                                                                                    
                                                                                    Ubuntu/Debian
                                                                                    # service networking stop
                                                                                    # service networking start
                                                                                    # service networking restart
                                                                                    
                                                                            【2.配置一主多从】:基于gtid
                                                                            【3.配置MHA】:
                                                                                    建立ssh免认证登陆
                                                                                    【3-1】:ssh-keygen
                                                                                    [root@localhost /]# ssh-keygen
                                                                                    Generating public/private rsa key pair.
                                                                                    Enter file in which to save the key (/root/.ssh/id_rsa):
                                                                                    Enter passphrase (empty for no passphrase):
                                                                                    Enter same passphrase again:
                                                                                    Your identification has been saved in /root/.ssh/id_rsa.
                                                                                    Your public key has been saved in /root/.ssh/id_rsa.pub.
                                                                                    The key fingerprint is:
                                                                                    7e:86:d5:91:dd:d1:6e:b9:c3:c1:de:cb:d2:34:55:5f root@localhost.localdomain
                                                                                    The key's randomart image is:
                                                                                    +--[ RSA 2048]----+
                                                                                    |               ..|
                                                                                    |             o .E|
                                                                                    |            o o.*|
                                                                                    |           . . +=|
                                                                                    |        S . . o.=|
                                                                                    |       . o     B.|
                                                                                    |        o o   + +|
                                                                                    |         o   . + |
                                                                                    |              .  |
                                                                                    +-----------------+
                                                                                    
                                                                                    【3-2】:本机也要配置,因为每台机器都可以成为主,避免节点切换产生问题
                                                                                    ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.135'
                                                                                    ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.136'
                                                                                    ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.137'
                                                                                    
                                                                                    root@localhost /]# ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.135'
                                                                                    The authenticity of host '192.168.164.135 (192.168.164.135)' can't be established.
                                                                                    RSA key fingerprint is 0c:fc:15:98:b4:01:fe:c8:59:2c:b1:7b:af:63:44:6e.
                                                                                    Are you sure you want to continue connecting (yes/no)? yes
                                                                                    Warning: Permanently added '192.168.164.135' (RSA) to the list of known hosts.
                                                                                    root@192.168.164.135's password:
                                                                                    Now try logging into the machine, with "ssh '-p 22 root@192.168.164.135'", and check in:

                                                                                      .ssh/authorized_keys

                                                                                    to make sure we haven't added extra keys that you weren't expecting.

                                                                                    
                                                                                    【3-3】:验证一下ssh是否可用
                                                                                    ssh root@192.168.164.135
                                                                                    [root@localhost /]# ssh root@192.168.164.135
                                                                                    [root@localhost ~]# exit
                                                                                    logout
                                                                                    Connection to 192.168.164.135 closed.
                                                                                    检测可用,登陆无需密码
                                                                                    
                                                                                    【3-4】:主从都需配置,3-1是在135中配置的ssh
                                                                                    在136,137中分别创建ssh,各配置一遍
                                                                                    ssh-keygen
                                                                                    ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.135'
                                                                                    ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.136'
                                                                                    ssh-copy-id -i /root/.ssh/id_rsa '-p 22 root@192.168.164.137'    
                                                                                    
                                                                            【4.配置MHA软件包】:软件包下载地址(http://download.csdn.net/download/qq_34605594/9940547)
                                                                                    安装依赖包:
                                                                                    yum install perl-DBD-MySQL ncftp perl-DBI.x86 perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
                                                                                    
                                                                                    Monitor节点需要安装manager和node两个包,数据库(主、从)节点只安装node包即可
                                                                                    
                                                                                    将node包发送到其他的从服务器上:
                                                                                    scp mha4mysql-node-0.57-0.el7.noarch.rpm root@192.168.164.136:/root
                                                                                    scp mha4mysql-node-0.57-0.el7.noarch.rpm root@192.168.164.137:/root
                                                                                    
                                                                                    各个节点上安装node包:
                                                                                    rpm -ivh mha4mysql-node-0.57-0.el7.noarch.rpm
                                                                                    
                                                                                    136环境下,除了作为从数据库服务器,也充当监控服务器,所以要安装manager软件包
                                                                                    scp mha4mysql-manager-0.57-0.el7.noarch.rpm root@192.168.164.136:/root
                                                                                    
                                                                                    yum install perl-DBD-MySQL ncftp perl-DBI.x86 perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
                                                                                    
                                                                                    yum install -y perl-DBD-MySQL

                                                                                    yum install -y perl-Config-Tiny

                                                                                    yum install -y perl-Log-Dispatch

                                                                                    yum install -y perl-Parallel-ForkManager

                                                                                    yum install -y perl-Config-IniFiles
                                                                                    
                                                                                    rpm -ivh mha4mysql-manager-0.57-0.el7.noarch.rpm
                                                                                    
                                                                            【5.MHA内manager配置】:
                                                                                    
                                                                                    【5-1创建manager配置文件】:在监控服务器下创建
                                                                                            mkdir -p /etc/mha            #创建配置文件目录
                                                                                            mkdir -p /home/mysq_mha        #创建下载的二进制日志保存目录
                                                                                            cd     /etc/mha
                                                                                            vim mysql_mha.cnf            #创建配置文件
                                                                                    
                                                                                            ------------------------------------------------------
                                                                                            [server default]
                                                                                            #数据库中建立,用户mha主从管理的数据库用户
                                                                                            #grant all privileges on *.* to 'mha'@'192.168.164.%' identified by '112358';
                                                                                            
                                                                                            user=mha        

                                                                                            password=112358

                                                                                            manager_workdir=/home/mysq_mha

                                                                                            manager_log=/home/mysq_mha/manager.log

                                                                                            #远程服务器的工作目录,在其他节点也都需要建立这个目录
                                                                                            remote_workdir=/home/mysq_mha
                                                                                            
                                                                                            #ssh登陆用户,因为配置的免密码登陆,所以无需密码
                                                                                            ssh_user=root

                                                                                            #主从复制的用户名
                                                                                            repl_user=test123

                                                                                            #主从复制的密码
                                                                                            repl_password=112358

                                                                                            #manager进程检查数据库是否连通的时间间隔
                                                                                            #每秒检测一次
                                                                                            ping_interval=1

                                                                                            #告诉管理进程获取二进制日志的位置
                                                                                            master_binlog_dir=/data/mysql/
                                                                                            
                                                                                            #指定一个脚本,主从切换之后,可以把主的虚拟ip绑定在新选取的主服务器上
                                                                                            #不提供脚本,无法提供虚拟ip漂移
                                                                                            master_ip_failover_script=/usr/bin/master_ip_failover
                                                                                            
                                                                                            #默认情况加,manager服务器来检测主数据库是否可用,使用脚本,可以通过多个网络检测主数据库是否可用
                                                                                            #安装mha master后,这个脚本默认存在在 /usr/bin/
                                                                                            #提供一些ip地址就可以通过这个ip地址来检测
                                                                                            secondary_check_script= /usr/bin/masterha_secondary_check -s 192.168.164.136 -s 192.168.164.137

                                                                                            

                                                                                            #主机信息
                                                                                            [server1]

                                                                                            #ip地址
                                                                                            hostname=192.168.164.135
                                                                                            
                                                                                            #可以参加选举的服务器ip
                                                                                            candidate_master=1

                                                                                            #主机信息
                                                                                            [server2]

                                                                                            #ip地址
                                                                                            hostname=192.168.164.137
                                                                                            
                                                                                            #可以参加选举的服务器ip
                                                                                            candidate_master=1
                                                                                            
                                                                                            #主机信息,136服务器即是从,又是监控,所以就配置他不允许加入选举
                                                                                            [server3]

                                                                                            #ip地址
                                                                                            hostname=192.168.164.136
                                                                                            
                                                                                            #不允许参加选举
                                                                                            no_master=1

                                                                                    【5-2:secondary_check_script虚拟ip脚本】:
                                                                                    
                                                                                    【5-3检测mha配置】:
                                                                                        【masterha_check_ssh --conf=/etc/mha/mysql_mha.cnf】
                                                                                            
                                                                                        [root@localhost bin]# masterha_check_ssh
                                                                                            --conf=<server_config_file> must be set.
                                                                                            [root@localhost bin]# masterha_check_ssh --conf=/etc/mha/mysql_mha.cnf
                                                                                            Mon Sep 25 17:08:43 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
                                                                                            Mon Sep 25 17:08:43 2017 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
                                                                                            Mon Sep 25 17:08:43 2017 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..
                                                                                            Mon Sep 25 17:08:43 2017 - [info] Starting SSH connection tests..
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]
                                                                                            Mon Sep 25 17:08:58 2017 - [debug]  Connecting via SSH from root@192.168.164.136(192.168.164.136:22) to root@192.168.164.135(192.168.164.135:22)..
                                                                                            Mon Sep 25 17:09:56 2017 - [debug]   ok.
                                                                                            Mon Sep 25 17:09:56 2017 - [debug]  Connecting via SSH from root@192.168.164.136(192.168.164.136:22) to root@192.168.164.137(192.168.164.137:22)..
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]   ok.
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]
                                                                                            Mon Sep 25 17:08:57 2017 - [debug]  Connecting via SSH from root@192.168.164.137(192.168.164.137:22) to root@192.168.164.135(192.168.164.135:22)..
                                                                                            Mon Sep 25 17:09:56 2017 - [debug]   ok.
                                                                                            Mon Sep 25 17:09:56 2017 - [debug]  Connecting via SSH from root@192.168.164.137(192.168.164.137:22) to root@192.168.164.136(192.168.164.136:22)..
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]   ok.
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]
                                                                                            Mon Sep 25 17:08:47 2017 - [debug]  Connecting via SSH from root@192.168.164.135(192.168.164.135:22) to root@192.168.164.137(192.168.164.137:22)..
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]   ok.
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]  Connecting via SSH from root@192.168.164.135(192.168.164.135:22) to root@192.168.164.136(192.168.164.136:22)..
                                                                                            Mon Sep 25 17:09:57 2017 - [debug]   ok.
                                                                                            Mon Sep 25 17:09:57 2017 - [info] All SSH connection tests passed successfully.
                                                                                        
                                                                                        ssh配置没问题
                                                                                        
                                                                                        【masterha_check_repl --conf=/etc/mha/mysql_mha.cnf】
                                                                                        
                                                                                        报错:
                                                                                        install_driver(mysql) failed: Attempt to reload DBD/mysql.pm aborted.
                                                                                        
                                                                                        yum install -y perl-DBD-MySQL 问题。
                                                                                        解决方法:
                                                                                        yum install cpan
                                                                                        cpan DBI
                                                                                        cpan DBD::mysql
                                                                                        
                                                                                        再次执行:
                                                                                        masterha_check_repl --conf=/etc/mha/mysql_mha.cnf    
                                                                                        结果:
                                                                                        [root@localhost ~]# masterha_check_repl --conf=/etc/mha/mysql_mha.cnf
                                                                                        Mon Sep 25 17:40:57 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
                                                                                        Mon Sep 25 17:40:57 2017 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
                                                                                        Mon Sep 25 17:40:57 2017 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..
                                                                                        Mon Sep 25 17:40:57 2017 - [info] MHA::MasterMonitor version 0.57.
                                                                                        Mon Sep 25 17:41:02 2017 - [warning] SQL Thread is stopped(no error) on 192.168.164.135(192.168.164.135:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Multi-master configuration is detected. Current primary(writable) master is 192.168.164.135(192.168.164.135:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Master configurations are as below:
                                                                                        Master 192.168.164.135(192.168.164.135:3306), replicating from 192.168.164.136(192.168.164.136:3306)
                                                                                        Master 192.168.164.136(192.168.164.136:3306), replicating from 192.168.164.135(192.168.164.135:3306), read-only

                                                                                        Mon Sep 25 17:41:02 2017 - [info] GTID failover mode = 1
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Dead Servers:
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Alive Servers:
                                                                                        Mon Sep 25 17:41:02 2017 - [info]   192.168.164.135(192.168.164.135:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info]   192.168.164.137(192.168.164.137:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info]   192.168.164.136(192.168.164.136:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Alive Slaves:
                                                                                        Mon Sep 25 17:41:02 2017 - [info]   192.168.164.137(192.168.164.137:3306)  Version=5.7.18-log (oldest major version between slaves) log-bin:enabled
                                                                                        Mon Sep 25 17:41:02 2017 - [info]     GTID ON
                                                                                        Mon Sep 25 17:41:02 2017 - [info]     Replicating from 192.168.164.135(192.168.164.135:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
                                                                                        Mon Sep 25 17:41:02 2017 - [info]   192.168.164.136(192.168.164.136:3306)  Version=5.7.18-log (oldest major version between slaves) log-bin:enabled
                                                                                        Mon Sep 25 17:41:02 2017 - [info]     GTID ON
                                                                                        Mon Sep 25 17:41:02 2017 - [info]     Replicating from 192.168.164.135(192.168.164.135:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info]     Not candidate for the new Master (no_master is set)
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Current Alive Master: 192.168.164.135(192.168.164.135:3306)
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Checking slave configurations..
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Checking replication filtering settings..
                                                                                        Mon Sep 25 17:41:02 2017 - [info]  binlog_do_db= , binlog_ignore_db=
                                                                                        Mon Sep 25 17:41:02 2017 - [info]  Replication filtering check ok.
                                                                                        Mon Sep 25 17:41:02 2017 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
                                                                                        Mon Sep 25 17:41:02 2017 - [info] Checking SSH publickey authentication settings on the current master..
                                                                                        Mon Sep 25 17:41:03 2017 - [info] HealthCheck: SSH to 192.168.164.135 is reachable.
                                                                                        Mon Sep 25 17:41:03 2017 - [info]
                                                                                        192.168.164.135(192.168.164.135:3306) (current master)
                                                                                         +--192.168.164.137(192.168.164.137:3306)
                                                                                         +--192.168.164.136(192.168.164.136:3306)

                                                                                        Mon Sep 25 17:41:03 2017 - [info] Checking replication health on 192.168.164.137..
                                                                                        Mon Sep 25 17:41:03 2017 - [info]  ok.
                                                                                        Mon Sep 25 17:41:03 2017 - [info] Checking replication health on 192.168.164.136..
                                                                                        Mon Sep 25 17:41:03 2017 - [info]  ok.
                                                                                        Mon Sep 25 17:41:03 2017 - [warning] master_ip_failover_script is not defined.
                                                                                        Mon Sep 25 17:41:03 2017 - [warning] shutdown_script is not defined.
                                                                                        Mon Sep 25 17:41:03 2017 - [info] Got exit code 0 (Not master dead).

                                                                                        MySQL Replication Health is OK.

                                                                                    检测成功
                                                                                    
                                                                                    
                                                                                    【5-4启动MHA】:
                                                                                        nohup命令参考

                                                                                        nohup 命令

                                                                                        用途:不挂断地运行命令。

                                                                                        语法:nohup Command [ Arg … ] [ & ]
                                                                                        
                                                                                        nohup masterha_manager --conf=/etc/mha/mysql_mha.cnf
                                                                                        
                                                                                        ps -ef
                                                                                        
                                                                                        程序执行中
                                                                                        [root@localhost ~]# masterha_manager --conf=/etc/mha/mysql_mha.cnf
                                                                                            Mon Sep 25 17:51:49 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
                                                                                            Mon Sep 25 17:51:49 2017 - [info] Reading application default configuration from /etc/mha/mysql_mha.cnf..
                                                                                            Mon Sep 25 17:51:49 2017 - [info] Reading server configuration from /etc/mha/mysql_mha.cnf..

                                                                                    【5-5测试MHA】:
                                                                                        主服务器:    164.135
                                                                                        从服务器:    164.136
                                                                                        从服务器2:    164.137
                                                                                        监控服务器:164.136
                                                                                        
                                                                                        
                                                                                        主服务器现在是2个ip地址
                                                                                        ifconfig
                                                                                        eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:29:F0  
                                                                                                  inet addr:192.168.164.135  Bcast:192.168.164.255  Mask:255.255.255.0
                                                                                                  inet6 addr: fe80::20c:29ff:fed8:29f0/64 Scope:Link
                                                                                                  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                                                                                                  RX packets:199195 errors:0 dropped:0 overruns:0 frame:0
                                                                                                  TX packets:123285 errors:0 dropped:0 overruns:0 carrier:0
                                                                                                  collisions:0 txqueuelen:1000
                                                                                                  RX bytes:97203266 (92.7 MiB)  TX bytes:13491758 (12.8 MiB)

                                                                                        eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:D8:29:F0  
                                                                                                  inet addr:192.168.164.35  Bcast:192.168.164.255  Mask:255.255.255.0
                                                                                                  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
                                                                                                  
                                                                                        192.168.164.135
                                                                                        
                                                                                        虚拟ip:
                                                                                        192.168.164.35
                                                                                        

                                                                                        停掉主服务的数据库进程
                                                                                        
                                                                                        service mysqld stop
                                                                                        
                                                                                        164.136服务器下,原主服务器是164.135
                                                                                        164.135服务器关闭下,查看主服务器是否正确切换了
                                                                                        
                                                                                        在136服务器下登陆mysql
                                                                                        show slave status \G
                                                                                        *************************** 1. row ***************************
                                                                                                       Slave_IO_State: Waiting for master to send event
                                                                                                          Master_Host: 192.168.164.137
                                                                                                          Master_User: test123
                                                                                                          Master_Port: 3306
                                                                                                        Connect_Retry: 60
                                                                                                      Master_Log_File: slave-bin.000002
                                                                                                  Read_Master_Log_Pos: 485
                                                                                                       Relay_Log_File: slave-relay-bin.000002
                                                                                                        Relay_Log_Pos: 414
                                                                                                Relay_Master_Log_File: slave-bin.000002
                                                                                                     Slave_IO_Running: Yes
                                                                                                    Slave_SQL_Running: Yes
                                                                                                    
                                                                                        主服务器已经切换到了164.137
                                                                                        
                                                                                    MHA正常完成了故障转移以及主从切换过程
                                                                                    
                                                                            【MHA工具的优缺点】:
                                                                                【优点】:    perl语言开发的开源软件
                                                                                            支持GTID的复制
                                                                                            主从切换,故障转移过程中,不容易产生数据丢失
                                                                                            同一节点开源监控多个集群
                                                                                
                                                                                【缺点】:    需要编写脚本或利用第三方工具来实现vip配置
                                                                                            MHA启动后只会对主数据库进行监控
                                                                                            需要基于ssh免认证登陆,有一定安全性
                                                                                            没有实现读的负载均衡
                                                                                            






               
原创粉丝点击