{"id":23251,"date":"2024-11-21T15:44:36","date_gmt":"2024-11-21T07:44:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23251\/"},"modified":"2024-11-21T15:44:36","modified_gmt":"2024-11-21T07:44:36","slug":"thinkphp6%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%88%86%e5%b8%83%e5%bc%8f%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23251\/","title":{"rendered":"ThinkPHP6\u4e2d\u5982\u4f55\u5b9e\u73b0\u5206\u5e03\u5f0f\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u53d1\u5c55\u7684\u65e5\u76ca\u8fc5\u901f\uff0c\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u91cf\u4e5f\u8d8a\u6765\u8d8a\u5927\uff0c\u5355\u4e00\u670d\u52a1\u5668\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5206\u5e03\u5f0f\u67b6\u6784\u5df2\u7ecf\u6210\u4e3a\u4e86\u89e3\u51b3\u65b9\u6848\u4e4b\u4e00\u3002thinkphp\u4f5c\u4e3a\u4e00\u6b3e\u9887\u53d7\u6b22\u8fce\u7684php\u6846\u67b6\uff0c\u81ea\u7136\u4e5f\u9700\u8981\u80fd\u591f\u652f\u6301\u5206\u5e03\u5f0f\u64cd\u4f5c\u624d\u80fd\u9002\u5e94\u73b0\u4ee3\u5316\u7684\u53d1\u5c55\u9700\u6c42\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5728thinkphp6\u4e2d\u5b9e\u73b0\u5206\u5e03\u5f0f\u64cd\u4f5c\u5462\uff1f<\/p>\n<p>1.\u6570\u636e\u5e93\u5206\u5e03\u5f0f<\/p>\n<p>\u6570\u636e\u5e93\u5206\u5e03\u5f0f\u662f\u5e38\u89c1\u7684\u5206\u5e03\u5f0f\u89e3\u51b3\u65b9\u6848\u4e4b\u4e00\uff0cThinkPHP6\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u652f\u6301\u3002\u9996\u5148\uff0c\u5728config\/database.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u591a\u4e2a\u6570\u636e\u5e93\uff1a<\/p>\n<pre>\/\/ \u9ed8\u8ba4\u6570\u636e\u5e93\u914d\u7f6e\n'default'     =&amp;gt; env('database.driver', 'mysql'),\n\n\/\/ \u6570\u636e\u5e93\u5217\u8868\n'connections' =&amp;gt; [\n    \/\/ \u6570\u636e\u5e931\n    'mysql1'    =&amp;gt; [\n        'type'            =&amp;gt; 'mysql',\n        'hostname'        =&amp;gt; '127.0.0.1',\n        'database'        =&amp;gt; '',\n        'username'        =&amp;gt; '',\n        'password'        =&amp;gt; '',\n        'hostport'        =&amp;gt; '',\n        'dsn'             =&amp;gt; '',\n        'params'          =&amp;gt; [],\n        'charset'         =&amp;gt; 'utf8mb4',\n        'prefix'          =&amp;gt; '',\n        'debug'           =&amp;gt; env('app_debug', false),\n        'deploy'          =&amp;gt; 0,\n        'rw_separate'     =&amp;gt; false,\n        'master_num'      =&amp;gt; 1,\n        'slave_no'        =&amp;gt; '',\n        'read_consistent' =&amp;gt; false,\n        'proxy'           =&amp;gt; '',\n    ],\n\n    \/\/ \u6570\u636e\u5e932\n    'mysql2'    =&amp;gt; [\n        \/\/ ...\n    ],\n\n    \/\/ ...\n],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728model\u7c7b\u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre>protected $connection = 'mysql1';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\uff0c\u5728\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\u5c31\u4f1a\u8fde\u63a5\u5230\u6307\u5b9a\u7684\u6570\u636e\u5e93\u4e86\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u6570\u636e\u5e93\u4e4b\u95f4\u5b9e\u73b0\u6570\u636e\u7684\u5206\u5e03\u5f0f\u5b58\u50a8\u548c\u8bbf\u95ee\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>2.\u7f13\u5b58\u5206\u5e03\u5f0f<\/p>\n<p>\u7f13\u5b58\u5206\u5e03\u5f0f\u4e5f\u662f\u8f83\u4e3a\u5e38\u89c1\u7684\u5206\u5e03\u5f0f\u65b9\u6848\u4e4b\u4e00\uff0cThinkPHP6\u4e5f\u6709\u76f8\u5e94\u7684\u7f13\u5b58\u5206\u5e03\u5f0f\u652f\u6301\u3002\u5728config\/cache.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u591a\u4e2a\u7f13\u5b58\u670d\u52a1\u5668\uff1a<\/p>\n<pre>\/\/ \u9ed8\u8ba4\u7f13\u5b58\u9a71\u52a8\n'default' =&amp;gt; env('cache.driver', 'file'),\n\n\/\/ \u7f13\u5b58\u5217\u8868\n'stores'  =&amp;gt; [\n    \/\/ \u7f13\u5b581\n    'redis1'   =&amp;gt; [\n        'type'       =&amp;gt; 'redis',\n        'hostname'   =&amp;gt; '127.0.0.1',\n        'port'       =&amp;gt; 6301,\n        'password'   =&amp;gt; '',\n        'select'     =&amp;gt; 0,\n        'timeout'    =&amp;gt; 0,\n        'expire'     =&amp;gt; 0,\n        'persistent' =&amp;gt; false,\n        'prefix'     =&amp;gt; 'tp6_',\n        'serialize'  =&amp;gt; [],\n    ],\n\n    \/\/ \u7f13\u5b582\n    'redis2'   =&amp;gt; [\n        \/\/ ...\n    ],\n\n    \/\/ ...\n],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u7f13\u5b58\u7c7b\u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre>Cache::store('redis1')-&amp;gt;set('key', 'value');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u6837\u4e5f\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7f13\u5b58\u670d\u52a1\u5668\u4e4b\u95f4\u5b9e\u73b0\u6570\u636e\u7684\u5206\u5e03\u5f0f\u5b58\u50a8\u548c\u8bbf\u95ee\u3002<\/p>\n<p>\u603b\u4e4b\uff0cThinkPHP6\u4e2d\u5b9e\u73b0\u5206\u5e03\u5f0f\u64cd\u4f5c\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u6846\u67b6\u63d0\u4f9b\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u76f8\u5e94\u7684\u4fe1\u606f\u5373\u53ef\uff0c\u800c\u4e14\u4e0d\u540c\u7684\u5206\u5e03\u5f0f\u65b9\u6848\u90fd\u6709\u76f8\u5e94\u7684\u652f\u6301\u3002\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u5b9a\u5236\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u6846\u67b6\u4e2d\u81ea\u884c\u5b9e\u73b0\u76f8\u5e94\u7684\u5206\u5e03\u5f0f\u7ec4\u4ef6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u4e2d\u5982\u4f55\u5b9e\u73b0\u5206\u5e03\u5f0f\u64cd\u4f5c\uff1f\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>\u968f\u7740\u4e92\u8054\u7f51\u53d1\u5c55\u7684\u65e5\u76ca\u8fc5\u901f\uff0c\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u91cf\u4e5f\u8d8a\u6765\u8d8a\u5927\uff0c\u5355\u4e00\u670d\u52a1\u5668\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5206\u5e03\u5f0f\u67b6\u6784\u5df2\u7ecf\u6210\u4e3a\u4e86\u89e3\u51b3\u65b9\u6848\u4e4b\u4e00\u3002thinkphp\u4f5c\u4e3a\u4e00\u6b3e\u9887\u53d7\u6b22\u8fce\u7684php\u6846\u67b6\uff0c\u81ea\u7136\u4e5f\u9700\u8981\u80fd\u591f\u652f\u6301\u5206\u5e03\u5f0f\u64cd\u4f5c\u624d\u80fd\u9002\u5e94\u73b0\u4ee3\u5316\u7684\u53d1\u5c55\u9700\u6c42\u3002\u90a3\u4e48\uff0c\u5982\u4f55\u5728thinkphp6\u4e2d\u5b9e\u73b0\u5206\u5e03\u5f0f\u64cd\u4f5c\u5462\uff1f 1.\u6570\u636e\u5e93\u5206\u5e03\u5f0f \u6570\u636e\u5e93\u5206\u5e03\u5f0f\u662f\u5e38\u89c1\u7684\u5206\u5e03\u5f0f\u89e3\u51b3\u65b9\u6848\u4e4b\u4e00\uff0cThinkPHP6\u4e5f\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u652f\u6301\u3002\u9996\u5148\uff0c\u5728config\/database.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u591a\u4e2a\u6570\u636e\u5e93\uff1a \/\/ \u9ed8\u8ba4\u6570\u636e\u5e93\u914d\u7f6e &#8216;default&#8217; =&amp;gt; env(&#8216;database.driver&#8217;, &#8216;mysql&#8217;), \/\/ \u6570\u636e\u5e93\u5217\u8868 &#8216;connections&#8217; =&amp;gt; [ \/\/ \u6570\u636e\u5e931 &#8216;mysql1&#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; &#8221;, &#8216;username&#8217; =&amp;gt; &#8221;, &#8216;password&#8217; =&amp;gt; &#8221;, &#8216;hostport&#8217; =&amp;gt; &#8221;, &#8216;dsn&#8217; =&amp;gt; &#8221;, &#8216;params&#8217; =&amp;gt; [], &#8216;charset&#8217; =&amp;gt; &#8216;utf8mb4&#8217;, &#8216;prefix&#8217; =&amp;gt; &#8221;, &#8216;debug&#8217; =&amp;gt; env(&#8216;app_debug&#8217;, false), &#8216;deploy&#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-23251","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23251","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=23251"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23251\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}