{"id":183734,"date":"2024-11-10T07:17:22","date_gmt":"2024-11-09T23:17:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183734\/"},"modified":"2024-11-10T07:17:23","modified_gmt":"2024-11-09T23:17:23","slug":"%e4%bd%bf%e7%94%a8javascript%e5%b0%87%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-js%e6%95%b8%e6%93%9a%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183734\/","title":{"rendered":"\u4f7f\u7528JavaScript\u5c07\u6578\u64da\u5b58\u5165MySQL\u6578\u64da\u5eab (js\u6578\u64da\u5230mysql\u6578\u64da\u5eab\u4e2d)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8javascript%e5%b0%87%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-cuedvxwovd\">\u4f7f\u7528JavaScript\u5c07\u6578\u64da\u5b58\u5165MySQL\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cJavaScript\u5df2\u7d93\u6210\u70ba\u4e00\u7a2e\u4e0d\u53ef\u6216\u7f3a\u7684\u6280\u8853\u3002\u7121\u8ad6\u662f\u524d\u7aef\u9084\u662f\u5f8c\u7aef\u958b\u767c\uff0cJavaScript\u90fd\u80fd\u5920\u63d0\u4f9b\u5f37\u5927\u7684\u529f\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528JavaScript\u5c07\u6578\u64da\u5b58\u5165MySQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-cuedvxwovd\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u74b0\u5883\uff1a<\/p>\n<ul>\n<li>Node.js\uff1a\u7528\u65bc\u904b\u884cJavaScript\u4ee3\u78bc\u7684\u74b0\u5883\u3002<\/li>\n<li>MySQL\uff1a\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u3002<\/li>\n<li>npm\uff1aNode.js\u7684\u5305\u7ba1\u7406\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86Node.js\u548cMySQL\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd\u4e00\u500bMySQL\u7684Node.js\u9a45\u52d5\u7a0b\u5e8f\uff0c\u901a\u5e38\u4f7f\u7528mysql2\u6216mysql\u5305\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code>npm install mysql2<\/code><\/pre>\n<h2 id=\"%e5%bb%ba%e7%ab%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e5%92%8c%e8%a1%a8-cuedvxwovd\">\u5efa\u7acb\u6578\u64da\u5eab\u548c\u8868<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u6578\u64da\u5eab\u548c\u4e00\u500b\u8868\u4f86\u5b58\u5132\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u6578\u64da\u5eab\u548c\u8868\u7684SQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>CREATE DATABASE my_database;\n\nUSE my_database;\n\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    email VARCHAR(100)\n);<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8javascript%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-cuedvxwovd\">\u4f7f\u7528JavaScript\u9023\u63a5MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u4f7f\u7528JavaScript\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u9023\u63a5\u6578\u64da\u5eab\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>const mysql = require('mysql2');\n\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'your_username',\n    password: 'your_password',\n    database: 'my_database'\n});\n\nconnection.connect((err) =&gt; {\n    if (err) {\n        console.error('\u9023\u63a5\u5931\u6557: ' + err.stack);\n        return;\n    }\n    console.log('\u5df2\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0cID: ' + connection.threadId);\n});<\/code><\/pre>\n<h2 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-cuedvxwovd\">\u63d2\u5165\u6578\u64da\u5230MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u958b\u59cb\u63d2\u5165\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>const user = { name: 'John Doe', email: 'john@example.com' };\n\nconnection.query('INSERT INTO users SET ?', user, (err, results) =&gt; {\n    if (err) {\n        console.error('\u63d2\u5165\u5931\u6557: ' + err.stack);\n        return;\n    }\n    console.log('\u63d2\u5165\u6210\u529f\uff0cID: ' + results.insertId);\n});<\/code><\/pre>\n<h2 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-cuedvxwovd\">\u67e5\u8a62\u6578\u64da<\/h2>\n<p>\u9664\u4e86\u63d2\u5165\u6578\u64da\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u67e5\u8a62\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>connection.query('SELECT * FROM users', (err, results) =&gt; {\n    if (err) {\n        console.error('\u67e5\u8a62\u5931\u6557: ' + err.stack);\n        return;\n    }\n    console.log('\u67e5\u8a62\u7d50\u679c: ', results);\n});<\/code><\/pre>\n<h2 id=\"%e9%97%9c%e9%96%89%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-cuedvxwovd\">\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>connection.end((err) =&gt; {\n    if (err) {\n        console.error('\u95dc\u9589\u9023\u63a5\u5931\u6557: ' + err.stack);\n        return;\n    }\n    console.log('\u6578\u64da\u5eab\u9023\u63a5\u5df2\u95dc\u9589');\n});<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cuedvxwovd\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528JavaScript\u5c07\u6578\u64da\u5b58\u5165MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff0c\u5305\u62ec\u74b0\u5883\u6e96\u5099\u3001\u6578\u64da\u5eab\u9023\u63a5\u3001\u6578\u64da\u63d2\u5165\u548c\u67e5\u8a62\u7b49\u3002\u9019\u4e9b\u6280\u8853\u5c0d\u65bc\u958b\u767c\u73fe\u4ee3Web\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0cServer.HK \u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528JavaScript\u5c07\u6578\u64da\u5b58\u5165MySQL\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u548c\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183734","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183734"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183734\/revisions"}],"predecessor-version":[{"id":183735,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183734\/revisions\/183735"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}