溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql MHA 高可用架構(gòu)部署

發(fā)布時間:2020-08-17 08:12:27 來源:ITPUB博客 閱讀:211 作者:urgel_babay 欄目:MySQL數(shù)據(jù)庫

MHA簡介:即Master High Availability Manager and Tools for MySQL,是日本的一位MySQL專家采用Perl語言編寫的一個腳本管理工具,

該工具僅適用于MySQL Replication(二層)環(huán)境,目的在于維持Master主庫的高可用性。

 在 MySQL 故障切換過程中,MHA 能做到 0~30 秒之內(nèi)自動完成數(shù)據(jù)庫的故障

切換操作,并且在進行故障切換的過程中,MHA 能最大程度上保證數(shù)據(jù)庫的一致性,以達到

真正意義上的高可用。

 MHA 由兩部分組成:MHA Manager(管理節(jié)點)和 MHA Node(數(shù)據(jù)節(jié)點)。MHA Manager

可以獨立部署在一臺獨立的機器上管理多個Master-Slave集群,也可以部署在一臺Slave上。

當(dāng) Master 出現(xiàn)故障是,它可以自動將最新數(shù)據(jù)的 Slave 提升為新的 Master,然后將所有其他

的 Slave 重新指向新的 Master。整個故障轉(zhuǎn)移過程對應(yīng)用程序是完全透明的。

整體環(huán)境介紹:
master1 :192.168.9.25
master2  :  192.168.9.26
slave1     :   192.168.9.29
slave2     :    192.168.9.30 
manager 節(jié)點: 192.168.9.27
lvs1:   192.168.9.27
lvs2 :  192.168.9.28
整體搭建流程思路:所有的mysql服務(wù)器上都要安裝node軟件,在manager管理服務(wù)器(9.27)上安裝manager和node節(jié)點。然后所有的mysql數(shù)據(jù)里要創(chuàng)建mha管理用戶,這里我起名字叫  admin
mysql MHA 高可用架構(gòu)部署

下面演示具體搭建過程:
一:9.27管理節(jié)點部署,安裝node和manager軟件,只要安裝上既可以了,不用開啟
首先上傳node和manager軟件安裝鏡像(我硬盤里有)

[root@lvs-a ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm   (6.5系統(tǒng))

[root@lvs-a ~]# #rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm    (5.8)


[root@lvs-a ~]# ls /etc/yum.repos.d/

base.repo epel.repo epel-testing.repo rhel-debuginfo.repo

[root@lvs-a ~]# yum -y install perl-DBD-MySQL   ncftp

[root@lvs-a  tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C  /usr/local      ##  node軟件解壓   

[root@lvs-a  tarbag]# cd /usr/local/mha4mysql-node-0.56         

[root@lvs-a  tarbag]# perl Makefile.PL

[root@lvs-a  tarbag]# make && make install        ## node安裝

[root@lvs-a  tarbag]# yum -y install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager

[root@lvs-a  tarbag]# tar -zxf mha4mysql-manager-0.56.tar.gz -C /usr/local/

[root@lvs-a  tarbag]# cd /usr/local/mha4mysql-manager-0.56    ##manager軟件安裝

[root@lvs-a  mha4mysql-manager-0.56]# perl Makefile.PL

[root@lvs-a  mha4mysql-manager-0.56]# make && make install

二:9.25主節(jié)點部署,只安裝node即可

[root@master1~]#rpm-ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[root@master1 ~]# ls /etc/yum.repos.d/
base.repo  epel.repo  epel-testing.repo  rhel-debuginfo.repo
[root@master1 ~]# yum -y install perl-DBD-MySQL  ncftp
[root@master1 tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C /usr/local
[root@master1 tarbag]# cd  /usr/local/mha4mysql-node-0.56
[root@master1 mha4mysql-node-0.56]#  perl Makefile.PL
[root@master1 mha4mysql-node-0.56]#  make && make install 
[root@master1 mha4mysql-node-0.56]# ln -s /usr/local/mysql/bin/*     /usr/local/bin/    

安裝mysql軟件  具體過程略過,參考:   MySQL 主從同步復(fù)制架構(gòu)搭建   文檔

創(chuàng)建mha管理賬號
Mysql> grant all on *.* to 'admin'@'%' identified by '123456';
Mysql> flush privileges;
創(chuàng)建mysql主從管理賬號
Mysql> grant replication  slave on *.* to 'repl'@'%'   identified by '123456';
Mysql> flush privileges; 


三、9.26主備節(jié)點部署(步驟同主庫一樣)

[root@master2~]#rpm-ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[root@master2 ~]# ls /etc/yum.repos.d/
base.repo  epel.repo  epel-testing.repo  rhel-debuginfo.repo
[root@master2 ~]# yum -y install perl-DBD-MySQL  ncftp
[root@master2 tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz  -C /usr/local
[root@master2 tarbag]# cd  /usr/local/mha4mysql-node-0.56
[root@master2 mha4mysql-node-0.56]#  perl Makefile.PL
[root@master2 mha4mysql-node-0.56]#  make && make install 
[root@master2 mha4mysql-node-0.56]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
創(chuàng)建mha管理賬號 其實可以不建立的,因為開始之前你要做讓master1去恢復(fù)別的mysql,只要master1上有即可。
Mysql> grant all on *.* to 'admin'@'%' identified by '123456';
Mysql> flush privileges;
創(chuàng)建mysql主從管理賬號
Mysql> grant replication slave on *.* to 'repl'@'%' identified by '123456';
Mysql> flush privileges;

四:9.29 從節(jié)點部署

[root@slave1~]#rpm-ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[root@slave1 ~]# ls /etc/yum.repos.d/
base.repo  epel.repo  epel-testing.repo  rhel-debuginfo.repo
[root@slave1 ~]# yum -y install perl-DBD-MySQL  ncftp
[root@slave1 tarbag]# tar -zxf mha4mysql-node-0.56..tar.gz -C /usr/local
[root@slave1 tarbag]# cd  /usr/local/mha4mysql-node-0.56
[root@slave1 mha4mysql-node-0.56]#  perl Makefile.PL
[root@slave1 mha4mysql-node-0.56]#  make && make install 
[root@slave1 mha4mysql-node-0.56]# ln -s /usr/local/mysql/bin/*   /usr/local/bin/
創(chuàng)建mha管理賬號
Mysql> grant all on *.* to 'admin'@'%' identified by '123456';
Mysql> flush privileges;

五:9.30從庫操作參考9.29.


六:至此需要安裝的軟件,已經(jīng)基本完成,下面具體展示如何配置manager管理節(jié)點,mha上層可以通過keepalive部署VIP,程序連接數(shù)據(jù)庫使用VIP,從而實現(xiàn)后臺數(shù)據(jù)庫的故障切換透明化。

[root@lvs-a ~]#  mkdir   -p  /etc/masterha

[root@lvs-a ~]#  mkdir -p /masterha/app1

[root@lvs-a mha4mysql-manager-0.56]#   cp  samples/conf/*  /etc/masterha/            

[root@lvs-a ~]#  cat   /etc/masterha/app1.cnf
[server default]  
manager_workdir=/masterha/app1  
manager_log=/masterha/app1/manager.log  
user=admin 
password=123456 
ssh_user=root 
repl_user=repl
repl_password=123456 
ping_interval=1      #每秒ping一次
shutdown_script="" 
master_ip_failover_script="/usr/local/bin/master_ip_failover"  ?。#_@個腳本接下來會創(chuàng)建         
master_ip_online_change_script="" 
report_script="" 
[server1]  
hostname=192.168.9.26
master_binlog_dir=/mysql/data/log
candidate_master=1              ##主庫要有這個參數(shù)
[server2]  
hostname=192.168.9.25
master_binlog_dir=/mysql/data/log
candidate_master=1
[server3]
hostname=192.168.9.29
master_binlog_dir=/data/log
[server4]
hostname=192.168.9.30
master_binlog_dir=/data/log/                                 #對應(yīng)路徑

[root@lvs-a u01]# cat /usr/local/bin/master_ip_failover  
#!/bin/bash
                #--------------第一部分:變量及函數(shù)定義-----------------#
        ###Begin Variables define###
        ssh_port=22
        cmd=/sbin/ifconfig
        vip=192.168.9.232
        device=eth0:0
        netmk=255.255.255.0
        start_vip="${cmd} ${device} ${vip} netmask ${netmk} up"
        stop_vip="${cmd} ${device} ${vip} netmask ${netmk} down"
        ###End Variables define###

        ###Begin Status Funciont###
        status()
        {
        exit 0
        }
        ###End Status Funciont###

        ###Begin Stop Or Stopssh Funciont###
        stop()
        {
        exit 0
        }
        ###End Stop Or Stopssh Funciont###

        ###Begin Start Funciont###
        start()
        {
        /usr/bin/ssh -p ${ssh_port} ${ssh_user}@${orig_master_host} ""${stop_vip}"" 
        /usr/bin/ssh -p ${ssh_port} ${ssh_user}@${new_master_host} ""${start_vip}""
        exit 0
        }
        ###End Start Funciont###
                #--------------第一部分:變量及函數(shù)定義-----------------#

                #--------------第二部分:命令行參數(shù)-----------------#
        ###Begin Get The Command-Line Parameters###
###eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,orig_master_port:,new_master_host:,new_master_ip:,new_master_port:,new_master_user:,new_master_password: -- "$@"`"
        eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,new_master_host:,new_master_ip: -- "$@"`"

        if [ $? != 0 ] ; then echo "Terminating..." >&2 ;exit 1;fi

        while true
        do
                case "$1" in
                        --command)
                                command="${2}";
                                shift;;
                        --ssh_user)
                                ssh_user="${2}";
                                shift;;
                        --orig_master_host)
                                orig_master_host="${2}";
                                shift;;
                        --orig_master_ip)
                                orig_master_ip="${2}";
                                shift;;
                        --new_master_host)
                                new_master_host="${2}";
                                shift;;
                        --new_master_ip)
                                new_master_ip="${2}";
                                shift;;
                        --)
                                shift;
                                break;;
                esac
        shift
        done
        ###End Get The Command-Line Parameters###
                #--------------第二部分:命令行參數(shù)-----------------#

                #--------------第三部分:函數(shù)調(diào)用-----------------#
        if [ "${command}" == "status" ];
        then
        status;
        fi
        if [ "${command}" == "stop" ] || [ "${command}" == "stopssh" ] ;
        then
        stop;
        fi
     if [ "${command}" == "start" ];
   then    
   start;
   fi
七:建立互信,注意要涵蓋所有服務(wù)器。下面僅展示一個服務(wù)器的。

[root@lvs-a ~]# ssh-keygen -t  rsa

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.25

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.26

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.29

[root@lvs-a ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.9.30

八:檢查mha配置是否正確。

1.檢查ssh是否配置成功

[root@lvs-a ~]#masterha_check_ssh --conf=/etc/masterha/app1.cnf

最后顯示如下字段說明成功:

Tue Jun 30 01:58:05 2015 - [info] All SSH connection tests passed successfully.

2.檢查mysql主從復(fù)制狀態(tài)是否成功

[root@lvs-a ~]#masterha_check_repl --conf=/etc/masterha/app1.cnf

最后出現(xiàn)MySQL Replication Health is OK.字段說明成功。

3. 啟動管理節(jié)點進程

[root@lvs-a ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log < /dev/null 2>&1 &

[1] 10085

4. 檢查mha主從狀態(tài)

[root@lvs-a ~]# masterha_check_status --conf=/etc/masterha/app1.cnf

顯示如下

app1 (pid:10085) is running(0:PING_OK), master:192.168.153.147

5. 停止mha

[root@lvs-a ~]# masterha_stop --conf=/etc/masterha/app1.cnf

 九:安裝lvs+keepalived的具體過程參考,文檔   LVS+keepalived的安裝部署  http://blog.itpub.net/29654823/viewspace-1844282/

十:驗證環(huán)境的正確性:

關(guān)閉現(xiàn)在的主(9.25),vip9.232 會自動飄向主備(9.26),從而實現(xiàn)了主的高可用,這時候,manger(9.27)進程也就死了,需要手動起來。





向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI