{"id":23963,"date":"2024-11-21T08:31:04","date_gmt":"2024-11-21T00:31:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23963\/"},"modified":"2024-11-21T08:31:04","modified_gmt":"2024-11-21T00:31:04","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8shardig%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23963\/","title":{"rendered":"\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Shardig\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u5728\u73b0\u4ee3web\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6570\u636e\u91cf\u901a\u5e38\u5f88\u5927\uff0c\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u60c5\u51b5\u5e76\u63d0\u5347\u6570\u636e\u5e93\u6027\u80fd\uff0c\u901a\u5e38\u4f1a\u91c7\u7528\u5206\u5e93\u5206\u8868\u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u7ba1\u7406\u3002sharding\u6570\u636e\u5e93\u5219\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6563\u5728\u591a\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u5e93\u96c6\u7fa4\u4e0a\u8fdb\u884c\u7ba1\u7406\uff0c\u4ee5\u6b64\u5b9e\u73b0\u6570\u636e\u7684\u9ad8\u53ef\u7528\u6027\u548c\u6027\u80fd\u63d0\u5347\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u57286\u4e2d\u4f7f\u7528sharding\u6570\u636e\u5e93\u3002<\/p>\n<ol>\n<li>\u914d\u7f6eSharding\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5728config\u6587\u4ef6\u5939\u4e0b\u65b0\u5efa\u4e00\u4e2adatabase.php\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u4e8e\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728\u8be5\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff0c\u6bcf\u4e2a\u8fde\u63a5\u5bf9\u5e94\u4e00\u4e2aSharding\u6570\u636e\u5e93\u96c6\u7fa4\u3002<\/p>\n<p>\u4ee5\u4e24\u4e2a\u6570\u636e\u5e93\u96c6\u7fa4\u7684\u4f8b\u5b50\u6765\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n<pre>return [\n    \/\/ \u4e3b\u5e93\u8fde\u63a5\n    'main' =&amp;gt; [\n        'type' =&amp;gt; 'mysql',\n        'hostname' =&amp;gt; '127.0.0.1',\n        'database' =&amp;gt; 'db_main',\n        'username' =&amp;gt; 'root',\n        'password' =&amp;gt; '',\n        'charset' =&amp;gt; 'utf8mb4',\n        'prefix' =&amp;gt; '',\n        'debug' =&amp;gt; true,\n    ],\n    \/\/ \u5206\u5e93\u8fde\u63a5\n    'sharding' =&amp;gt; [\n        'type' =&amp;gt; 'mysql',\n        'hostname' =&amp;gt; '127.0.0.1',\n        'database' =&amp;gt; 'db_sharding',\n        'username' =&amp;gt; 'root',\n        'password' =&amp;gt; '',\n        'charset' =&amp;gt; 'utf8mb4',\n        'prefix' =&amp;gt; '',\n        'debug' =&amp;gt; true,\n        \/\/ \u5206\u5e93\u5206\u8868\u89c4\u5219\n        'shard' =&amp;gt; [\n            'type' =&amp;gt; 'column',\n            'column' =&amp;gt; 'id',\n            'function' =&amp;gt; function($value) {\n                return 'db_' . ($value % 4 + 1);\n            },\n        ],\n    ]\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0cmain\u4e3a\u4e3b\u5e93\u8fde\u63a5\u914d\u7f6e\uff0csharding\u4e3a\u5206\u5e93\u914d\u7f6e\u3002\u5176\u4e2d\uff0csharding\u8fde\u63a5\u4e2d\u6307\u5b9a\u4e86shard\u53c2\u6570\uff0c\u5b83\u5b9a\u4e49\u4e86\u5206\u5e93\u5206\u8868\u7684\u89c4\u5219\u3002\u8fd9\u91cc\u91c7\u7528\u4e86column\u5206\u5e93\u65b9\u5f0f\uff0c\u4ee5id\u5217\u4e3a\u5206\u5e93\u4f9d\u636e\u3002function\u5b9a\u4e49\u4e86\u5177\u4f53\u7684\u5206\u5e93\u903b\u8f91\uff0c\u6839\u636eid\u7684\u503c\u5c06\u6570\u636e\u5206\u6563\u5230\u56db\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<ol>\n<li>\u5b9e\u4f8b\u5316Sharding\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u4ee3\u7801\u4e2d\u9700\u8981\u5b9e\u4f8b\u5316Sharding\u6570\u636e\u5e93\u8fde\u63a5\u3002\u901a\u5e38\uff0c\u9700\u8981\u501f\u52a9Db\u7c7b\u5b8c\u6210\u6b64\u4efb\u52a1\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>use thinkDb;\n\n\/\/ \u5b9e\u4f8b\u5316Sharding\u8fde\u63a5\nDb::connect('sharding')-&amp;gt;query('SELECT * FROM my_table');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cDb::connect(&#8216;sharding&#8217;)\u62ff\u5230\u7684\u5c31\u662fdatabase.php\u4e2dsharding\u914d\u7f6e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528Sharding\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p>\u6709\u4e86\u4ee5\u4e0a\u7684\u914d\u7f6e\u548c\u51c6\u5907\uff0cSharding\u6570\u636e\u5e93\u7684\u4f7f\u7528\u5c31\u548c\u666e\u901a\u6570\u636e\u5e93\u4e00\u6837\u4e86\uff0c\u53ea\u662f\u9700\u8981\u6ce8\u610f\u4f7f\u7528\u6b63\u786e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5373\u53ef\u3002<\/p>\n<pre>use thinkDb;\n\n\/\/ \u4f7f\u7528Sharding\u8fde\u63a5\u67e5\u8be2my_table\u8868\u7684\u6570\u636e\nDb::connect('sharding')-&amp;gt;table('my_table')-&amp;gt;select();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u7136\uff0c\u7531\u4e8e\u6570\u636e\u662f\u5206\u5e03\u5728\u591a\u4e2a\u6570\u636e\u5e93\u4e2d\u7684\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u8de8\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u65f6\uff0c\u9700\u8981\u6253\u5f00\u8de8\u6570\u636e\u5e93\u64cd\u4f5c\u652f\u6301\u3002\u5728config\u6587\u4ef6\u5939\u4e0b\u7684database.php\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5f00\u542f\u6b64\u9009\u9879\u3002<\/p>\n<pre>return [\n    'connections' =&amp;gt; [\n        \/\/ ...\n        \/\/ \u5f00\u542f\u8de8\u5e93\u64cd\u4f5c\u652f\u6301\n        'cross_db' =&amp;gt; true,\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528Sharding\u6570\u636e\u5e93\u7684\u7b80\u5355\u4ecb\u7ecd\u3002\u901a\u8fc7\u914d\u7f6e\u548c\u4ee3\u7801\u7684\u7ed3\u5408\uff0c\u53ef\u4ee5\u8ba9\u5e94\u7528\u80fd\u591f\u8f7b\u677e\u5730\u7ba1\u7406\u548c\u4f7f\u7528Sharding\u6570\u636e\u5e93\uff0c\u63d0\u5347\u6570\u636e\u7ba1\u7406\u7684\u6548\u7387\u548c\u53ef\u9760\u6027\uff0c\u4e3a\u5e94\u7528\u7684\u53d1\u5c55\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Shardig\u6570\u636e\u5e93\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u73b0\u4ee3web\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6570\u636e\u91cf\u901a\u5e38\u5f88\u5927\uff0c\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u60c5\u51b5\u5e76\u63d0\u5347\u6570\u636e\u5e93\u6027\u80fd\uff0c\u901a\u5e38\u4f1a\u91c7\u7528\u5206\u5e93\u5206\u8868\u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u7ba1\u7406\u3002sharding\u6570\u636e\u5e93\u5219\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6563\u5728\u591a\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u5e93\u96c6\u7fa4\u4e0a\u8fdb\u884c\u7ba1\u7406\uff0c\u4ee5\u6b64\u5b9e\u73b0\u6570\u636e\u7684\u9ad8\u53ef\u7528\u6027\u548c\u6027\u80fd\u63d0\u5347\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u57286\u4e2d\u4f7f\u7528sharding\u6570\u636e\u5e93\u3002 \u914d\u7f6eSharding\u6570\u636e\u5e93\u8fde\u63a5 \u9996\u5148\uff0c\u9700\u8981\u5728config\u6587\u4ef6\u5939\u4e0b\u65b0\u5efa\u4e00\u4e2adatabase.php\u914d\u7f6e\u6587\u4ef6\uff0c\u7528\u4e8e\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728\u8be5\u6587\u4ef6\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff0c\u6bcf\u4e2a\u8fde\u63a5\u5bf9\u5e94\u4e00\u4e2aSharding\u6570\u636e\u5e93\u96c6\u7fa4\u3002 \u4ee5\u4e24\u4e2a\u6570\u636e\u5e93\u96c6\u7fa4\u7684\u4f8b\u5b50\u6765\u8fdb\u884c\u8bf4\u660e\uff1a return [ \/\/ \u4e3b\u5e93\u8fde\u63a5 &#8216;main&#8217; =&amp;gt; [ &#8216;type&#8217; =&amp;gt; &#8216;mysql&#8217;, &#8216;hostname&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;database&#8217; =&amp;gt; &#8216;db_main&#8217;, &#8216;username&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; &#8221;, &#8216;charset&#8217; =&amp;gt; &#8216;utf8mb4&#8217;, &#8216;prefix&#8217; =&amp;gt; &#8221;, &#8216;debug&#8217; =&amp;gt; true, ], \/\/ \u5206\u5e93\u8fde\u63a5 &#8216;sharding&#8217; =&amp;gt; [ &#8216;type&#8217; =&amp;gt; &#8216;mysql&#8217;, &#8216;hostname&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;database&#8217; =&amp;gt; &#8216;db_sharding&#8217;, &#8216;username&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23963","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23963"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23963\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}