{"id":24993,"date":"2024-11-21T16:08:37","date_gmt":"2024-11-21T08:08:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24993\/"},"modified":"2024-11-21T16:08:37","modified_gmt":"2024-11-21T08:08:37","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8workerman%e4%b8%ad%e4%bd%bf%e7%94%a8postgresql%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\/24993\/","title":{"rendered":"\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169934098735674.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u7684\u53d1\u5c55\uff0c\u5bf9\u4e8e\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u7684\u9700\u6c42\u8d8a\u6765\u8d8a\u9ad8\uff0c\u800cPostgreSQL\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u4e14\u53ef\u9760\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u5907\u53d7\u5f00\u53d1\u8005\u9752\u7750\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6ePostgreSQL<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u548c\u914d\u7f6ePostgreSQL\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5PostgreSQL\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u548c\u76f8\u5e94\u7684\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u6211\u4eec\u7684\u6570\u636e\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u7528\u6237\uff0c\u5e76\u4e3a\u5176\u5206\u914d\u9002\u5f53\u7684\u6743\u9650\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u5b89\u88c5\u548c\u914d\u7f6eWorkerman<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6eWorkerman\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528Composer\u5b89\u88c5Workerman\uff1a<\/p>\n<pre>composer require workerman\/workerman<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2aworker.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php require_once __DIR__ . '\/vendor\/autoload.php';\nuse WorkermanWorker;\n\n\/\/ \u521b\u5efa\u4e00\u4e2aWorker\u76d1\u542c\u6307\u5b9a\u7aef\u53e3\n$worker = new Worker('tcp:\/\/0.0.0.0:2345');\n\n\/\/ \u5f53\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570\n$worker-&gt;onConnect = function($connection) {\n  echo \"New connection\n\";\n};\n\n\/\/ \u5f53\u6536\u5230\u5ba2\u6237\u7aef\u6d88\u606f\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570\n$worker-&amp;gt;onMessage = function($connection, $data) {\n  echo \"Received message: $data\n\";\n  \/\/ \u5728\u8fd9\u91cc\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230PostgreSQL\u4e2d\n  storeData($data);\n};\n\n\/\/ \u542f\u52a8worker\nWorker::runAll();\n\nfunction storeData($data) {\n  \/\/ \u8fde\u63a5\u5230PostgreSQL\u6570\u636e\u5e93\n  $conn = pg_connect(\"host=localhost dbname=mydatabase user=myuser password=mypassword\");\n  if (!$conn) {\n     echo \"Unable to connect to PostgreSQL\n\";\n     exit;\n  }\n  \n  \/\/ \u6267\u884cSQL\u67e5\u8be2\n  $result = pg_query($conn, \"INSERT INTO mytable (data) VALUES ('$data')\");\n  if (!$result) {\n     echo pg_last_error($conn);\n     exit;\n  }\n  \n  \/\/ \u5173\u95ed\u8fde\u63a5\n  pg_close($conn);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<\/ol>\n<p>\u4e09\u3001\u6d4b\u8bd5Workerman\u548cPostgreSQL\u7684\u96c6\u6210<br \/>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Worker\u8fdb\u7a0b\u5e76\u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n<pre>php worker.php start<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\n<p>\u5ba2\u6237\u7aef\u8fde\u63a5\u5230Workerman\uff1a<\/p>\n<pre>telnet localhost 2345<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u5411Workerman\u53d1\u9001\u4e00\u6761\u6d88\u606f\uff1a<\/p>\n<pre>Testing Workerman and PostgreSQL integration<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u5728\u6570\u636e\u5e93\u4e2d\u67e5\u770b\u5b58\u50a8\u7684\u6570\u636e\uff1a<\/p>\n<pre>SELECT * FROM mytable;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<\/ol>\n<p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u770b\u5230\u521a\u521a\u53d1\u9001\u7684\u6d88\u606f\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u3002\u901a\u8fc7\u914d\u7f6e\u548c\u5b89\u88c5PostgreSQL\uff0c\u4ee5\u53ca\u7f16\u5199\u76f8\u5e94\u7684Workerman\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u7684\u96c6\u6210\uff0c\u5e76\u80fd\u591f\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u3002\u8fd9\u4e3a\u6211\u4eec\u5f00\u53d1\u9ad8\u6027\u80fd\u7684Web\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\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\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8 \u5f15\u8a00\uff1a\u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u7684\u53d1\u5c55\uff0c\u5bf9\u4e8e\u6570\u636e\u5b58\u50a8\u548c\u7ba1\u7406\u7684\u9700\u6c42\u8d8a\u6765\u8d8a\u9ad8\uff0c\u800cPostgreSQL\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u4e14\u53ef\u9760\u7684\u5f00\u6e90\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u5907\u53d7\u5f00\u53d1\u8005\u9752\u7750\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Workerman\u4e2d\u4f7f\u7528PostgreSQL\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6ePostgreSQL\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u548c\u914d\u7f6ePostgreSQL\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a \u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5PostgreSQL\u3002 \u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u548c\u76f8\u5e94\u7684\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u6211\u4eec\u7684\u6570\u636e\u3002 \u521b\u5efa\u4e00\u4e2a\u7528\u6237\uff0c\u5e76\u4e3a\u5176\u5206\u914d\u9002\u5f53\u7684\u6743\u9650\u3002 \u4e8c\u3001\u5b89\u88c5\u548c\u914d\u7f6eWorkerman\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6eWorkerman\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a \u4f7f\u7528Composer\u5b89\u88c5Workerman\uff1a composer require workerman\/workerman \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2aworker.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php require_once __DIR__ . &#8216;\/vendor\/autoload.php&#8217;; use WorkermanWorker; \/\/ \u521b\u5efa\u4e00\u4e2aWorker\u76d1\u542c\u6307\u5b9a\u7aef\u53e3 $worker = new Worker(&#8216;tcp:\/\/0.0.0.0:2345&#8217;); \/\/ \u5f53\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570 $worker-&gt;onConnect = function($connection) { echo &#8220;New connection &#8220;; }; \/\/ \u5f53\u6536\u5230\u5ba2\u6237\u7aef\u6d88\u606f\u65f6\u89e6\u53d1\u7684\u56de\u8c03\u51fd\u6570 $worker-&amp;gt;onMessage = function($connection, $data) { echo &#8220;Received message: $data &#8220;; \/\/ \u5728\u8fd9\u91cc\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230PostgreSQL\u4e2d storeData($data); }; \/\/ \u542f\u52a8worker [&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-24993","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24993","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=24993"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24993\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}