{"id":24037,"date":"2024-11-21T15:10:49","date_gmt":"2024-11-21T07:10:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24037\/"},"modified":"2024-11-21T15:10:49","modified_gmt":"2024-11-21T07:10:49","slug":"swoole%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91swoole_mysql_server","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24037\/","title":{"rendered":"Swoole\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1swoole_mysql_server"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u6025\u901f\u53d1\u5c55\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u4e86\u4e00\u4e2a\u4e0d\u53ef\u907f\u514d\u7684\u95ee\u9898\u3002\u5728\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\uff0c\u5e38\u89c4\u7684\u5355\u7ebf\u7a0b\u3001\u963b\u585e\u5f0fi\/o\u65b9\u5f0f\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u8fd9\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u800c\u6b63\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5b9e\u73b0\u5f02\u6b65\u3001\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u5f80\u5f80\u662f\u4e00\u4e2a\u74f6\u9888\u3002\u56e0\u6b64\uff0c\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_mysql_server\u662f\u4e00\u4e2a\u975e\u5e38\u503c\u5f97\u7814\u7a76\u7684\u8bdd\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684MySQL\u670d\u52a1\u5668\u3002<\/p>\n<p>Swoole\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>Swoole\u662f\u4e00\u79cdPHP\u6269\u5c55\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u3001\u5f02\u6b65\u7684\u3001\u591a\u8fdb\u7a0b\u7684\u3001\u534f\u7a0b\u5b9e\u73b0\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u80fd\u591f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u548c\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u3002Swoole\u652f\u6301\u5f02\u6b65TCP\/UDP\/Unix Socket\u901a\u4fe1\u3001\u5f02\u6b65Redis\u3001\u5f02\u6b65MySQL\u3001\u534f\u7a0b\u7b49\u7279\u6027\u3002<\/p>\n<p>Swoole\u7684\u534f\u7a0b\u5b9e\u73b0<\/p>\n<p>\u5728Swoole\u4e2d\u5b9e\u73b0\u534f\u7a0b\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528Swoole\u63d0\u4f9b\u7684\u534f\u7a0b\u5de5\u5177\u548c\u6807\u51c6\u7684PHP\u534f\u7a0bAPI\u5373\u53ef\u3002Swoole\u63d0\u4f9b\u4e86\u5982\u4e0b\u7684\u534f\u7a0b\u5de5\u5177\uff1a<\/p>\n<ul>\n<li>SwooleCoroutineun()\uff1a\u542f\u52a8\u534f\u7a0b<\/li>\n<li>SwooleCoroutinecreate()\uff1a\u521b\u5efa\u534f\u7a0b<\/li>\n<li>SwooleCoroutinedefer()\uff1a\u63a8\u8fdf\u6267\u884c<\/li>\n<li>SwooleCoroutineChannel\uff1a\u534f\u7a0b\u901a\u4fe1<\/li>\n<li>SwooleCoroutineSystem\uff1a\u534f\u7a0b\u6587\u4ef6\u7cfb\u7edf<\/li>\n<li>SwooleCoroutineMySQL\uff1a\u534f\u7a0bMySQL\u5ba2\u6237\u7aef<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0swoole_mysql_server<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1swoole_mysql_server\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php use SwooleCoroutineMySQL;\n\n$server = new SwooleServer('0.0.0.0', 9501, SWOOLE_BASE);\n\n$server-&gt;set([\n    'worker_num' =&amp;gt; 4,\n]);\n\n$server-&amp;gt;on('receive', function ($server, $fd, $from_id, $data) {\n    $mysql = new MySQL();\n    $mysql-&amp;gt;connect([\n        'host' =&amp;gt; '127.0.0.1',\n        'port' =&amp;gt; 3306,\n        'user' =&amp;gt; 'root',\n        'password' =&amp;gt; '123456',\n        'database' =&amp;gt; 'test',\n    ]);\n\n    $result = $mysql-&amp;gt;query('SELECT * FROM test_table');\n    $server-&amp;gt;send($fd, json_encode($result));\n});\n\n$server-&amp;gt;start();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aswoole\u670d\u52a1\u5668\uff0c\u7136\u540e\u8bbe\u7f6e\u4e864\u4e2aworker\u8fdb\u7a0b\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u6536\u5230\u5ba2\u6237\u7aef\u8bf7\u6c42\u65f6\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u534f\u7a0bMySQL\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u534f\u7a0bMySQL\u5ba2\u6237\u7aef\u5bf9\u8c61\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4e86\u67e5\u8be2\u64cd\u4f5c\u3002\u6700\u540e\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u901a\u8fc7\u670d\u52a1\u5668\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u4f7f\u7528\u534f\u7a0b\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8MySQL\u670d\u52a1\u5668\u7684\u6027\u80fd\uff0c\u540c\u65f6\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u5207\u6362\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u989d\u5916\u5f00\u9500\uff0c\u4f7f\u5f97\u670d\u52a1\u5668\u66f4\u52a0\u7684\u9ad8\u6548\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_mysql_server\u3002\u534f\u7a0b\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u5904\u7406\u5927\u91cf\u8bf7\u6c42\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u907f\u514d\u7ebf\u7a0b\u548c\u4e0a\u4e0b\u6587\u7684\u5207\u6362\uff0c\u4ece\u800c\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u6027\u80fd\u3002\u5728\u5f00\u53d1\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7a0b\u5e8f\u65f6\uff0c\u5bf9\u534f\u7a0b\u7684\u7406\u89e3\u548c\u5e94\u7528\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1_mysql_server\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\u7684\u6025\u901f\u53d1\u5c55\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u4e86\u4e00\u4e2a\u4e0d\u53ef\u907f\u514d\u7684\u95ee\u9898\u3002\u5728\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\uff0c\u5e38\u89c4\u7684\u5355\u7ebf\u7a0b\u3001\u963b\u585e\u5f0fi\/o\u65b9\u5f0f\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u8fd9\u65f6\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u800c\u6b63\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5b9e\u73b0\u5f02\u6b65\u3001\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002 \u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u5f80\u5f80\u662f\u4e00\u4e2a\u74f6\u9888\u3002\u56e0\u6b64\uff0c\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_mysql_server\u662f\u4e00\u4e2a\u975e\u5e38\u503c\u5f97\u7814\u7a76\u7684\u8bdd\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684MySQL\u670d\u52a1\u5668\u3002 Swoole\u662f\u4ec0\u4e48\uff1f Swoole\u662f\u4e00\u79cdPHP\u6269\u5c55\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u3001\u5f02\u6b65\u7684\u3001\u591a\u8fdb\u7a0b\u7684\u3001\u534f\u7a0b\u5b9e\u73b0\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u80fd\u591f\u5b9e\u73b0\u9ad8\u5e76\u53d1\u548c\u9ad8\u6027\u80fd\u7684\u670d\u52a1\u5668\u7a0b\u5e8f\u3002Swoole\u652f\u6301\u5f02\u6b65TCP\/UDP\/Unix Socket\u901a\u4fe1\u3001\u5f02\u6b65Redis\u3001\u5f02\u6b65MySQL\u3001\u534f\u7a0b\u7b49\u7279\u6027\u3002 Swoole\u7684\u534f\u7a0b\u5b9e\u73b0 \u5728Swoole\u4e2d\u5b9e\u73b0\u534f\u7a0b\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528Swoole\u63d0\u4f9b\u7684\u534f\u7a0b\u5de5\u5177\u548c\u6807\u51c6\u7684PHP\u534f\u7a0bAPI\u5373\u53ef\u3002Swoole\u63d0\u4f9b\u4e86\u5982\u4e0b\u7684\u534f\u7a0b\u5de5\u5177\uff1a SwooleCoroutineun()\uff1a\u542f\u52a8\u534f\u7a0b SwooleCoroutinecreate()\uff1a\u521b\u5efa\u534f\u7a0b SwooleCoroutinedefer()\uff1a\u63a8\u8fdf\u6267\u884c SwooleCoroutineChannel\uff1a\u534f\u7a0b\u901a\u4fe1 SwooleCoroutineSystem\uff1a\u534f\u7a0b\u6587\u4ef6\u7cfb\u7edf SwooleCoroutineMySQL\uff1a\u534f\u7a0bMySQL\u5ba2\u6237\u7aef \u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0swoole_mysql_server \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1swoole_mysql_server\u7684\u793a\u4f8b\u4ee3\u7801\uff1a &lt;?php use SwooleCoroutineMySQL; $server = new SwooleServer(&#8216;0.0.0.0&#8217;, 9501, SWOOLE_BASE); $server-&gt;set([ &#8216;worker_num&#8217; =&amp;gt; 4, ]); $server-&amp;gt;on(&#8216;receive&#8217;, function ($server, $fd, $from_id, $data) { $mysql = new MySQL(); $mysql-&amp;gt;connect([ &#8216;host&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;port&#8217; =&amp;gt; 3306, &#8216;user&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; &#8216;123456&#8217;, &#8216;database&#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-24037","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24037","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=24037"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24037\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}