{"id":170061,"date":"2024-11-06T20:49:20","date_gmt":"2024-11-06T12:49:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/170061\/"},"modified":"2024-11-06T20:49:21","modified_gmt":"2024-11-06T12:49:21","slug":"%e4%bd%a0%e9%82%84%e4%b8%8d%e7%9f%a5%e9%81%93%e6%80%8e%e9%ba%bc%e5%81%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab%e5%88%86%e9%9b%a2%e9%ba%bc%ef%bc%8c%e7%94%a8%e9%80%99%e5%80%8b%e4%b8%ad%e9%96%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/170061\/","title":{"rendered":"\u4f60\u9084\u4e0d\u77e5\u9053\u600e\u9ebc\u505a\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\u9ebc\uff0c\u7528\u9019\u500b\u4e2d\u9593\u4ef6\u8b93\u4f60\u6027\u80fd\u63d010\u500d"},"content":{"rendered":"<h1 id=\"%e4%bd%a0%e9%82%84%e4%b8%8d%e7%9f%a5%e9%81%93%e6%80%8e%e9%ba%bc%e5%81%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab%e5%88%86%e9%9b%a2%e9%ba%bc%ef%bc%8c%e7%94%a8%e9%80%99%e5%80%8b%e4%b8%ad%e9%96%93-SOlNVasWdF\">\u4f60\u9084\u4e0d\u77e5\u9053\u600e\u9ebc\u505a\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\u9ebc\uff0c\u7528\u9019\u500b\u4e2d\u9593\u4ef6\u8b93\u4f60\u6027\u80fd\u63d010\u500d<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6574\u9ad4\u8868\u73fe\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\u548c\u6578\u64da\u91cf\u7684\u64f4\u5927\uff0c\u55ae\u4e00\u6578\u64da\u5eab\u7684\u8b80\u5beb\u8ca0\u8f09\u53ef\u80fd\u6703\u6210\u70ba\u74f6\u9838\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0c\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\u6280\u8853\u61c9\u904b\u800c\u751f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904e\u4e2d\u9593\u4ef6\u5be6\u73fe\u6578\u64da\u5eab\u7684\u8b80\u5beb\u5206\u96e2\uff0c\u4e26\u63d0\u5347\u6027\u80fd\u9054\u5230\u5341\u500d\u4e4b\u591a\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab%e5%88%86%e9%9b%a2%ef%bc%9f-SOlNVasWdF\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\u662f\u4e00\u7a2e\u67b6\u69cb\u8a2d\u8a08\uff0c\u65e8\u5728\u5c07\u6578\u64da\u5eab\u7684\u8b80\u53d6\u64cd\u4f5c\u548c\u5beb\u5165\u64cd\u4f5c\u5206\u958b\u8655\u7406\u3002\u901a\u5e38\uff0c\u4e3b\u6578\u64da\u5eab\u8ca0\u8cac\u5beb\u5165\u64cd\u4f5c\uff0c\u800c\u4e00\u500b\u6216\u591a\u500b\u5f9e\u6578\u64da\u5eab\u5247\u5c08\u9580\u8655\u7406\u8b80\u53d6\u8acb\u6c42\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u4e3b\u6578\u64da\u5eab\u7684\u8ca0\u64d4\uff0c\u63d0\u9ad8\u6574\u9ad4\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e8%ae%80%e5%af%ab%e5%88%86%e9%9b%a2%ef%bc%9f-SOlNVasWdF\">\u70ba\u4ec0\u9ebc\u9700\u8981\u8b80\u5beb\u5206\u96e2\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u901a\u904e\u5c07\u8b80\u53d6\u8acb\u6c42\u5206\u6563\u5230\u591a\u500b\u5f9e\u6578\u64da\u5eab\u4e0a\uff0c\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u4e3b\u6578\u64da\u5eab\u7684\u8ca0\u8f09\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u6dfb\u52a0\u66f4\u591a\u7684\u5f9e\u6578\u64da\u5eab\u4f86\u61c9\u5c0d\u589e\u9577\u7684\u8b80\u53d6\u9700\u6c42\u3002<\/li>\n<li><strong>\u6545\u969c\u5bb9\u932f\uff1a<\/strong>\u5982\u679c\u4e3b\u6578\u64da\u5eab\u51fa\u73fe\u6545\u969c\uff0c\u5f9e\u6578\u64da\u5eab\u4ecd\u7136\u53ef\u4ee5\u63d0\u4f9b\u8b80\u53d6\u670d\u52d9\uff0c\u5f9e\u800c\u63d0\u9ad8\u7cfb\u7d71\u7684\u53ef\u7528\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab%e5%88%86%e9%9b%a2%ef%bc%9f-SOlNVasWdF\">\u5982\u4f55\u5be6\u73fe\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\uff1f<\/h2>\n<p>\u5be6\u73fe\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\u901a\u5e38\u9700\u8981\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e4%b8%bb%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab-SOlNVasWdF\">1. \u8a2d\u7f6e\u4e3b\u5f9e\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u8a2d\u7f6e\u4e00\u500b\u4e3b\u6578\u64da\u5eab\u548c\u4e00\u500b\u6216\u591a\u500b\u5f9e\u6578\u64da\u5eab\u3002\u4e3b\u6578\u64da\u5eab\u8ca0\u8cac\u6240\u6709\u7684\u5beb\u5165\u64cd\u4f5c\uff0c\u800c\u5f9e\u6578\u64da\u5eab\u5247\u7528\u65bc\u8655\u7406\u8b80\u53d6\u8acb\u6c42\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e4%b8%ad%e9%96%93%e4%bb%b6-SOlNVasWdF\">2. \u4f7f\u7528\u4e2d\u9593\u4ef6<\/h3>\n<p>\u4e2d\u9593\u4ef6\u5728\u9019\u88e1\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\u3002\u5b83\u8ca0\u8cac\u5c07\u8b80\u53d6\u8acb\u6c42\u81ea\u52d5\u5206\u767c\u5230\u5f9e\u6578\u64da\u5eab\uff0c\u4e26\u5c07\u5beb\u5165\u8acb\u6c42\u767c\u9001\u5230\u4e3b\u6578\u64da\u5eab\u3002\u5e38\u898b\u7684\u4e2d\u9593\u4ef6\u6709\uff1a<\/p>\n<ul>\n<li><strong>MySQL Proxy\uff1a<\/strong>\u9019\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u4e2d\u9593\u4ef6\uff0c\u53ef\u4ee5\u900f\u660e\u5730\u5c07\u8acb\u6c42\u8f49\u767c\u5230\u4e3b\u5f9e\u6578\u64da\u5eab\u3002<\/li>\n<li><strong>ProxySQL\uff1a<\/strong>\u9019\u662f\u4e00\u500b\u9ad8\u6027\u80fd\u7684MySQL\u4ee3\u7406\uff0c\u652f\u6301\u8907\u96dc\u7684\u8def\u7531\u898f\u5247\u548c\u8ca0\u8f09\u5747\u8861\u3002<\/li>\n<li><strong>MaxScale\uff1a<\/strong>\u9019\u662f\u4e00\u500b\u7531MariaDB\u958b\u767c\u7684\u4e2d\u9593\u4ef6\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u5eab\u7ba1\u7406\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h3 id=\"3-%e9%85%8d%e7%bd%ae%e6%87%89%e7%94%a8%e7%a8%8b%e5%ba%8f-SOlNVasWdF\">3. \u914d\u7f6e\u61c9\u7528\u7a0b\u5e8f<\/h3>\n<p>\u6700\u5f8c\uff0c\u9700\u8981\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4ee5\u4fbf\u5b83\u80fd\u5920\u6b63\u78ba\u5730\u4f7f\u7528\u4e2d\u9593\u4ef6\u9032\u884c\u8b80\u5beb\u5206\u96e2\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const db = require('some-database-library');\n\n\/\/ \u8a2d\u7f6e\u4e3b\u6578\u64da\u5eab\u9023\u63a5\nconst masterDb = db.createConnection({\n    host: 'master-db-host',\n    user: 'username',\n    password: 'password',\n    database: 'dbname'\n});\n\n\/\/ \u8a2d\u7f6e\u5f9e\u6578\u64da\u5eab\u9023\u63a5\nconst slaveDb = db.createConnection({\n    host: 'slave-db-host',\n    user: 'username',\n    password: 'password',\n    database: 'dbname'\n});\n\n\/\/ \u5be6\u73fe\u8b80\u5beb\u5206\u96e2\nfunction queryDatabase(query, isWrite) {\n    const connection = isWrite ? masterDb : slaveDb;\n    return connection.query(query);\n}\n<\/code><\/pre>\n<h2 id=\"%e6%80%a7%e8%83%bd%e6%8f%90%e5%8d%87%e7%9a%84%e5%af%a6%e9%9a%9b%e6%a1%88%e4%be%8b-SOlNVasWdF\">\u6027\u80fd\u63d0\u5347\u7684\u5be6\u969b\u6848\u4f8b<\/h2>\n<p>\u6839\u64da\u4e00\u4e9b\u5be6\u969b\u6848\u4f8b\uff0c\u901a\u904e\u5be6\u65bd\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\uff0c\u8a31\u591a\u4f01\u696d\u7684\u6027\u80fd\u63d0\u5347\u9054\u5230\u4e86\u5341\u500d\u4ee5\u4e0a\u3002\u4f8b\u5982\uff0c\u4e00\u5bb6\u96fb\u5b50\u5546\u52d9\u516c\u53f8\u5728\u5be6\u65bd\u8b80\u5beb\u5206\u96e2\u5f8c\uff0c\u5c07\u5176\u7db2\u7ad9\u7684\u97ff\u61c9\u6642\u9593\u5f9e\u539f\u4f86\u7684500\u6beb\u79d2\u964d\u4f4e\u5230\u4e8650\u6beb\u79d2\uff0c\u986f\u8457\u63d0\u5347\u4e86\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SOlNVasWdF\">\u7e3d\u7d50<\/h2>\n<p>\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6280\u8853\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u901a\u904e\u4f7f\u7528\u4e2d\u9593\u4ef6\uff0c\u4f01\u696d\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u9019\u4e00\u67b6\u69cb\uff0c\u4e26\u5728\u9762\u5c0d\u65e5\u76ca\u589e\u9577\u7684\u6578\u64da\u9700\u6c42\u6642\u4fdd\u6301\u9ad8\u6548\u904b\u884c\u3002\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6027\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u4f60\u7684\u6578\u64da\u5eab\u67b6\u69cb\uff0c\u9019\u5c07\u70ba\u4f60\u7684\u696d\u52d9\u5e36\u4f86\u66f4\u5927\u7684\u9748\u6d3b\u6027\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5229\u7528\u9019\u500b\u4e2d\u9593\u4ef6\u5be6\u73fe\u6578\u64da\u5eab\u8b80\u5beb\u5206\u96e2\uff0c\u63d0\u5347\u6027\u80fd\u905410\u500d\uff0c\u8b93\u4f60\u7684\u61c9\u7528\u66f4\u9ad8\u6548\u904b\u884c\uff01<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-170061","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/170061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=170061"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/170061\/revisions"}],"predecessor-version":[{"id":170062,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/170061\/revisions\/170062"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=170061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=170061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=170061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}