{"id":1571,"date":"2026-07-21T23:13:44","date_gmt":"2026-07-21T15:13:44","guid":{"rendered":"https:\/\/www.loongdi.net\/blog\/?p=1571"},"modified":"2026-07-21T23:13:44","modified_gmt":"2026-07-21T15:13:44","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%8f%af%e7%94%a8%e6%80%a7%ef%bc%9alinux%e7%b3%bb%e7%bb%9f%e9%85%8d%e7%bd%ae%e5%92%8c%e9%83%a8%e7%bd%b2%e7%ae%80%e5%8d%95%e7%9a%84ha%e7%8e%af%e5%a2%83","status":"publish","type":"post","link":"https:\/\/www.loongdi.net\/blog\/1571.html","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\uff1aLinux\u7cfb\u7edf\u914d\u7f6e\u548c\u90e8\u7f72\u7b80\u5355\u7684HA\u73af\u5883"},"content":{"rendered":"<p><p>\u5728\u5f53\u4eca\u5feb\u901f\u53d1\u5c55\u7684\u6280\u672f\u73af\u5883\u4e2d\uff0c\u9ad8\u53ef\u7528\u6027\uff08High Availability\uff0c\u7b80\u79f0HA\uff09\u5df2\u7ecf\u6210\u4e3a\u786e\u4fdd\u7cfb\u7edf\u548c\u5e94\u7528\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u7684\u5173\u952e\u56e0\u7d20\u3002\u672c\u6587\u5c06\u57fa\u4e8e\u5b9e\u64cd\u5bfc\u5411\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e2d\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\uff0c\u901a\u8fc7\u914d\u7f6e\u548c\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684HA\u73af\u5883\uff0c\u89e3\u51b3\u5355\u70b9\u6545\u969c\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u64cd\u4f5c\u524d\u7684\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u6709\u4ee5\u4e0b\u51c6\u5907\u5de5\u4f5c\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e24\u53f0\u914d\u7f6e\u76f8\u540c\u7684Linux\u670d\u52a1\u5668\u3002<\/li>\n<li>\u81f3\u5c11\u4e00\u4e2a\u9759\u6001IP\u5730\u5740\u3002<\/li>\n<li>\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\uff0c\u5982MySQL\u3001Nginx\u7b49\u3002<\/li>\n<li>\u914d\u7f6eSSH\u514d\u5bc6\u7801\u767b\u5f55\u3002<\/li>\n<\/ul>\n<p><h2>\u4efb\u52a1\uff1a\u90e8\u7f72\u9ad8\u53ef\u7528\u6027MySQL\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u914d\u7f6e\u4e00\u4e2a\u57fa\u4e8eMariaDB\uff08MySQL\u7684\u4e00\u4e2a\u5206\u652f\uff09\u7684\u9ad8\u53ef\u7528\u6027\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u6b65\u9aa41\uff1a\u5b89\u88c5MariaDB<\/h3>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.loongdi.net\/blog\/wp-content\/uploads\/2026\/03\/L995rR.jpg\" alt=\"\u5982\u4f55\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\uff1aLinux\u7cfb\u7edf\u914d\u7f6e\u548c\u90e8\u7f72\u7b80\u5355\u7684HA\u73af\u5883\" title=\"\u5982\u4f55\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\uff1aLinux\u7cfb\u7edf\u914d\u7f6e\u548c\u90e8\u7f72\u7b80\u5355\u7684HA\u73af\u5883\"><\/p>\n<p><p>\u5728\u4e24\u53f0\u670d\u52a1\u5668\u4e0a\u5b89\u88c5MariaDB\uff1a<\/p>\n<\/p>\n<p><pre><code>sudo apt-get update<\/p>\r\n<p>sudo apt-get install mariadb-server<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa42\uff1a\u914d\u7f6eMariaDB\u4e3b\u4ece\u590d\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u4e3b\u670d\u52a1\u5668\uff08Master\uff09\u4e0a\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<p><pre><code>sudo vi \/etc\/mysql\/my.cnf<\/code><\/pre>\n<\/li>\n<li>\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p>    ini<\/p>\n<p>    [mysqld]<\/p>\n<p>    server-id=1<\/p>\n<p>    log-bin=mariadb-bin<\/p>\n<p>    binlog-format=ROW<\/p>\n<p>    expire_logs_days=7<\/p>\n<p>    innodb_flush_log_at_trx_commit=1<\/p>\n<p>    sync_binlog=1<\/p>\n<p>    read_only=0<\/p>\n<\/li>\n<li>\u91cd\u542fMySQL\u670d\u52a1\uff1a<\/p>\n<p><pre><code>sudo systemctl restart mariadb<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><p>\u5728\u4ece\u670d\u52a1\u5668\uff08Slave\uff09\u4e0a\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<p><pre><code>sudo vi \/etc\/mysql\/my.cnf<\/code><\/pre>\n<\/li>\n<li>\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p>    ini<\/p>\n<p>    [mysqld]<\/p>\n<p>    server-id=2<\/p>\n<p>    read_only=1<\/p>\n<\/li>\n<li>\u91cd\u542fMySQL\u670d\u52a1\uff1a<\/p>\n<p><pre><code>sudo systemctl restart mariadb<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><h3>\u6b65\u9aa43\uff1a\u540c\u6b65\u4e3b\u4ece\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u4ece\u670d\u52a1\u5668\u4e0a\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u4f7f\u4ece\u670d\u52a1\u5668\u4e0e\u4e3b\u670d\u52a1\u5668\u6570\u636e\u540c\u6b65\uff1a<\/p>\n<\/p>\n<p><pre><code>sudo mysql -u root -p<\/p>\r\n<p>mysql> STOP SLAVE;<\/p>\r\n<p>mysql> CHANGE MASTER TO MASTER_HOST='\u4e3b\u670d\u52a1\u5668IP', MASTER_USER='repl_user', MASTER_PASSWORD='repl_password', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=4;<\/p>\r\n<p>mysql> START SLAVE;<\/p>\r\n<p>mysql> quit;<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa44\uff1a\u914d\u7f6e\u6545\u969c\u8f6c\u79fb<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Keepalived\u6216Heartbeat\u5b9e\u73b0\u6545\u969c\u8f6c\u79fb\u3002\u8fd9\u91cc\u4ee5Keepalived\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5Keepalived\uff1a<\/p>\n<p><pre><code>sudo apt-get install keepalived<\/code><\/pre>\n<\/li>\n<li>\u914d\u7f6eKeepalived\uff1a<\/p>\n<p>    ini<\/p>\n<p>    ! Configuration File for keepalived<\/p>\n<p>    global_defs {<\/p>\n<p>        notification_email      email@example.com<\/p>\n<p>        notification_email_from test@example.com<\/p>\n<p>        smtp_server             localhost<\/p>\n<p>        smtp_connect_timeout    30<\/p>\n<p>        router_id LVS_DEVEL<\/p>\n<p>    }<\/p>\n<p>    vrrp_instance VI_1 {<\/p>\n<p>        state MASTER<\/p>\n<p>        interface eth0<\/p>\n<p>        virtual_router_id 51<\/p>\n<p>        priority 90<\/p>\n<p>        advert_int 1<\/p>\n<p>        authentication {<\/p>\n<p>            auth_type PASS<\/p>\n<p>            auth_pass 1111<\/p>\n<p>        }<\/p>\n<p>        virtual_ipaddress {<\/p>\n<p>            192.168.1.100\/24 dev eth0 label eth0:0<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<\/li>\n<li>\u542f\u52a8Keepalived\uff1a<\/p>\n<p><pre><code>sudo systemctl start keepalived<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><h3>\u6b65\u9aa45\uff1a\u6d4b\u8bd5\u9ad8\u53ef\u7528\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u4e3b\u670d\u52a1\u5668\u4e0a\u505c\u6b62MySQL\u670d\u52a1\uff0c\u7136\u540e\u5c1d\u8bd5\u8bbf\u95ee\u6570\u636e\u5e93\u3002\u5982\u679c\u4e00\u5207\u914d\u7f6e\u6b63\u786e\uff0c\u5e94\u8be5\u80fd\u591f\u901a\u8fc7\u4ece\u670d\u52a1\u5668\u8bbf\u95ee\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code>sudo systemctl stop mariadb<\/p>\n<p>ping 192.168.1.100<\/p>\n<p>mysql -h 192.168.1.100 -u your_username -p<\/p>\n<p><h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u4e3b\u4ece\u590d\u5236\u7684\u7528\u6237\u548c\u5bc6\u7801\u6b63\u786e\u3002<\/li>\n<li>\u5728\u6545\u969c\u8f6c\u79fb\u8fc7\u7a0b\u4e2d\uff0c\u8bf7\u786e\u4fddKeepalived\u914d\u7f6e\u6b63\u786e\u3002<\/li>\n<li>\u5b9a\u671f\u68c0\u67e5\u65e5\u5fd7\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u7a33\u5b9a\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u5df2\u7ecf\u6210\u529f\u90e8\u7f72\u4e86\u4e00\u4e2a\u9ad8\u53ef\u7528\u6027MySQL\u6570\u636e\u5e93\u73af\u5883\u3002\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u90e8\u7f72\u65f6\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u914d\u7f6e\u548c\u4f18\u5316\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f53\u4eca\u5feb\u901f\u53d1\u5c55\u7684\u6280\u672f\u73af\u5883\u4e2d\uff0c\u9ad8\u53ef\u7528\u6027\uff08High Availability\uff0c\u7b80\u79f0HA\uff09\u5df2\u7ecf\u6210\u4e3a\u786e\u4fdd\u7cfb\u7edf\u548c\u5e94\u7528\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u7684\u5173\u952e\u56e0\u7d20\u3002\u672c\u6587\u5c06\u57fa\u4e8e\u5b9e\u64cd\u5bfc\u5411\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e2d\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\uff0c\u901a\u8fc7\u914d\u7f6e\u548c\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684HA\u73af\u5883\uff0c\u89e3\u51b3\u5355\u70b9\u6545\u969c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":37,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"topic":[],"class_list":["post-1571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hw"],"_links":{"self":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/1571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/comments?post=1571"}],"version-history":[{"count":1,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/1571\/revisions"}],"predecessor-version":[{"id":1572,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/posts\/1571\/revisions\/1572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/media\/37"}],"wp:attachment":[{"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/media?parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/categories?post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/tags?post=1571"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.loongdi.net\/blog\/wp-json\/wp\/v2\/topic?post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}