{"id":171833,"date":"2024-11-07T08:02:18","date_gmt":"2024-11-07T00:02:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171833\/"},"modified":"2024-11-07T08:02:19","modified_gmt":"2024-11-07T00:02:19","slug":"%e8%bc%95%e9%ac%86%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%b0%e5%9d%80%ef%bc%9a%e6%ba%90%e7%a2%bc%e6%94%b9%e6%b3%95-%e6%ba%90%e7%a2%bc%e8%a3%a1%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171833\/","title":{"rendered":"\u8f15\u9b06\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff1a\u6e90\u78bc\u6539\u6cd5 (\u6e90\u78bc\u88e1\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740)"},"content":{"rendered":"<h1 id=\"%e8%bc%95%e9%ac%86%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%b0%e5%9d%80%ef%bc%9a%e6%ba%90%e7%a2%bc%e6%94%b9%e6%b3%95-OJjysZRdVs\">\u8f15\u9b06\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff1a\u6e90\u78bc\u6539\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u6578\u64da\u5eab\u662f\u61c9\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u7d44\u4ef6\u4e4b\u4e00\u3002\u7121\u8ad6\u662f\u7db2\u7ad9\u3001\u79fb\u52d5\u61c9\u7528\u9084\u662f\u5176\u4ed6\u985e\u578b\u7684\u8edf\u4ef6\uff0c\u6578\u64da\u5eab\u7684\u914d\u7f6e\u548c\u7ba1\u7406\u90fd\u81f3\u95dc\u91cd\u8981\u3002\u7576\u9700\u8981\u66f4\u6539\u6578\u64da\u5eab\u5730\u5740\u6642\uff0c\u8a31\u591a\u958b\u767c\u8005\u53ef\u80fd\u6703\u611f\u5230\u56f0\u60d1\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904e\u6e90\u78bc\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%b0%e5%9d%80%ef%bc%9f-OJjysZRdVs\">\u70ba\u4ec0\u9ebc\u9700\u8981\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff1f<\/h2>\n<p>\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u60c5\u6cc1\uff1a<\/p>\n<ul>\n<li>\u9077\u79fb\u5230\u65b0\u7684\u6578\u64da\u5eab\u670d\u52d9\u5668\uff1a\u7576\u6578\u64da\u5eab\u670d\u52d9\u5668\u5347\u7d1a\u6216\u9077\u79fb\u6642\uff0c\u9700\u8981\u66f4\u65b0\u61c9\u7528\u7a0b\u5e8f\u4e2d\u7684\u6578\u64da\u5eab\u5730\u5740\u3002<\/li>\n<li>\u66f4\u6539\u6578\u64da\u5eab\u985e\u578b\uff1a\u5982\u679c\u5f9e\u4e00\u7a2e\u6578\u64da\u5eab\u7cfb\u7d71\uff08\u5982 MySQL\uff09\u5207\u63db\u5230\u53e6\u4e00\u7a2e\uff08\u5982 PostgreSQL\uff09\uff0c\u5247\u9700\u8981\u76f8\u61c9\u5730\u4fee\u6539\u9023\u63a5\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u958b\u767c\u548c\u751f\u7522\u74b0\u5883\u7684\u5340\u5206\uff1a\u5728\u958b\u767c\u74b0\u5883\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u80fd\u4f7f\u7528\u672c\u5730\u6578\u64da\u5eab\uff0c\u800c\u5728\u751f\u7522\u74b0\u5883\u4e2d\u5247\u4f7f\u7528\u9060\u7a0b\u6578\u64da\u5eab\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8%e6%ba%90%e7%a2%bc%e4%b8%ad%e4%bf%ae%e6%94%b9%e6%95%b8%e6%93%9a%e5%ba%ab%e5%9c%b0%e5%9d%80-OJjysZRdVs\">\u5982\u4f55\u5728\u6e90\u78bc\u4e2d\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740<\/h2>\n<p>\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\u7684\u5177\u9ad4\u6b65\u9a5f\u53d6\u6c7a\u65bc\u6240\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\u548c\u6846\u67b6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u7de8\u7a0b\u8a9e\u8a00\u7684\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"php-OJjysZRdVs\">PHP<\/h3>\n<p>\u5728 PHP \u4e2d\uff0c\u6578\u64da\u5eab\u9023\u63a5\u901a\u5e38\u5728\u4e00\u500b\u914d\u7f6e\u6587\u4ef6\u4e2d\u9032\u884c\u8a2d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u6578\u64da\u5eab\u914d\u7f6e\n$host = 'localhost'; \/\/ \u6578\u64da\u5eab\u5730\u5740\n$dbname = 'my_database'; \/\/ \u6578\u64da\u5eab\u540d\u7a31\n$username = 'root'; \/\/ \u7528\u6236\u540d\n$password = 'password'; \/\/ \u5bc6\u78bc\n\n\/\/ \u5275\u5efa\u6578\u64da\u5eab\u9023\u63a5\ntry {\n    $pdo = new PDO(\"mysql:host=$host;dbname=$dbname\", $username, $password);\n    \/\/ \u8a2d\u7f6e\u932f\u8aa4\u6a21\u5f0f\u70ba\u7570\u5e38\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n} catch (PDOException $e) {\n    echo \"\u9023\u63a5\u5931\u6557: \" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<p>\u8981\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff0c\u53ea\u9700\u66f4\u6539 `$host` \u8b8a\u91cf\u7684\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>$host = 'new_database_host'; \/\/ \u65b0\u7684\u6578\u64da\u5eab\u5730\u5740\n<\/code><\/pre>\n<h3 id=\"python-OJjysZRdVs\">Python<\/h3>\n<p>\u5728 Python \u4e2d\uff0c\u4f7f\u7528 SQLAlchemy \u9032\u884c\u6578\u64da\u5eab\u9023\u63a5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>from sqlalchemy import create_engine\n\n# \u6578\u64da\u5eab\u914d\u7f6e\nusername = 'root'\npassword = 'password'\nhost = 'localhost'\ndbname = 'my_database'\n\n# \u5275\u5efa\u6578\u64da\u5eab\u5f15\u64ce\nengine = create_engine(f'mysql+pymysql:\/\/{username}:{password}@{host}\/{dbname}')\n<\/code><\/pre>\n<p>\u8981\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff0c\u53ea\u9700\u66f4\u6539 `host` \u8b8a\u91cf\u7684\u503c\uff1a<\/p>\n<pre><code>host = 'new_database_host'  # \u65b0\u7684\u6578\u64da\u5eab\u5730\u5740\n<\/code><\/pre>\n<h3 id=\"node-js-OJjysZRdVs\">Node.js<\/h3>\n<p>\u5728 Node.js \u4e2d\uff0c\u4f7f\u7528 Mongoose \u9023\u63a5 MongoDB \u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const mongoose = require('mongoose');\n\n\/\/ \u6578\u64da\u5eab\u914d\u7f6e\nconst dbURI = 'mongodb:\/\/localhost:27017\/my_database';\n\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\nmongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true })\n    .then(() =&gt; console.log('\u6578\u64da\u5eab\u9023\u63a5\u6210\u529f'))\n    .catch(err =&gt; console.error('\u6578\u64da\u5eab\u9023\u63a5\u5931\u6557:', err));\n<\/code><\/pre>\n<p>\u8981\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff0c\u53ea\u9700\u66f4\u6539 `dbURI` \u7684\u503c\uff1a<\/p>\n<pre><code>const dbURI = 'mongodb:\/\/new_database_host:27017\/my_database'; \/\/ \u65b0\u7684\u6578\u64da\u5eab\u5730\u5740\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-OJjysZRdVs\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\u6642\uff0c\u958b\u767c\u8005\u61c9\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u65b0\u7684\u6578\u64da\u5eab\u5730\u5740\u662f\u6b63\u78ba\u7684\uff0c\u4e26\u4e14\u6578\u64da\u5eab\u670d\u52d9\u5668\u6b63\u5728\u904b\u884c\u3002<\/li>\n<li>\u6aa2\u67e5\u9632\u706b\u7246\u8a2d\u7f6e\uff0c\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u53ef\u4ee5\u8a2a\u554f\u65b0\u7684\u6578\u64da\u5eab\u670d\u52d9\u5668\u3002<\/li>\n<li>\u5728\u751f\u7522\u74b0\u5883\u4e2d\u9032\u884c\u66f4\u6539\u6642\uff0c\u5efa\u8b70\u5148\u5728\u958b\u767c\u74b0\u5883\u4e2d\u9032\u884c\u6e2c\u8a66\uff0c\u4ee5\u907f\u514d\u6f5b\u5728\u7684\u554f\u984c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OJjysZRdVs\">\u7e3d\u7d50<\/h2>\n<p>\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\u662f\u4e00\u9805\u5e38\u898b\u7684\u4efb\u52d9\uff0c\u7121\u8ad6\u662f\u5728\u958b\u767c\u9084\u662f\u751f\u7522\u74b0\u5883\u4e2d\u3002\u901a\u904e\u4e86\u89e3\u5982\u4f55\u5728\u6e90\u78bc\u4e2d\u9032\u884c\u9019\u4e00\u66f4\u6539\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u7ba1\u7406\u5176\u61c9\u7528\u7a0b\u5e8f\u7684\u6578\u64da\u5eab\u914d\u7f6e\u3002\u7121\u8ad6\u60a8\u4f7f\u7528\u7684\u662f PHP\u3001Python \u9084\u662f Node.js\uff0c\u4ee5\u4e0a\u793a\u4f8b\u90fd\u80fd\u5e6b\u52a9\u60a8\u8f15\u9b06\u5b8c\u6210\u9019\u4e00\u4efb\u52d9\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\u3002\u7121\u8ad6\u662f\u6578\u64da\u5eab\u7ba1\u7406\u9084\u662f\u5176\u4ed6\u9700\u6c42\uff0c\u6211\u5011\u90fd\u80fd\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f15\u9b06\u5b78\u6703\u5982\u4f55\u5728\u6e90\u78bc\u4e2d\u4fee\u6539\u6578\u64da\u5eab\u5730\u5740\uff0c\u7c21\u5316\u914d\u7f6e\u904e\u7a0b\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-171833","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171833","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=171833"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171833\/revisions"}],"predecessor-version":[{"id":171834,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171833\/revisions\/171834"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}