成功搭建Mysql_MHA高可用架構(gòu)方法-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的廣州網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

本文主要給大家簡單講講搭建Mysql_MHA高可用架構(gòu)方法,相關(guān)專業(yè)術(shù)語大家可以上網(wǎng)查查或者找一些相關(guān)書籍補(bǔ)充一下,這里就不涉獵了,我們就直奔主題吧,希望搭建Mysql_MHA高可用架構(gòu)方法這篇文章可以給大家?guī)硪恍?shí)際幫助。

Mysql_MHA高可用架構(gòu)搭建

窗體頂端

窗體底端

環(huán)境及兼容包

系統(tǒng)環(huán)境:centos6.5

Mysql: mysql-5.5.6 數(shù)據(jù)庫用源碼安裝,這里就不介紹了

主機(jī)分配:

  Master : 192.168.0.101 node1 (主庫)

  Slave1 : 192.168.0.102 node2 (備用主庫)

  Slave2 : 192.168.0.103 node3 (從庫+MHA控制節(jié)點(diǎn))

MHA兼容包見附件

 成功搭建Mysql_MHA高可用架構(gòu)方法

窗體頂端

窗體底端

添加免密碼登錄,互為認(rèn)證

++++++++++以下操作在三臺節(jié)點(diǎn)上都的執(zhí)行#+++++++++++++

##添加hosts,并修改各自主機(jī)名

 192.168.0.101node1

 192.168.0.102node2

 192.168.0.103node3

##添加秘鑰

 ssh-keygen -t rsa

 cat/root/.ssh/id_rsa.pub >/root/.ssh/authorized_keys

##將三個節(jié)點(diǎn)的秘鑰都添加到 authorized_keys,包括自己的秘鑰

##在三個節(jié)點(diǎn)上依次執(zhí)行下列命令用以檢查 ssh

 ssh node1 date

 ssh node2 date

 ssh node3 date

##只有互為認(rèn)證登錄成功才能繼續(xù)后續(xù)操作

窗體頂端

窗體底端

安裝MHA node 包

++++++++++以下操作在三臺節(jié)點(diǎn)上都的執(zhí)行#+++++++++++++

##更新yum為阿里源

 mv/etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.old

 wget -O/etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

 yum clean all

 yum makecache

 yum update

##安裝環(huán)境包

 yum installperl-DBD-MySQL perl perl-devel cpan

 rpm -ivhmha4mysql-node-0.56-0.el6.noarch.rpm

##mha node 安裝完成后會在/usr/bin 下面生成以下四個腳本:(這些工具通常由mha manger的腳本觸發(fā),無需人工操作)

/usr/bin/save_binary_logs      ##保存和復(fù)制master的二進(jìn)制日志

/usr/bin/apply_diff_relay_logs ##識別差異的中繼日志事件并將其差異的事件應(yīng)用于其他slave

/usr/bin/filter_mysqlbinlog    ##去除不必要的ROLLBACK事件(MHA已經(jīng)不再使用這個工具)

/usr/bin/purge_relay_logs      ##清除中繼日志(不會阻塞SQL線程)

窗體頂端

窗體底端

安裝MHA manager節(jié)點(diǎn)包

 [node3]#tar xf mha.tar.gz

 [node3]# cd mha

##登陸node3嘗試安裝mha4mysql-manager-0.56-0.el6.noarch,果然報錯缺少perl模塊的包

##按安裝順序整理如下:

compat-db43-4.3.29-15.el6

perl-Mail-Sender-0.8.16-3.el6

perl-Mail-Sendmail-0.79-12.el6

perl-Config-Tiny-2.12-7.1.el6

perl-Parallel-ForkManager-0.7.9-1.el6

perl-Log-Dispatch-2.27-1.el6

#在安裝perl-Log-Dispatch-2.27-1.el6包時還會遇到有模塊的包需要安裝,按安裝順序整理如下:

perl-TimeDate-1.16-11.1.el6

perl-MIME-Types-1.28-2.el6

perl-MailTools-2.04-4.el6

perl-Email-Date-Format-1.002-5.el6

perl-Params-Validate-0.92-3.el6

perl-MIME-Lite-3.027-2.el6

之后便可以正常安裝mha4mysql-manager-0.56-0.el6.noarch

rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

##mha manager 安裝完成后會在/usr/bin 下面生成一些腳本:

/usr/bin/masterha_check_repl        ##檢查mysql復(fù)制狀況

/usr/bin/masterha_check_ssh         ##檢查MHA的ssh配置狀況

/usr/bin/masterha_check_status      ##檢測當(dāng)前MHA運(yùn)行狀態(tài)

/usr/bin/masterha_conf_host         ##添加或刪除配置的server信息

/usr/bin/masterha_manager           ##啟動MHA

/usr/bin/masterha_master_monitor    ##監(jiān)測master是否宕機(jī)

/usr/bin/masterha_master_switch     ##控制故障轉(zhuǎn)移(自動或手動)

/usr/bin/masterha_secondary_check   ##也是監(jiān)測master是否宕機(jī)的腳本

/usr/bin/masterha_stop              ##關(guān)閉MHA

窗體頂端

窗體底端

修改數(shù)據(jù)庫為主從

##將三個數(shù)據(jù)庫文件 my.cf 中 server-id = 的值依次修改為 1、2、3

 [node1]#mysql-uroot

mysql> show master status;

        //查看 master_log_file和 Position 值

##在三個庫中都添加repl和monitor用戶

mysql> GRANT REPLICATION SLAVE ON *.* TO'repl'@'192.168.0.%' IDENTIFIED BY '123456';

mysql> grant all privileges on *.* to'monitor'@'192.168.0.%' identified by '123456';

mysql> flush privileges;

##在node1 上執(zhí)行下列語句

mysql> install plugin rpl_semi_sync_master soname'semisync_master.so';

      //主庫安裝semisync_master插件

mysql> set global rpl_semi_sync_master_enabled=1;

mysql> set globalrpl_semi_sync_master_timeout=30000;

      //表示主庫在某次事務(wù)中,如果等待時間超過30000毫秒,那么則降級為普通模式,不再等待備庫。如果主庫再次探測到,備庫恢復(fù)了,則會自動再次回到Semi-sync狀態(tài)。

##node2和node3上操作:

mysql> CHANGE MASTER TO

           MASTER_HOST='192.168.0.101',

           MASTER_PORT=3306,

           MASTER_USER='repl',

           MASTER_PASSWORD='123456',

           MASTER_LOG_FILE='mysql-bin.000004',

           MASTER_LOG_POS=0;

                //MASTER_LOG_FILE= 和 MASTER_LOG_POS= 參數(shù)的值填寫剛剛showmaster status查詢的值

mysql> install plugin rpl_semi_sync_slave soname'semisync_slave.so';

mysql> set global rpl_semi_sync_slave_enabled=1;

mysql> flush privileges;

mysql> start slave;

####從mysql5.5之后,mysql為了保證主從庫數(shù)據(jù)一致性,引進(jìn)了semi-sync功能,

semi-sync意思是MASTER只需要接收到其中一臺SLAVE的返回信息,就會commit;否則需等待直至切換成異步再提交。

優(yōu)點(diǎn):

當(dāng)事務(wù)返回客戶端成功后,則日志一定在至少兩臺主機(jī)上存在。

MySQL的Semi-sync適合小事務(wù),且兩臺主機(jī)的延遲又較小,則Semi-sync可以實(shí)現(xiàn)在性能很小損失的情況下的零數(shù)據(jù)丟失。

缺點(diǎn):

完成單個事務(wù)增加了額外的等待延遲,延遲的大小取決于網(wǎng)絡(luò)的好壞。

窗體頂端

窗體底端

Manager節(jié)點(diǎn)配置

 [node3]#

 [node3]# mkdir -p/etc/masterha /masterha/app1   /masterha/scripts

 [node3]# cd/etc/masterha/

 [node3]# vimapp1.cnf

            //app1.cnf 文件文末提供,在這里先把這兩項(xiàng)參數(shù)值留空,到添加VIP 地址的時候在改成文 app1.cnf 文件中的值

              master_ip_failover_script=""

              master_ip_online_change_script=""

###檢測配置

##檢測ssh配置

 [node3]# exportPERL5LIB=$PERL5LIB:/usr/lib/perl5/vendor_perl/

 [node3]#masterha_check_ssh --conf=/etc/masterha/app1.cnf

##檢測mysql配置

 [node3]#masterha_check_repl --conf=/etc/masterha/app1.cnf

##檢查MHA 的運(yùn)行狀態(tài)

 [node3]#masterha_check_status --conf=/etc/masterha/app1.cnf

###運(yùn)行MHA

 [node3]# nohupmasterha_manager --conf=/etc/masterha/app1.cnf < /dev/null >/masterha/app1/manager.log 2>&1 &

 [node3]# tail -f/masterha/app1/manager.log

 [node3]#masterha_check_status --conf=/etc/masterha/app1.cnf

窗體頂端

窗體底端

驗(yàn)證

##先在master庫上查看slave信息

mysql> show slave hosts;

+-----------+------+------+-----------+

| Server_id | Host | Port | Master_id |

+-----------+------+------+-----------+

|        3 |     | 3306 |        1 |

|        2 |     | 3306 |        1 |

+-----------+------+------+-----------+

##關(guān)閉 node1 上面的 master 庫,然后在node2的slave庫上查看是否切為主庫

mysql> show slave hosts;

+-----------+------+------+-----------+

| Server_id | Host | Port | Master_id |

+-----------+------+------+-----------+

|        3 |     | 3306 |        2 |

+-----------+------+------+-----------+

##恢復(fù)高可用架構(gòu)

##failover成功之后,MHA就會停了,這時候是slave1 到slave2的主從復(fù)制,假設(shè)192.168.0.101 數(shù)據(jù)庫恢復(fù)了,那我們需要恢復(fù)高可用架構(gòu),讓MHA啟動。

##這是時候需要做的是,啟動192.168.0.101數(shù)據(jù)庫,在manager上面執(zhí)行以下命令,找到CHANGEMASTER 執(zhí)行即可。

 [node1]# grep -i"All other slaves should start replication from here"/masterha/app1/manager.log

 Fri Aug 2512:11:40 2017 - [info] All other slavesshould start replication from here. Statement should

 be: CHANGE MASTERTO MASTER_HOST='192.168.0.102', MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000012',

 MASTER_LOG_POS=328, MASTER_USER='repl', MASTER_PASSWORD='123456';

      //注意 MASTER_HOST= MASTER_LOG_FILE= MASTER_LOG_POS=  三個的值

 [node1]# mysql-uroot

 mysql> CHANGEMASTER TO MASTER_HOST='192.168.0.102', MASTER_LOG_FILE='mysql-bin.000012',MASTER_LOG_POS=328, MASTER_USER='repl', MASTER_PASSWORD='123456';

 mysql> slavestart;

 [node2]#mysql-uroot

 mysql> showslave hosts;

+-----------+------+------+-----------+

| Server_id | Host | Port | Master_id |

+-----------+------+------+-----------+

|        3 |     | 3306 |        2 |

|        1 |     | 3306 |        2 |

+-----------+------+------+-----------+

###在開啟MHA 服務(wù),然后關(guān)閉node2 上面的mysql模擬宕機(jī)

 [node3]# nohupmasterha_manager --conf=/etc/masterha/app1.cnf < /dev/null >/masterha/app1/manager.log 2>&1 &

##還需刪除app1.failover.complete文件,否則會報錯

 [node3]# rm -f/masterha/app1/app1.failover.complete

 [node2]# servicemysql3306 stop

 [node1]# mysql-uroot

 mysql> showslave hosts;

+-----------+------+------+-----------+

| Server_id | Host | Port | Master_id |

+-----------+------+------+-----------+

|        3 |     | 3306 |        1 |

+-----------+------+------+-----------+

##在執(zhí)行上面(恢復(fù)高可用架構(gòu)) 操作,將slave1 加入到集群里來

窗體頂端

窗體底端

VIP地址漂移

####注意必須所有的主機(jī)網(wǎng)卡信息都為 eth0

##先在 node1 上添加虛擬地址

 [node1]#/sbin/ifconfig eth0:1 192.168.0.100/24

##修改app1.cnf 配置文件中

master_ip_failover_script=/etc/masterha/master_ip_failover           #master failover時執(zhí)行

master_ip_online_change_script=/etc/masterha/master_ip_online_change  #master switchover時執(zhí)行

##編輯master_ip_failover和 master_ip_online_change文件

 [node3]# cd/etc/masterha/

 [node3]# vimmaster_ip_failover

 [node3]# vimmaster_ip_online_change

            //配置文件內(nèi)容在文檔末尾

窗體頂端

窗體底端

app1.cnf 配置文件

app1.cnf

[server default]

user=monitor

password=123456

ping_interval=10

#repl_workdir=/masterha/app1

repl_user=repl

repl_password=123456

ssh_user=root

master_ip_failover_script=/etc/masterha/master_ip_failover

master_ip_online_change_script=/etc/masterha/master_ip_online_change

#report_script= /etc/masterha/send_report

shutdown_script=""

secondary_check_script=/usr/bin/masterha_secondary_check-s node2 -s node1

manager_workdir=/masterha/app1

manager_log=/masterha/app1/manager.log

#remote_workdir=/masterha/app1

[server1]

hostname=192.168.0.101

port=3306

master_binlog_dir=/data/mysql/data3306

candidate_master=1

#check_repl_delay=0

[server2]

hostname=192.168.0.102

port=3306

master_binlog_dir=/data/mysql/data3306

candidate_master=1

#check_repl_delay=0

[server3]

hostname=192.168.0.103

#port=3306

#master_binlog_dir=/data/mysql/data3306

no_master=1

#node3不參與競選,只作從庫和manager使用

read_only=1

窗體頂端

窗體底端

配置文件master_ip_failover

master_ip_failover

#!/usr/bin/env perl

use strict;

use warnings FATAL =>'all';

use Getopt::Long;

my (

$command,         $ssh_user,       $orig_master_host, $orig_master_ip,

$orig_master_port, $new_master_host, $new_master_ip,   $new_master_port

);

my $vip = '192.168.0.100/24';

my $key = "1";

my $ssh_start_vip = "/sbin/ifconfig eth0:$key$vip";

my $ssh_stop_vip = "/sbin/ifconfig eth0:$keydown";

my $exit_code = 0;

GetOptions(

'command=s'         => \$command,

'ssh_user=s'        => \$ssh_user,

'orig_master_host=s' => \$orig_master_host,

'orig_master_ip=s'  => \$orig_master_ip,

'orig_master_port=i' => \$orig_master_port,

'new_master_host=s' => \$new_master_host,

'new_master_ip=s'   => \$new_master_ip,

'new_master_port=i' => \$new_master_port,

);

exit &main();

sub main {

#print "\n\nIN SCRIPTTEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

if ( $command eq "stop" || $command eq"stopssh" ) {

       #$orig_master_host, $orig_master_ip, $orig_master_port are passed.

       # If youmanage master ip address at global catalog database,

       #invalidate orig_master_ip here.

       my$exit_code = 1;

       eval {

           print"\n\n\n***************************************************************\n";

           print"Disabling the VIP - $vip on old master: $orig_master_host\n";

           print"***************************************************************\n\n\n\n";

&stop_vip();

           $exit_code = 0;

       };

       if ($@){

           warn"Got Error: $@\n";

           exit$exit_code;

       }

       exit$exit_code;

}

elsif ( $command eq "start" ) {

       # allarguments are passed.

       # If youmanage master ip address at global catalog database,

       # activatenew_master_ip here.

       # You canalso grant write access (create user, set read_only=0, etc) here.

my $exit_code = 10;

       eval {

           print"\n\n\n***************************************************************\n";

           print"Enabling the VIP - $vip on new master: $new_master_host \n";

           print"***************************************************************\n\n\n\n";

&start_vip();

           $exit_code = 0;

       };

       if ($@){

           warn$@;

           exit $exit_code;

       }

       exit$exit_code;

}

elsif ( $command eq "status" ) {

       print"Checking the Status of the script.. OK \n";

       `ssh$ssh_user\@$orig_master_host \" $ssh_start_vip \"`;

       exit0;

}

else {

&usage();

       exit1;

}

}

# A simple system call that enable the VIP on the newmaster

sub start_vip() {

`ssh $ssh_user\@$new_master_host \" $ssh_start_vip\"`;

}

# A simple system call that disable the VIP on theold_master

sub stop_vip() {

`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip\"`;

}

sub usage {

print

"Usage: master_ip_failover–command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip–orig_master_port=po

rt –new_master_host=host –new_master_ip=ip–new_master_port=port\n";

}

窗體頂端

窗體底端

配置文件master_ip_online_change

master_ip_online_change

#!/usr/bin/env perl

use strict;

use warnings FATAL =>'all';

use Getopt::Long;

my $vip = '192.168.0.100/24';

my $key = "1";

my $ssh_start_vip = "/sbin/ifconfig eth0:$key$vip";

my $ssh_stop_vip = "/sbin/ifconfig eth0:$keydown";

my $exit_code = 0;

my (

 $command,             $orig_master_is_new_slave,$orig_master_host,

 $orig_master_ip,      $orig_master_port,        $orig_master_user,

 $orig_master_password, $orig_master_ssh_user,    $new_master_host,

 $new_master_ip,       $new_master_port,         $new_master_user,

 $new_master_password, $new_master_ssh_user,

);

GetOptions(

 'command=s'               => \$command,

 'orig_master_is_new_slave' => \$orig_master_is_new_slave,

 'orig_master_host=s'      =>\$orig_master_host,

 'orig_master_ip=s'        =>\$orig_master_ip,

 'orig_master_port=i'      =>\$orig_master_port,

 'orig_master_user=s'      =>\$orig_master_user,

 'orig_master_password=s'  =>\$orig_master_password,

 'orig_master_ssh_user=s'  =>\$orig_master_ssh_user,

 'new_master_host=s'       =>\$new_master_host,

 'new_master_ip=s'         =>\$new_master_ip,

 'new_master_port=i'       =>\$new_master_port,

 'new_master_user=s'       =>\$new_master_user,

 'new_master_password=s'   => \$new_master_password,

 'new_master_ssh_user=s'   =>\$new_master_ssh_user,

);

exit &main();

sub main {

#print "\n\nIN SCRIPTTEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

if ( $command eq "stop" || $command eq"stopssh" ) {

       #$orig_master_host, $orig_master_ip, $orig_master_port are passed.

       # If youmanage master ip address at global catalog database,

       #invalidate orig_master_ip here.

       my $exit_code = 1;

       eval {

           print"\n\n\n***************************************************************\n";

           print"Disabling the VIP - $vip on old master: $orig_master_host\n";

           print"***************************************************************\n\n\n\n";

&stop_vip();

           $exit_code = 0;

       };

       if ($@){

           warn"Got Error: $@\n";

           exit$exit_code;

       }

       exit$exit_code;

}

elsif ( $command eq "start" ) {

       # allarguments are passed.

       # If youmanage master ip address at global catalog database,

       # activatenew_master_ip here.

       # You canalso grant write access (create user, set read_only=0, etc) here.

my $exit_code = 10;

       eval {

           print"\n\n\n***************************************************************\n";

           print"Enabling the VIP - $vip on new master: $new_master_host \n";

           print"***************************************************************\n\n\n\n";

&start_vip();

           $exit_code = 0;

       };

       if ($@){

           warn$@;

           exit$exit_code;

       }

       exit$exit_code;

}

elsif ( $command eq "status" ) {

       print"Checking the Status of the script.. OK \n";

       `ssh$orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`;

       exit0;

}

else {

&usage();

       exit1;

}

}

# A simple system call that enable the VIP on the newmaster

sub start_vip() {

`ssh $new_master_ssh_user\@$new_master_host \"$ssh_start_vip \"`;

}

# A simple system call that disable the VIP on theold_master

sub stop_vip() {

`ssh $orig_master_ssh_user\@$orig_master_host \"$ssh_stop_vip \"`;

}

sub usage {

print

"Usage: master_ip_failover–command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip–orig_master_port=po

rt –new_master_host=host –new_master_ip=ip–new_master_port=port\n";

}

搭建Mysql_MHA高可用架構(gòu)方法就先給大家講到這里,對于其它相關(guān)問題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會捕捉一些行業(yè)新聞及專業(yè)知識分享給大家的。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站標(biāo)題:成功搭建Mysql_MHA高可用架構(gòu)方法-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article22/cshgcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序自適應(yīng)網(wǎng)站、虛擬主機(jī)、用戶體驗(yàn)電子商務(wù)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)