{"id":171,"date":"2025-09-17T20:59:14","date_gmt":"2025-09-17T12:59:14","guid":{"rendered":"https:\/\/www.mooloco.com\/?p=171"},"modified":"2025-12-09T11:20:43","modified_gmt":"2025-12-09T03:20:43","slug":"mha%e6%95%b0%e6%8d%ae%e5%ba%93%e9%ab%98%e5%8f%af%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.mooloco.com\/?p=171","title":{"rendered":"MHA\u6570\u636e\u5e93\u9ad8\u53ef\u7528"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5e9f\u8bdd\u5c11\u8bf4\uff0c\u76f4\u63a5\u5f00\u59cb\uff01<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_75 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83\" >\u5b9e\u9a8c\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%BC%80%E5%A7%8B\" >\u5f00\u59cb<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C\" >\u51c6\u5907\u5de5\u4f5c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%AE%89%E8%A3%85_MHA_%E8%BD%AF%E4%BB%B6\" >\u5b89\u88c5 MHA \u8f6f\u4ef6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%9C%A8%E6%89%80%E6%9C%89%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%85%8D%E7%BD%AE%E6%97%A0%E5%AF%86%E7%A0%81%E8%AE%A4%E8%AF%81\" >\u5728\u6240\u6709\u670d\u52a1\u5668\u4e0a\u914d\u7f6e\u65e0\u5bc6\u7801\u8ba4\u8bc1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%9C%A8_manager_%E8%8A%82%E7%82%B9%E4%B8%8A%E9%85%8D%E7%BD%AE_MHA\" >\u5728 manager \u8282\u70b9\u4e0a\u914d\u7f6e MHA<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%90%AF%E5%8A%A8\" >\u542f\u52a8<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E5%9C%A8_manager_%E8%8A%82%E7%82%B9%E4%B8%8A%E5%90%AF%E5%8A%A8_MHA\" >\u5728 manager \u8282\u70b9\u4e0a\u542f\u52a8 MHA<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.mooloco.com\/?p=171\/#%E6%9F%A5%E7%9C%8B%E7%8A%B6%E6%80%81\" >\u67e5\u770b\u72b6\u6001<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83\"><\/span>\u5b9e\u9a8c\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MHA manager \u8282\u70b9\u670d\u52a1\u5668\uff1aCentOS7.6(64 \u4f4d) manager\/192.168.10.13 \uff0c\u5b89\u88c5MHA node \u548c manager \u7ec4\u4ef6 Master \u8282\u70b9\u670d\u52a1\u5668\uff1aCentOS7.6(64 \u4f4d) mysql1\/192.168.10.16 \uff0c\u5b89\u88c5mysql5.7\u3001MHA node \u7ec4\u4ef6 Slave1 \u8282\u70b9\u670d\u52a1\u5668\uff1aCentOS7.6(64 \u4f4d) mysql2\/192.168.10.14 \uff0c\u5b89\u88c5mysql5.7\u3001MHA node \u7ec4\u4ef6 Slave2 \u8282\u70b9\u670d\u52a1\u5668\uff1aCentOS7.6(64 \u4f4d) mysql3\/192.168.10.15 \uff0c\u5b89\u88c5mysql5.7\u3001MHA node \u7ec4\u4ef6<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BC%80%E5%A7%8B\"><\/span>\u5f00\u59cb<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C\"><\/span>\u51c6\u5907\u5de5\u4f5c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsystemctl stop firewalld\nsystemctl disable firewalld\nsetenforce 0\n\n\u2460  Master\u3001Slave1\u3001Slave2 \u8282\u70b9\u4e0a\u5b89\u88c5 mysql5.7\n\n\u2461  \u4fee\u6539 Master\u3001Slave1\u3001Slave2 \u8282\u70b9\u7684\u4e3b\u673a\u540d\nhostnamectl set-hostname Master\nhostnamectl set-hostname slave1\nhostnamectl set-hostname slave2\n\n\u2462 \u4fee\u6539 Master\u3001Slave1\u3001Slave2 \u8282\u70b9\u7684 Mysql\u4e3b\u914d\u7f6e\u6587\u4ef6\/etc\/my.cnf \n##Master \u8282\u70b9##\nvim \/etc\/my.cnf\n&#x5B;mysqld]\nserver-id = 1\nlog_bin = master-bin\nlog-slave-updates = true\n\nsystemctl restart mysqld\n\n##Slave1 \u8282\u70b9##\nvim \/etc\/my.cnf\nserver-id = 2 \t\t\t\t\t\t#\u4e09\u53f0\u670d\u52a1\u5668\u7684 server-id \u4e0d\u80fd\u4e00\u6837\nlog_bin = master-bin\nrelay-log = relay-log-bin\nrelay-log-index = slave-relay-bin.index\n\nsystemctl restart mysqld\n\n\n###Slave2 \u8282\u70b9##\nvim \/etc\/my.cnf\t\t\t\t\t\t#\u4e09\u53f0\u670d\u52a1\u5668\u7684 server-id \u4e0d\u80fd\u4e00\u6837\nserver-id = 3 \nrelay-log = relay-log-bin\nrelay-log-index = slave-relay-bin.index\n\n\nsystemctl restart mysqld\n\n\u2463 \u5728 Master\u3001Slave1\u3001Slave2 \u8282\u70b9\u4e0a\u90fd\u521b\u5efa\u4e24\u4e2a\u8f6f\u94fe\u63a5\nln -s \/usr\/local\/mysql\/bin\/mysql \/usr\/sbin\/\nln -s \/usr\/local\/mysql\/bin\/mysqlbinlog \/usr\/sbin\/\n\n\u2464\uff0e\u914d\u7f6e mysql \u4e00\u4e3b\u4e24\u4ece\n\uff081\uff09\u6240\u6709\u6570\u636e\u5e93\u8282\u70b9\u8fdb\u884c mysql \u6388\u6743\nmysql -uroot -p\ngrant replication slave on *.* to &#039;myslave&#039;@&#039;192.168.1.%&#039; identified by &#039;123456&#039;;\t\t#\u4ece\u6570\u636e\u5e93\u540c\u6b65\u4f7f\u7528\ngrant all privileges on *.* to &#039;mha&#039;@&#039;192.168.1.%&#039; identified by &#039;manager&#039;;\t\t#manager \u4f7f\u7528\n\ngrant all privileges on *.* to &#039;mha&#039;@&#039;master&#039; identified by &#039;manager&#039;;\t\t\t\t#\u9632\u6b62\u4ece\u5e93\u901a\u8fc7\u4e3b\u673a\u540d\u8fde\u63a5\u4e0d\u4e0a\u4e3b\u5e93\ngrant all privileges on *.* to &#039;mha&#039;@&#039;slave1&#039; identified by &#039;manager&#039;;\ngrant all privileges on *.* to &#039;mha&#039;@&#039;slave2&#039; identified by &#039;manager&#039;;\nflush privileges;\n\n\uff082\uff09\u5728 Master \u8282\u70b9\u67e5\u770b\u4e8c\u8fdb\u5236\u6587\u4ef6\u548c\u540c\u6b65\u70b9\nshow master status;\n+-------------------+----------+--------------+------------------+-------------------+\n| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |\n+-------------------+----------+--------------+------------------+-------------------+\n| master-bin.000001 |    1745  |              |                  |                   |\n+-------------------+----------+--------------+------------------+-------------------+\n\n\uff083\uff09\u5728 Slave1\u3001Slave2 \u8282\u70b9\u6267\u884c\u540c\u6b65\u64cd\u4f5c\nchange master to master_host=&#039;192.168.1.233&#039;,master_user=&#039;myslave&#039;,master_password=&#039;123456&#039;,master_log_file=&#039;master-bin.000001&#039;,master_log_pos=1743; \n\nstart slave;\n\n\uff084\uff09\u5728 Slave1\u3001Slave2 \u8282\u70b9\u67e5\u770b\u6570\u636e\u540c\u6b65\u7ed3\u679c\nshow slave status\\G\t\t\n\/\/\u786e\u4fdd IO \u548c SQL \u7ebf\u7a0b\u90fd\u662f Yes\uff0c\u4ee3\u8868\u540c\u6b65\u6b63\u5e38\u3002\nSlave_IO_Running: Yes\nSlave_SQL_Running: Yes\n\n\uff085\uff09\u4e24\u4e2a\u4ece\u5e93\u5fc5\u987b\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\u6a21\u5f0f\uff1a\nset global read_only=1;\n\n\uff086\uff09\u63d2\u5165\u6570\u636e\u6d4b\u8bd5\u6570\u636e\u5e93\u540c\u6b65\n##\u5728 Master \u4e3b\u5e93\u63d2\u5165\u6761\u6570\u636e\uff0c\u6d4b\u8bd5\u662f\u5426\u540c\u6b65##\ncreate database test_db;\nuse test_db;\ncreate table test(id int);\ninsert into test(id) values (1);\n\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%89%E8%A3%85_MHA_%E8%BD%AF%E4%BB%B6\"><\/span>\u5b89\u88c5 MHA \u8f6f\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\uff081\uff09\u6240\u6709\u670d\u52a1\u5668\u4e0a\u90fd\u5b89\u88c5 MHA \u4f9d\u8d56\u7684\u73af\u5883\uff0c\u9996\u5148\u5b89\u88c5 epel \u6e90\nyum install epel-release --nogpgcheck -y\n\nyum install -y perl-DBD-MySQL \\\nperl-Config-Tiny \\\nperl-Log-Dispatch \\\nperl-Parallel-ForkManager \\\nperl-ExtUtils-CBuilder \\\nperl-ExtUtils-MakeMaker \\\nperl-CPAN\n\n\uff082\uff09\u5b89\u88c5 MHA \u8f6f\u4ef6\u5305\uff0c\u5148\u5728\u6240\u6709\u670d\u52a1\u5668\u4e0a\u5fc5\u987b\u5148\u5b89\u88c5 node \u7ec4\u4ef6\n\u5bf9\u4e8e\u6bcf\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u7248\u672c\u4e0d\u4e00\u6837\uff0c\u8fd9\u91cc CentOS7.6\u9009\u62e9 0.57 \u7248\u672c\u3002\n\u5728\u6240\u6709\u670d\u52a1\u5668\u4e0a\u5fc5\u987b\u5148\u5b89\u88c5 node \u7ec4\u4ef6\uff0c\u6700\u540e\u5728 MHA-manager \u8282\u70b9\u4e0a\u5b89\u88c5 manager \u7ec4\u4ef6\uff0c\u56e0\u4e3a manager \u4f9d\u8d56 node \u7ec4\u4ef6\u3002\ncd \/opt\ntar zxvf mha4mysql-node-0.57.tar.gz\ncd mha4mysql-node-0.57\nperl Makefile.PL\nmake &amp;amp;&amp;amp; make install\n\n\uff083\uff09\u5728 MHA manager \u8282\u70b9\u4e0a\u5b89\u88c5 manager \u7ec4\u4ef6\ncd \/opt\ntar zxvf mha4mysql-manager-0.57.tar.gz\ncd mha4mysql-manager-0.57\nperl Makefile.PL\nmake &amp;amp;&amp;amp; make install\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#manager \u7ec4\u4ef6\u5b89\u88c5\u540e\u5728\/usr\/local\/bin \u4e0b\u9762\u4f1a\u751f\u6210\u51e0\u4e2a\u5de5\u5177\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\uff1a\nmasterha_check_ssh \u68c0\u67e5 MHA \u7684 SSH \u914d\u7f6e\u72b6\u51b5\nmasterha_check_repl \u68c0\u67e5 MySQL \u590d\u5236\u72b6\u51b5\nmasterha_manger \u542f\u52a8 manager\u7684\u811a\u672c\nmasterha_check_status \u68c0\u6d4b\u5f53\u524d MHA \u8fd0\u884c\u72b6\u6001\nmasterha_master_monitor \u68c0\u6d4b master \u662f\u5426\u5b95\u673a\nmasterha_master_switch \u63a7\u5236\u6545\u969c\u8f6c\u79fb\uff08\u81ea\u52a8\u6216\u8005 \u624b\u52a8\uff09\nmasterha_conf_host \u6dfb\u52a0\u6216\u5220\u9664\u914d\u7f6e\u7684 server \u4fe1\u606f\nmasterha_stop  \u5173\u95edmanager\n\n#node \u7ec4\u4ef6\u5b89\u88c5\u540e\u4e5f\u4f1a\u5728\/usr\/local\/bin \u4e0b\u9762\u4f1a\u751f\u6210\u51e0\u4e2a\u811a\u672c\uff08\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u7531 MHAManager \u7684\u811a\u672c\u89e6\u53d1\uff0c\u65e0\u9700\u4eba\u4e3a\u64cd\u4f5c\uff09\u4e3b\u8981\u5982\u4e0b\uff1a\nsave_binary_logs \u4fdd\u5b58\u548c\u590d\u5236 master \u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\napply_diff_relay_logs \u8bc6\u522b\u5dee\u5f02\u7684\u4e2d\u7ee7\u65e5\u5fd7\u4e8b\u4ef6\u5e76\u5c06\u5176\u5dee\u5f02\u7684\u4e8b\u4ef6\u5e94\u7528\u4e8e\u5176\u4ed6\u7684 slave\nfilter_mysqlbinlog \u53bb\u9664\u4e0d\u5fc5\u8981\u7684 ROLLBACK \u4e8b\u4ef6\uff08MHA \u5df2\u4e0d\u518d\u4f7f\u7528\u8fd9\u4e2a\u5de5\u5177\uff09\npurge_relay_logs \u6e05\u9664\u4e2d\u7ee7\u65e5\u5fd7\uff08\u4e0d\u4f1a\u963b\u585e SQL \u7ebf\u7a0b\uff09\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%9C%A8%E6%89%80%E6%9C%89%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E9%85%8D%E7%BD%AE%E6%97%A0%E5%AF%86%E7%A0%81%E8%AE%A4%E8%AF%81\"><\/span>\u5728\u6240\u6709\u670d\u52a1\u5668\u4e0a\u914d\u7f6e\u65e0\u5bc6\u7801\u8ba4\u8bc1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\uff081\uff09\u5728 manager \u8282\u70b9\u4e0a\u914d\u7f6e\u5230\u6240\u6709\u6570\u636e\u5e93\u8282\u70b9\u7684\u65e0\u5bc6\u7801\u8ba4\u8bc1\nssh-keygen -t rsa \t\t\t\t#\u4e00\u8def\u6309\u56de\u8f66\u952e\nssh-copy-id 192.168.1.233\nssh-copy-id 192.168.1.205\nssh-copy-id 192.168.1.151\n\n\uff082\uff09\u5728 master \u4e0a\u914d\u7f6e\u5230\u6570\u636e\u5e93\u8282\u70b9 slave1 \u548c slave2 \u7684\u65e0\u5bc6\u7801\u8ba4\u8bc1\nssh-keygen -t rsa\nssh-copy-id 192.168.1.205\nssh-copy-id 192.168.1.151\n\n\uff083\uff09\u5728 slave1 \u4e0a\u914d\u7f6e\u5230\u6570\u636e\u5e93\u8282\u70b9 master \u548c slave2 \u7684\u65e0\u5bc6\u7801\u8ba4\u8bc1\nssh-keygen -t rsa\nssh-copy-id 192.168.1.233\nssh-copy-id 192.168.1.151\n\n\uff084\uff09\u5728 slave2 \u4e0a\u914d\u7f6e\u5230\u6570\u636e\u5e93\u8282\u70b9 master \u548c slave1 \u7684\u65e0\u5bc6\u7801\u8ba4\u8bc1\nssh-keygen -t rsa\nssh-copy-id 192.168.1.233\nssh-copy-id 192.168.1.205\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%9C%A8_manager_%E8%8A%82%E7%82%B9%E4%B8%8A%E9%85%8D%E7%BD%AE_MHA\"><\/span>\u5728 manager \u8282\u70b9\u4e0a\u914d\u7f6e MHA<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\uff081\uff09\u5728 manager \u8282\u70b9\u4e0a\u590d\u5236\u76f8\u5173\u811a\u672c\u5230\/usr\/local\/bin \u76ee\u5f55\ncp -rp \/opt\/mha4mysql-manager-0.57\/samples\/scripts \/usr\/local\/bin\n\/\/\u62f7\u8d1d\u540e\u4f1a\u6709\u56db\u4e2a\u6267\u884c\u6587\u4ef6\nll \/usr\/local\/bin\/scripts\/\n----------------------------------------------------------------------------------------------------------\nmaster_ip_failover  \t\t#\u81ea\u52a8\u5207\u6362\u65f6 VIP \u7ba1\u7406\u7684\u811a\u672c\nmaster_ip_online_change \t#\u5728\u7ebf\u5207\u6362\u65f6 vip \u7684\u7ba1\u7406\npower_manager \t\t\t\t#\u6545\u969c\u53d1\u751f\u540e\u5173\u95ed\u4e3b\u673a\u7684\u811a\u672c\nsend_report \t\t\t\t#\u56e0\u6545\u969c\u5207\u6362\u540e\u53d1\u9001\u62a5\u8b66\u7684\u811a\u672c\n----------------------------------------------------------------------------------------------------------\n\n\uff082\uff09\u590d\u5236\u4e0a\u8ff0\u7684\u81ea\u52a8\u5207\u6362\u65f6 VIP \u7ba1\u7406\u7684\u811a\u672c\u5230 \/usr\/local\/bin \u76ee\u5f55\uff0c\u8fd9\u91cc\u4f7f\u7528master_ip_failover\u811a\u672c\u6765\u7ba1\u7406 VIP \u548c\u6545\u969c\u5207\u6362\ncp \/usr\/local\/bin\/scripts\/master_ip_failover \/usr\/local\/bin\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#vim \/usr\/local\/bin\/master_ip_failover\n#!\/usr\/bin\/env perl\nuse strict;\nuse warnings FATAL =&gt; &#039;all&#039;;\nuse Carp;\n$SIG{__DIE__} = sub { confess(@_); };\n\nuse Getopt::Long;\n\n# MHA \u53c2\u6570\nmy (\n  $command, $ssh_user, $orig_master_host, $orig_master_ip,\n  $orig_master_port, $new_master_host, $new_master_ip, $new_master_port\n);\n\n# VIP \u914d\u7f6e\nmy $vip   = &#039;192.168.1.100&#039;;    # \u865a\u62df IP\nmy $ifdev = &#039;eth0&#039;;             # \u7f51\u5361\nmy $key   = &#039;1&#039;;                # \u865a\u62df\u7f51\u5361\u5e8f\u53f7\n\n# \u89e3\u6790\u53c2\u6570\nGetOptions(\n  &#039;command=s&#039;          =&gt; \\$command,\n  &#039;ssh_user=s&#039;         =&gt; \\$ssh_user,\n  &#039;orig_master_host=s&#039; =&gt; \\$orig_master_host,\n  &#039;orig_master_ip=s&#039;   =&gt; \\$orig_master_ip,\n  &#039;orig_master_port=i&#039; =&gt; \\$orig_master_port,\n  &#039;new_master_host=s&#039;  =&gt; \\$new_master_host,\n  &#039;new_master_ip=s&#039;    =&gt; \\$new_master_ip,\n  &#039;new_master_port=i&#039;  =&gt; \\$new_master_port,\n);\n\nexit main();\n\nsub main {\n    print &quot;\\n== MHA Failover Script ==\\n&quot;;\n    print &quot;Command: $command\\n&quot;;\n\n    if ( $command eq &quot;stop&quot; || $command eq &quot;stopssh&quot; ) {\n        print &quot;Disabling VIP $vip on old master: $orig_master_host\\n&quot;;\n        my $rc = stop_vip();\n        exit $rc;\n    }\n    elsif ( $command eq &quot;start&quot; ) {\n        print &quot;Enabling VIP $vip on new master: $new_master_host\\n&quot;;\n        my $rc = start_vip();\n        exit $rc;\n    }\n    elsif ( $command eq &quot;status&quot; ) {\n        my $rc = status_vip();\n        if ($rc == 0) {\n            print &quot;VIP $vip is present on master.\\n&quot;;\n        } else {\n            print &quot;VIP $vip is not present.\\n&quot;;\n        }\n        exit $rc;\n    }\n    else {\n        usage();\n        exit 1;\n    }\n}\n\n# \u542f\u52a8 VIP\nsub start_vip {\n    my $cmd = &quot;ssh $ssh_user\\@$new_master_host \\&quot;ip addr add $vip\/24 dev $ifdev label $ifdev:$key\\&quot;&quot;;\n    my $rc = system($cmd);\n    if ($rc != 0) {\n        warn &quot;Failed to start VIP on $new_master_host (exit $rc)\\n&quot;;\n        return 1;\n    }\n    return 0;\n}\n\n# \u505c\u6b62 VIP\nsub stop_vip {\n    my $cmd = &quot;ssh $ssh_user\\@$orig_master_host \\&quot;ip addr del $vip\/24 dev $ifdev label $ifdev:$key\\&quot;&quot;;\n    my $rc = system($cmd);\n    if ($rc != 0) {\n        warn &quot;Failed to stop VIP on $orig_master_host (exit $rc)\\n&quot;;\n        return 1;\n    }\n    return 0;\n}\n\n# \u68c0\u67e5 VIP\nsub status_vip {\n    my $cmd = &quot;ssh $ssh_user\\@$orig_master_host \\&quot;ip addr show dev $ifdev\\&quot; | grep -q $vip&quot;;\n    my $rc = system($cmd);\n    return $rc == 0 ? 0 : 1;\n}\n\nsub usage {\n    print &amp;lt;&amp;lt;&quot;USAGE&quot;;\nUsage: master_ip_failover\n  --command=start|stop|stopssh|status\n  --orig_master_host=host\n  --orig_master_ip=ip\n  --orig_master_port=port\n  --new_master_host=host\n  --new_master_ip=ip\n  --new_master_port=port\nUSAGE\n}\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\uff084\uff09\u521b\u5efa MHA \u8f6f\u4ef6\u76ee\u5f55\u5e76\u62f7\u8d1d\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd9\u91cc\u4f7f\u7528app1.cnf\u914d\u7f6e\u6587\u4ef6\u6765\u7ba1\u7406 mysql \u8282\u70b9\u670d\u52a1\u5668\nmkdir \/etc\/masterha\ncp \/opt\/mha4mysql-manager-0.57\/samples\/conf\/app1.cnf \/etc\/masterha\n\nvim \/etc\/masterha\/app1.cnf\t\t\t\t\t\t#\u5220\u9664\u539f\u6709\u5185\u5bb9\uff0c\u76f4\u63a5\u590d\u5236\u5e76\u4fee\u6539\u8282\u70b9\u670d\u52a1\u5668\u7684IP\u5730\u5740\n&#x5B;server default]\nmanager_log=\/var\/log\/masterha\/app1\/manager.log\nmanager_workdir=\/var\/log\/masterha\/app1\nmaster_binlog_dir=\/usr\/local\/mysql\/data\nmaster_ip_failover_script=\/usr\/local\/bin\/master_ip_failover\nmaster_ip_online_change_script=\/usr\/local\/bin\/master_ip_online_change\npassword=manager\nping_interval=1\nremote_workdir=\/tmp\nrepl_password=123456\nrepl_user=myslave\nsecondary_check_script=\/usr\/local\/bin\/masterha_secondary_check -s 192.168.10.14 -s 192.168.10.15\nshutdown_script=&quot;&quot;\nssh_user=root\nuser=mha\n\n&#x5B;server1]\nhostname=192.168.10.16\nport=3306\n\n&#x5B;server2]\ncandidate_master=1\ncheck_repl_delay=0\nhostname=192.168.10.14\nport=3306\n\n&#x5B;server3]\nhostname=192.168.10.15\nport=3306\n\n----------------------------------------------------------------------------------------------------------\n&#x5B;server default]\nmanager_log=\/var\/log\/masterha\/app1\/manager.log\u3000\u3000\u3000\u3000\u3000\u3000#manager\u65e5\u5fd7\nmanager_workdir=\/var\/log\/masterha\/app1\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000    #manager\u5de5\u4f5c\u76ee\u5f55\nmaster_binlog_dir=\/usr\/local\/mysql\/data\/\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000#master\u4fdd\u5b58binlog\u7684\u4f4d\u7f6e\uff0c\u8fd9\u91cc\u7684\u8def\u5f84\u8981\u4e0emaster\u91cc\u914d\u7f6e\u7684binlog\u7684\u8def\u5f84\u4e00\u81f4\uff0c\u4ee5\u4fbfMHA\u80fd\u627e\u5230\nmaster_ip_failover_script=\/usr\/local\/bin\/master_ip_failover\u3000\u3000#\u8bbe\u7f6e\u81ea\u52a8failover\u65f6\u5019\u7684\u5207\u6362\u811a\u672c\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u7684\u90a3\u4e2a\u811a\u672c\nmaster_ip_online_change_script=\/usr\/local\/bin\/master_ip_online_change\u3000\u3000#\u8bbe\u7f6e\u624b\u52a8\u5207\u6362\u65f6\u5019\u7684\u5207\u6362\u811a\u672c\npassword=manager\t\t\t#\u8bbe\u7f6emysql\u4e2droot\u7528\u6237\u7684\u5bc6\u7801\uff0c\u8fd9\u4e2a\u5bc6\u7801\u662f\u524d\u6587\u4e2d\u521b\u5efa\u76d1\u63a7\u7528\u6237\u7684\u90a3\u4e2a\u5bc6\u7801\nping_interval=1\t\t\t\t#\u8bbe\u7f6e\u76d1\u63a7\u4e3b\u5e93\uff0c\u53d1\u9001ping\u5305\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u9ed8\u8ba4\u662f3\u79d2\uff0c\u5c1d\u8bd5\u4e09\u6b21\u6ca1\u6709\u56de\u5e94\u7684\u65f6\u5019\u81ea\u52a8\u8fdb\u884cfailover\nremote_workdir=\/tmp\t\t\t#\u8bbe\u7f6e\u8fdc\u7aefmysql\u5728\u53d1\u751f\u5207\u6362\u65f6binlog\u7684\u4fdd\u5b58\u4f4d\u7f6e\nrepl_password=123\t\t    #\u8bbe\u7f6e\u590d\u5236\u7528\u6237\u7684\u5bc6\u7801\nrepl_user=myslave\t\t\t#\u8bbe\u7f6e\u590d\u5236\u7528\u6237\u7684\u7528\u6237\nreport_script=\/usr\/local\/send_report\u3000\u3000\u3000\u3000\u3000#\u8bbe\u7f6e\u53d1\u751f\u5207\u6362\u540e\u53d1\u9001\u7684\u62a5\u8b66\u7684\u811a\u672c\nsecondary_check_script=\/usr\/local\/bin\/masterha_secondary_check -s 192.168.10.14 -s 192.168.10.15\t#\u6307\u5b9a\u68c0\u67e5\u7684\u4ece\u670d\u52a1\u5668IP\u5730\u5740\nshutdown_script=&quot;&quot;\t\t\t#\u8bbe\u7f6e\u6545\u969c\u53d1\u751f\u540e\u5173\u95ed\u6545\u969c\u4e3b\u673a\u811a\u672c\uff08\u8be5\u811a\u672c\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5173\u95ed\u4e3b\u673a\u9632\u6b62\u53d1\u751f\u8111\u88c2,\u8fd9\u91cc\u6ca1\u6709\u4f7f\u7528\uff09\nssh_user=root\t\t\t\t#\u8bbe\u7f6essh\u7684\u767b\u5f55\u7528\u6237\u540d\nuser=mha\t\t\t\t\t#\u8bbe\u7f6e\u76d1\u63a7\u7528\u6237root\n\n&#x5B;server1]\nhostname=192.168.10.16\nport=3306\n\n&#x5B;server2]\nhostname=192.168.10.14\nport=3306\ncandidate_master=1\n#\u8bbe\u7f6e\u4e3a\u5019\u9009master\uff0c\u8bbe\u7f6e\u8be5\u53c2\u6570\u4ee5\u540e\uff0c\u53d1\u751f\u4e3b\u4ece\u5207\u6362\u4ee5\u540e\u5c06\u4f1a\u5c06\u6b64\u4ece\u5e93\u63d0\u5347\u4e3a\u4e3b\u5e93\uff0c\u5373\u4f7f\u8fd9\u4e2a\u4ece\u5e93\u4e0d\u662f\u96c6\u7fa4\u4e2d\u6700\u65b0\u7684slave\n\ncheck_repl_delay=0\n#\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5982\u679c\u4e00\u4e2aslave\u843d\u540emaster \u8d85\u8fc7100M\u7684relay logs\u7684\u8bdd\uff0cMHA\u5c06\u4e0d\u4f1a\u9009\u62e9\u8be5slave\u4f5c\u4e3a\u4e00\u4e2a\u65b0\u7684master\uff0c \u56e0\u4e3a\u5bf9\u4e8e\u8fd9\u4e2aslave\u7684\u6062\u590d\u9700\u8981\u82b1\u8d39\u5f88\u957f\u65f6\u95f4\uff1b\u901a\u8fc7\u8bbe\u7f6echeck_repl_delay=0\uff0cMHA\u89e6\u53d1\u5207\u6362\u5728\u9009\u62e9\u4e00\u4e2a\u65b0\u7684master\u7684\u65f6\u5019\u5c06\u4f1a\u5ffd\u7565\u590d\u5236\u5ef6\u65f6\uff0c\u8fd9\u4e2a\u53c2\u6570\u5bf9\u4e8e\u8bbe\u7f6e\u4e86candidate_master=1\u7684\u4e3b\u673a\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5019\u9009\u4e3b\u5728\u5207\u6362\u7684\u8fc7\u7a0b\u4e2d\u4e00\u5b9a\u662f\u65b0\u7684master\n\n&#x5B;server3]\nhostname=192.168.10.15\nport=3306\n----------------------------------------------------------------------------------------------------------\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%90%AF%E5%8A%A8\"><\/span>\u542f\u52a8<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e00\u6b21\u914d\u7f6e\u9700\u8981\u5728 Master \u8282\u70b9\u4e0a\u624b\u52a8\u5f00\u542f\u865a\u62dfIP<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/sbin\/ifconfig eth0:1 192.168.1.100\/24<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmanager \u8282\u70b9\u4e0a\u6d4b\u8bd5 ssh\n\u5728 manager \u8282\u70b9\u4e0a\u6d4b\u8bd5 ssh \u65e0\u5bc6\u7801\u8ba4\u8bc1\uff0c\u5982\u679c\u6b63\u5e38\u6700\u540e\u4f1a\u8f93\u51fa successfully\uff0c\u5982\u4e0b\u6240\u793a\u3002\nmasterha_check_ssh -conf=\/etc\/masterha\/app1.cnf\n\n\u5728 manager \u8282\u70b9\u4e0a\u6d4b\u8bd5 mysql \u4e3b\u4ece\u8fde\u63a5\u60c5\u51b5\uff0c\u6700\u540e\u51fa\u73b0 MySQL Replication Health is OK \u5b57\u6837\u8bf4\u660e\u6b63\u5e38\u3002\u5982\u4e0b\u6240\u793a\u3002\nmasterha_check_repl -conf=\/etc\/masterha\/app1.cnf\n\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%9C%A8_manager_%E8%8A%82%E7%82%B9%E4%B8%8A%E5%90%AF%E5%8A%A8_MHA\"><\/span>\u5728 manager \u8282\u70b9\u4e0a\u542f\u52a8 MHA<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnohup masterha_manager --conf=\/etc\/masterha\/app1.cnf --remove_dead_master_conf --ignore_last_failover &amp;lt; \/dev\/null &gt; \/var\/log\/masterha\/app1\/manager.log 2&gt;&amp;amp;1 &amp;amp;\n\n#\u751f\u4ea7\u4e2djava \u670d\u52a1\u542f\u52a8\u7684\u65b9\u5f0f\nnohup  java -jar  \u5fae\u670d\u52a1\u540d\u79f0-\uff08 war jar\uff09   &amp;amp;   \n\n----------------------------------------------------------------------------------------------------------\n--remove_dead_master_conf\uff1a\u8be5\u53c2\u6570\u4ee3\u8868\u5f53\u53d1\u751f\u4e3b\u4ece\u5207\u6362\u540e\uff0c\u8001\u7684\u4e3b\u5e93\u7684 ip \u5c06\u4f1a\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u79fb\u9664\u3002\n--manger_log\uff1a\u65e5\u5fd7\u5b58\u653e\u4f4d\u7f6e\u3002\n--ignore_last_failover\uff1a\u5728\u7f3a\u7701\u60c5\u51b5\u4e0b\uff0c\u5982\u679c MHA \u68c0\u6d4b\u5230\u8fde\u7eed\u53d1\u751f\u5b95\u673a\uff0c\u4e14\u4e24\u6b21\u5b95\u673a\u95f4\u9694\u4e0d\u8db3 8 \u5c0f\u65f6\u7684\u8bdd\uff0c\u5219\u4e0d\u4f1a\u8fdb\u884c Failover\uff0c \u4e4b\u6240\u4ee5\u8fd9\u6837\u9650\u5236\u662f\u4e3a\u4e86\u907f\u514d ping-pong \u6548\u5e94\u3002\u8be5\u53c2\u6570\u4ee3\u8868\u5ffd\u7565\u4e0a\u6b21 MHA \u89e6\u53d1\u5207\u6362\u4ea7\u751f\u7684\u6587\u4ef6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cMHA \u53d1\u751f\u5207\u6362\u540e\u4f1a\u5728\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u8bbe\u7f6e\u7684\u65e5\u5fd7app1.failover.complete\u6587\u4ef6\uff0c\u4e0b\u6b21\u518d\u6b21\u5207\u6362\u7684\u65f6\u5019\u5982\u679c\u53d1\u73b0\u8be5\u76ee\u5f55\u4e0b\u5b58\u5728\u8be5\u6587\u4ef6\u5c06\u4e0d\u5141\u8bb8\u89e6\u53d1\u5207\u6362\uff0c\u9664\u975e\u5728\u7b2c\u4e00\u6b21\u5207\u6362\u540e\u6536\u5230\u5220\u9664\u8be5\u6587\u4ef6\uff0c\u4e3a\u4e86\u65b9\u4fbf\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u4e3a--ignore_last_failover\u3002\n----------------------------------------------------------------------------------------------------------\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%9F%A5%E7%9C%8B%E7%8A%B6%E6%80%81\"><\/span>\u67e5\u770b\u72b6\u6001<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmasterha_check_status --conf=\/etc\/masterha\/app1.cnf\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/\u82e5\u8981\u5173\u95ed manager \u670d\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u3002\nmasterha_stop --conf=\/etc\/masterha\/app1.cnf\n\u6216\u8005\u53ef\u4ee5\u76f4\u63a5\u91c7\u7528 kill \u8fdb\u7a0b ID \u7684\u65b9\u5f0f\u5173\u95ed\u3002\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e9f\u8bdd\u5c11\u8bf4\uff0c\u76f4\u63a5\u5f00\u59cb\uff01 \u5b9e\u9a8c\u73af\u5883 MHA manager \u8282\u70b9\u670d\u52a1\u5668\uff1aCentOS7.6(64 \u4f4d) manag<\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"https:\/\/www.mooloco.com\/?p=171\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-main_contents"],"_links":{"self":[{"href":"https:\/\/www.mooloco.com\/index.php?rest_route=\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooloco.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooloco.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooloco.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooloco.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=171"}],"version-history":[{"count":2,"href":"https:\/\/www.mooloco.com\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/www.mooloco.com\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/www.mooloco.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooloco.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooloco.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}