溫馨提示×

溫馨提示×

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

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

mysql中MHA如何搭建

發(fā)布時間:2021-11-16 15:51:13 來源:億速云 閱讀:218 作者:小新 欄目:MySQL數(shù)據(jù)庫

這篇文章主要為大家展示了“mysql中MHA如何搭建”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“mysql中MHA如何搭建”這篇文章吧。

操作系統(tǒng) :
debian5.0.2

數(shù)據(jù)庫版本:
mysql5.0.51a

結(jié)構(gòu):
manager : 192.168.1.136(M1)

master : 192.168.1.20:3306(N1)
slave1 :  192.168.1.20:3307(N2)
slave2 :  192.168.1.20:3308(N3)
slave3 :  192.168.1.20:3309(N4)

軟件包準(zhǔn)備:
mha4mysql-manager_0.55-1_all.deb(還有對應(yīng)的二進(jìn)制包,源碼包)
mha4mysql-node_0.54-1_all.deb

準(zhǔn)備就緒:
①安裝依賴包
apt-get install libdbd-mysql-perl
apt-get install libconfig-tiny-perl
apt-get install liblog-dispatch-perl
apt-get install libparallel-forkmanager-perl
centos版(yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager)

②安裝mha manager包和node包
M1:dpkg -i mha4mysql-manager_0.55-1_all.deb
N1:dpkg -i mha4mysql-node_0.54-1_all.deb
N2:dpkg -i mha4mysql-node_0.54-1_all.deb
N3:dpkg -i mha4mysql-node_0.54-1_all.deb
N4:dpkg -i mha4mysql-node_0.54-1_all.deb

③主機(jī)信任(由于公司環(huán)境的原因,這里使用的是mysql用戶)
M1:
#su mysql
#ssh-keygen -t rsa
#cat ~/.ssh/id_rsa.pub
將公鑰內(nèi)容添加到N1,N2,N3,N4的對應(yīng)用戶(這里是mysql用戶)下的./ssh/authorized_keys下即可實現(xiàn)M1到N1,N2,N3,N4的免密登錄。
另外,這里踩了個坑,就是免密登錄的server端(N1,N2,N3,N4)即/etc/ssh/sshd_config文件必須開啟公鑰登錄
mysql中MHA如何搭建
免密登錄的client端(M1)即/etc/ssh/ssh_config文件不能關(guān)閉公鑰登錄(這個參數(shù)設(shè)置之后在公司環(huán)境下貌似還是不能免密登錄,最后用了默認(rèn)配置就可以了)
mysql中MHA如何搭建
N1,N2,N3,N4節(jié)點(diǎn)之間也互相信任

2016-12-4 22:44:12 ADD
ssh客戶端得注釋以下參數(shù) 才能免密登錄
PreferredAuthentications password

③新建mysql mha管理賬戶,mysql repl復(fù)制用戶
mysql中MHA如何搭建
mysql中MHA如何搭建

④新建mha工作目錄
M1 :mkdir -p /opt/mha_manager/log  chown -R mysql /opt/mha_manager
N1~N4:mkdir -p /opt/mha_node        chown -R mysql /opt/mha_node

⑤配置manager
M1:
#mkdir  -p /etc/mha
#cat /etc/mha/app1.conf

  • manager_workdir=/opt/mha_manager_work

  • manager_log=/opt/mha_manager_work/log/app1.log

  • remote_workdir=/opt/mha_node

  • 對以上的配置文件更改屬主為ssh信任賬戶(這里是mysql)




  • 另外(源碼安裝的mysql注意):


  •     mha遠(yuǎn)程登錄操作mysql的時候默認(rèn)用不到/usr/local/mysql/bin下面的mysql 以及 mysqlbinlog會報相應(yīng)的錯誤信息,


  • 這個時候我們需要做如下操作


  • #ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog


  • #ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql



  • ④開啟manager


  • mysql中MHA如何搭建
  • 這里要注意的是,不要后臺啟動,即不要以 masterha_manager --conf=/etc/mha/app1.conf & 形式啟動,不然會報錯退出。我們前臺啟動,然后ctrl+z , bg放入后臺。



  • 至此,整個搭建過程完畢。



  • 注意事項:


  • ①slave最好設(shè)置 read_only=1 relay_log_purge=0,利用定時任務(wù)刪除relay_log;


  • ②mha進(jìn)行failover之后會在app1.log中記錄相應(yīng)的chang master to master_xxx等信息,因此我們可以通過這條信息手動將

  • 所宕master

  • 連接到新主;


  • ③可以使用(masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf)進(jìn)行手動切換主,然后將新主手動添加作為老主的從(步驟2),注意mha manager必須處于關(guān)閉狀態(tài)(masterha_check_status --conf=xxx);


  • ④我們可以在app1.conf配置主庫宕機(jī)后vip的飄逸腳本(

  • master_ip_failover_script=/usr/bin/masterha_ip_failover

  • ),但是第一次必須得自己手動在master添加vip.




  • 附 masterha_ip_failover腳本



  • 點(diǎn)擊(此處)折疊或打開

    1. #!/usr/bin/env perl


    2. #  Copyright (C) 2011 DeNA Co.,Ltd.

    3. #

    4. #  This program is free software; you can redistribute it and/or modify

    5. #  it under the terms of the GNU General Public License as published by

    6. #  the Free Software Foundation; either version 2 of the License, or

    7. # (at your option) any later version.

    8. #

    9. #  This program is distributed in the hope that it will be useful,

    10. #  but WITHOUT ANY WARRANTY; without even the implied warranty of

    11. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

    12. #  GNU General Public License for more details.

    13. #

    14. #  You should have received a copy of the GNU General Public License

    15. #   along with this program; if not, write to the Free Software

    16. #  Foundation, Inc.,

    17. #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


    18. ## Note: This is a sample script and is not complete. Modify the script based on your environment.


    19. use strict;

    20. use warnings FATAL => 'all';


    21. use Getopt::Long;


    22. my (

    23.     $command, $ssh_user, $orig_master_host, $orig_master_ip,

    24.     $orig_master_port, $new_master_host, $new_master_ip, $new_master_port

    25. );


    26. my $vip = '192.168.1.111/24';

    27. my $key = '0';

    28. my $ssh_start_vip = "sudo /sbin/ifconfig eth2:$key $vip";

    29. my $ssh_stop_vip = "sudo /sbin/ifconfig eth2:$key down";

    30. #my $ssh_Bcast_arp = "/usr/bin/arping -c 3 -A 10.1.99.233"; #ARP回復(fù)模式,更新鄰居。要是不加則服務(wù)器會自動等到vip緩存失效,期間VIP會有一定時間的不可用。


    31. GetOptions(

    32.     'command=s' => \$command,

    33.     'ssh_user=s' => \$ssh_user,

    34.     'orig_master_host=s' => \$orig_master_host,

    35.     'orig_master_ip=s' => \$orig_master_ip,

    36.     'orig_master_port=i' => \$orig_master_port,

    37.     'new_master_host=s' => \$new_master_host,

    38.     'new_master_ip=s' => \$new_master_ip,

    39.     'new_master_port=i' => \$new_master_port,

    40. );


    41. exit &main();


    42. sub main {


    43.     print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";


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


    45.         my $exit_code = 1;

    46.         eval {

    47.             print "Disabling the VIP on old master: $orig_master_host \n";

    48.             &stop_vip();

    49.             $exit_code = 0;

    50.         };

    51.         if ($@) {

    52.             warn "Got Error: $@\n";

    53.             exit $exit_code;

    54.         }

    55.         exit $exit_code;

    56.     }

    57.     elsif ( $command eq "start" ) {


    58.         my $exit_code = 10;

    59.         eval {

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

    61.             &start_vip();

    62.            # &start_arp();

    63.             $exit_code = 0;

    64.         };

    65.         if ($@) {

    66.             warn $@;

    67.             exit $exit_code;

    68.         }

    69.         exit $exit_code;

    70.     }

    71.     elsif ( $command eq "status" ) {

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

    73.         exit 0;

    74.     }


    75.     else {

    76.         &usage();

    77.         exit 1;

    78.     }

    79. }


    80. sub start_vip() {

    81.     `ssh -p56000 $ssh_user\@$new_master_host \" $ssh_start_vip \"`;

    82. }

    83. #sub start_arp() {

    84. #    `ssh $ssh_user\@$new_master_host \" $ssh_Bcast_arp \"`;

    85. #}

    86. sub stop_vip() {

    87.     `ssh -p56000 $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;

    88. }


    89. sub usage {

    90.   print

    91. "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";

    92. }

以上是“mysql中MHA如何搭建”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

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

AI