{"id":5492,"date":"2024-11-14T09:01:02","date_gmt":"2024-11-14T01:01:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5492\/"},"modified":"2024-11-14T09:01:02","modified_gmt":"2024-11-14T01:01:02","slug":"node%e6%80%8e%e4%b9%88%e8%bf%9e%e6%8e%a5mysql%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5492\/","title":{"rendered":"node\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 node.js \u8fde\u63a5 mysql \u6570\u636e\u5e93\u8fde\u63a5\u65b9\u6cd5\uff1a\u5b89\u88c5 mysql \u5305\u5f15\u5165 mysql \u5305\u521b\u5efa\u8fde\u63a5\u6253\u5f00\u8fde\u63a5\u6267\u884c\u67e5\u8be2\u5173\u95ed\u8fde\u63a5\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052904212348684.jpg\" class=\"aligncenter\" title=\"node\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93\u63d2\u56fe\" alt=\"node\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Node.js \u8fde\u63a5 MySQL \u6570\u636e\u5e93<\/strong><\/p>\n<p><strong>\u8fde\u63a5\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528  \u5305\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>const mysql = require('mysql');\n\nconst connection = mysql.createConnection({\n  host: 'localhost',\n  user: 'root',\n  password: 'password',\n  database: 'database_name'\n});\n\nconnection.connect();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u5b89\u88c5 mysql \u5305\uff1a<\/strong><\/p>\n<pre>npm install mysql<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u5f15\u5165 mysql \u5305\uff1a<\/strong><\/p>\n<pre>const mysql = require('mysql');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u521b\u5efa\u8fde\u63a5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 mysql.createConnection() \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\uff0c\u5982\u4e3b\u673a\u3001\u7528\u6237\u3001\u5bc6\u7801\u3001\u6570\u636e\u5e93\u540d\u79f0\u7b49\u3002<\/p>\n<p><strong>4. \u6253\u5f00\u8fde\u63a5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 connect() \u65b9\u6cd5\u6253\u5f00\u8fde\u63a5\uff0c\u8be5\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u8fde\u63a5\u6210\u529f\u6216\u5931\u8d25\u65f6\u89e6\u53d1\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>connection.connect(function(err) {\n  if (err) throw err;\n  console.log('Connected!');\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u6267\u884c\u67e5\u8be2\uff1a<\/strong><\/p>\n<p>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 query() \u65b9\u6cd5\u6267\u884c SQL \u67e5\u8be2\u3002query() \u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f SQL \u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u67e5\u8be2\u5b8c\u6210\u6216\u5931\u8d25\u65f6\u89e6\u53d1\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u67e5\u8be2\u6240\u6709\u7528\u6237\uff1a<\/p>\n<pre>const sql = 'SELECT * FROM users';\nconnection.query(sql, function(err, results) {\n  if (err) throw err;\n  console.log(results);\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u5173\u95ed\u8fde\u63a5\uff1a<\/strong><\/p>\n<p>\u67e5\u8be2\u5b8c\u6210\u540e\uff0c\u8bb0\u5f97\u4f7f\u7528 end() \u65b9\u6cd5\u5173\u95ed\u8fde\u63a5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>connection.end();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fnode\u600e\u4e48\u8fde\u63a5\u6570\u636e\u5e93\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 node.js \u8fde\u63a5 mysql \u6570\u636e\u5e93\u8fde\u63a5\u65b9\u6cd5\uff1a\u5b89\u88c5 mysql \u5305\u5f15\u5165 mysql \u5305\u521b\u5efa\u8fde\u63a5\u6253\u5f00\u8fde\u63a5\u6267\u884c\u67e5\u8be2\u5173\u95ed\u8fde\u63a5 \u5982\u4f55\u4f7f\u7528 Node.js \u8fde\u63a5 MySQL \u6570\u636e\u5e93 \u8fde\u63a5\u65b9\u6cd5\uff1a \u4f7f\u7528 \u5305\u8fde\u63a5 MySQL \u6570\u636e\u5e93\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a const mysql = require(&#8216;mysql&#8217;); const connection = mysql.createConnection({ host: &#8216;localhost&#8217;, user: &#8216;root&#8217;, password: &#8216;password&#8217;, database: &#8216;database_name&#8217; }); connection.connect(); \u767b\u5f55\u540e\u590d\u5236 \u8be6\u7ec6\u6b65\u9aa4\uff1a 1. \u5b89\u88c5 mysql \u5305\uff1a npm install mysql \u767b\u5f55\u540e\u590d\u5236 2. \u5f15\u5165 mysql \u5305\uff1a const mysql = require(&#8216;mysql&#8217;); [&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-5492","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5492","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=5492"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5492\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}