{"id":25006,"date":"2024-11-21T14:48:29","date_gmt":"2024-11-21T06:48:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25006\/"},"modified":"2024-11-21T14:48:29","modified_gmt":"2024-11-21T06:48:29","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8workerman%e4%b8%ad%e4%bd%bf%e7%94%a8mysql%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%ad%98%e5%82%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25006\/","title":{"rendered":"\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169934539157640.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8<\/p>\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5f02\u6b65PHP Socket\u6846\u67b6\uff0cWorkerman\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u670d\u52a1\u5668\u7684\u5f00\u53d1\u3002\u5728\u8bb8\u591a\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u9700\u8981\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5MySQL\u6269\u5c55<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86MySQL\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5MySQL\u6269\u5c55\uff1a<\/p>\n<pre>$ pecl install mysql<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u5df2\u7ecf\u5b89\u88c5\u4e86MySQL\u6269\u5c55\uff0c\u5219\u53ef\u4ee5\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002<\/p>\n<p>\u4e8c\u3001\u5efa\u7acbMySQL\u8fde\u63a5<\/p>\n<p>\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u4e0eMySQL\u7684\u8fde\u63a5\u3002\u5728Workerman\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6765\u5efa\u7acbMySQL\u8fde\u63a5\uff1a<\/p>\n<pre>&lt;?php require_once __DIR__ . '\/Workerman\/Autoloader.php';\n\nuse WorkermanWorker;\nuse WorkermanMySQLConnection;\n\n$worker = new Worker();\n\n$worker-&gt;onWorkerStart = function() {\n    $GLOBALS['db'] = new Connection('host', 'username', 'password', 'database');\n};\n\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5f15\u5165\u4e86Workerman\u6846\u67b6\u7684Autoloader\uff0c\u5e76\u58f0\u660e\u4e86\u4e00\u4e2aWorker\u5bf9\u8c61\u3002\u5728Worker\u5bf9\u8c61\u7684onWorkerStart\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2aMySQL\u8fde\u63a5\uff0c\u4f7f\u7528\u6307\u5b9a\u7684\u4e3b\u673a\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u3002\u5c06\u8fde\u63a5\u5bf9\u8c61\u5b58\u50a8\u5728\u5168\u5c40\u53d8\u91cf$GLOBALS[&#8216;db&#8217;]\u4e2d\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u4e09\u3001\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5<\/p>\n<p>\u5728\u5efa\u7acb\u4e86MySQL\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528MySQL\u8fde\u63a5\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5\u4e86\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>&lt;?php use WorkermanWorker;\nuse WorkermanMySQLConnection;\n\n$worker = new Worker();\n\n$worker-&gt;onWorkerStart = function() {\n    $GLOBALS['db'] = new Connection('host', 'username', 'password', 'database');\n};\n\n$worker-&amp;gt;onMessage = function($connection, $data) {\n    $res = $GLOBALS['db']-&amp;gt;query('SELECT * FROM users');\n    if (!$res) {\n        $connection-&amp;gt;send('\u67e5\u8be2\u5931\u8d25');\n    } else {\n        $connection-&amp;gt;send(json_encode($res));\n    }\n};\n\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728Worker\u5bf9\u8c61\u7684onMessage\u56de\u8c03\u51fd\u6570\u4e2d\u6267\u884c\u4e86\u4e00\u4e2a\u67e5\u8be2\u8bed\u53e5\uff0c\u67e5\u8be2\u4e86\u540d\u4e3ausers\u7684\u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002\u5982\u679c\u67e5\u8be2\u5931\u8d25\uff0c\u5219\u8fd4\u56de&#8221;\u67e5\u8be2\u5931\u8d25&#8221;\uff1b\u5426\u5219\u5c06\u67e5\u8be2\u7ed3\u679c\u4f7f\u7528json_encode\u51fd\u6570\u8fdb\u884c\u5e8f\u5217\u5316\uff0c\u5e76\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\uff0c\u6267\u884c\u5404\u79cdSQL\u8bed\u53e5\uff0c\u5982\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u56db\u3001\u8fde\u63a5\u6c60\u4f18\u5316<\/p>\n<p>\u5728\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u7528\u5230\u8fde\u63a5\u6c60\u6765\u4f18\u5316\u6570\u636e\u5e93\u8fde\u63a5\u3002Workerman\u6846\u67b6\u63d0\u4f9b\u4e86MySQL\u8fde\u63a5\u6c60\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u590d\u7528MySQL\u8fde\u63a5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u8fde\u63a5\u6c60\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php use WorkermanWorker;\nuse WorkermanMySQLConnection;\n\n$worker = new Worker();\n\n$worker-&gt;onWorkerStart = function() {\n    $GLOBALS['db'] = new WorkermanMySQLPool('host', 'username', 'password', 'database');\n};\n\n$worker-&amp;gt;onMessage = function($connection, $data) {\n    $GLOBALS['db']-&amp;gt;pop(function($db) use ($connection) {\n        $res = $db-&amp;gt;query('SELECT * FROM users');\n        if (!$res) {\n            $connection-&amp;gt;send('\u67e5\u8be2\u5931\u8d25');\n        } else {\n            $connection-&amp;gt;send(json_encode($res));\n        }\n        $db-&amp;gt;push($db);\n    });\n};\n\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Workerman\u6846\u67b6\u63d0\u4f9b\u7684\u8fde\u63a5\u6c60\u7c7bWorkermanMySQLPool\u6765\u521b\u5efa\u8fde\u63a5\u6c60\u5bf9\u8c61\u3002\u5728onMessage\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528$GLOBALS[&#8216;db&#8217;]-&gt;pop\u65b9\u6cd5\u4ece\u8fde\u63a5\u6c60\u4e2d\u83b7\u53d6\u4e00\u4e2a\u8fde\u63a5\uff0c\u7136\u540e\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002\u6700\u540e\u4f7f\u7528$db-&gt;push\u65b9\u6cd5\u5c06\u8fde\u63a5\u8fd4\u56de\u7ed9\u8fde\u63a5\u6c60\uff0c\u4ee5\u4f9b\u5176\u4ed6\u8bf7\u6c42\u4f7f\u7528\u3002<\/p>\n<p>\u4e94\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u3002\u9996\u5148\u9700\u8981\u901a\u8fc7pecl install \u547d\u4ee4\u5b89\u88c5MySQL\u6269\u5c55\uff0c\u7136\u540e\u5efa\u7acb\u4e0eMySQL\u7684\u8fde\u63a5\uff0c\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5\u3002\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u4f18\u5316\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u80fd\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u60a8\u5728\u4f7f\u7528Workerman\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u65f6\u987a\u5229\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\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>\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8 \u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5f02\u6b65PHP Socket\u6846\u67b6\uff0cWorkerman\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u670d\u52a1\u5668\u7684\u5f00\u53d1\u3002\u5728\u8bb8\u591a\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u9700\u8981\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5b89\u88c5MySQL\u6269\u5c55 \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86MySQL\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5MySQL\u6269\u5c55\uff1a $ pecl install mysql \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u5df2\u7ecf\u5b89\u88c5\u4e86MySQL\u6269\u5c55\uff0c\u5219\u53ef\u4ee5\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002 \u4e8c\u3001\u5efa\u7acbMySQL\u8fde\u63a5 \u4f7f\u7528MySQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u4e0eMySQL\u7684\u8fde\u63a5\u3002\u5728Workerman\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6765\u5efa\u7acbMySQL\u8fde\u63a5\uff1a &lt;?php require_once __DIR__ . &#8216;\/Workerman\/Autoloader.php&#8217;; use WorkermanWorker; use WorkermanMySQLConnection; $worker = new Worker(); $worker-&gt;onWorkerStart = function() { $GLOBALS[&#8216;db&#8217;] = new Connection(&#8216;host&#8217;, &#8216;username&#8217;, &#8216;password&#8217;, &#8216;database&#8217;); }; Worker::runAll(); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5f15\u5165\u4e86Workerman\u6846\u67b6\u7684Autoloader\uff0c\u5e76\u58f0\u660e\u4e86\u4e00\u4e2aWorker\u5bf9\u8c61\u3002\u5728Worker\u5bf9\u8c61\u7684onWorkerStart\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2aMySQL\u8fde\u63a5\uff0c\u4f7f\u7528\u6307\u5b9a\u7684\u4e3b\u673a\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u548c\u6570\u636e\u5e93\u540d\u3002\u5c06\u8fde\u63a5\u5bf9\u8c61\u5b58\u50a8\u5728\u5168\u5c40\u53d8\u91cf$GLOBALS[&#8216;db&#8217;]\u4e2d\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002 \u4e09\u3001\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5 \u5728\u5efa\u7acb\u4e86MySQL\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528MySQL\u8fde\u63a5\u5bf9\u8c61\u6765\u6267\u884cSQL\u67e5\u8be2\u8bed\u53e5\u4e86\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a &lt;?php use WorkermanWorker; use WorkermanMySQLConnection; $worker = new Worker(); $worker-&gt;onWorkerStart = function() [&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-25006","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25006","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=25006"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25006\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}