{"id":129265,"date":"2024-10-26T20:18:58","date_gmt":"2024-10-26T12:18:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/129265\/"},"modified":"2024-10-26T20:18:59","modified_gmt":"2024-10-26T12:18:59","slug":"redis%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%addb%e8%a8%88%e6%95%b8%e7%9a%84%e5%af%a6%e7%8f%be%ef%bc%88redis-%e7%8d%b2%e5%8f%96db%e5%80%8b%e6%95%b8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/129265\/","title":{"rendered":"Redis\u6578\u64da\u5eab\u4e2dDB\u8a08\u6578\u7684\u5be6\u73fe\uff08redis \u7372\u53d6db\u500b\u6578\uff09"},"content":{"rendered":"<h1 id=\"redis%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%addb%e8%a8%88%e6%95%b8%e7%9a%84%e5%af%a6%e7%8f%be%ef%bc%88redis-%e7%8d%b2%e5%8f%96db%e5%80%8b%e6%95%b8%ef%bc%89-unojCzWJLa\">Redis\u6578\u64da\u5eab\u4e2dDB\u8a08\u6578\u7684\u5be6\u73fe\uff08redis \u7372\u53d6db\u500b\u6578\uff09<\/h1>\n<p>Redis\u662f\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\uff0c\u5982\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u5b58\u5132\u7b49\u3002\u4f5c\u70ba\u4e00\u500b\u958b\u6e90\u7684\u6578\u64da\u5eab\uff0cRedis\u652f\u6301\u591a\u500b\u6578\u64da\u5eab\u5be6\u4f8b\uff0c\u9019\u4f7f\u5f97\u7528\u6236\u80fd\u5920\u5728\u540c\u4e00\u500bRedis\u670d\u52d9\u5668\u4e0a\u7ba1\u7406\u591a\u500b\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Redis\u4e2d\u7372\u53d6\u6578\u64da\u5eab\u7684\u500b\u6578\uff0c\u4ee5\u53ca\u76f8\u95dc\u7684\u5be6\u73fe\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"redis%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b5%90%e6%a7%8b-unojCzWJLa\">Redis\u4e2d\u7684\u6578\u64da\u5eab\u7d50\u69cb<\/h2>\n<p>\u5728Redis\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6982\u5ff5\u662f\u76f8\u5c0d\u7c21\u55ae\u7684\u3002\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0cRedis\u63d0\u4f9b16\u500b\u6578\u64da\u5eab\uff0c\u9019\u4e9b\u6578\u64da\u5eab\u7684\u7d22\u5f15\u5f9e0\u523015\u3002\u7528\u6236\u53ef\u4ee5\u901a\u904e\u9078\u64c7\u4e0d\u540c\u7684\u6578\u64da\u5eab\u4f86\u5b58\u5132\u548c\u6aa2\u7d22\u6578\u64da\u3002\u6bcf\u500b\u6578\u64da\u5eab\u90fd\u662f\u7368\u7acb\u7684\uff0c\u9019\u610f\u5473\u8457\u5728\u4e00\u500b\u6578\u64da\u5eab\u4e2d\u5b58\u5132\u7684\u6578\u64da\u4e0d\u6703\u5f71\u97ff\u5230\u5176\u4ed6\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e7%8d%b2%e5%8f%96redis%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%80%8b%e6%95%b8-unojCzWJLa\">\u7372\u53d6Redis\u6578\u64da\u5eab\u7684\u500b\u6578<\/h2>\n<p>\u8981\u7372\u53d6Redis\u4e2d\u53ef\u7528\u6578\u64da\u5eab\u7684\u500b\u6578\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6216\u901a\u904e\u7de8\u7a0b\u63a5\u53e3\u4f86\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8redis%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%b7%a5%e5%85%b7-unojCzWJLa\">1. \u4f7f\u7528Redis\u547d\u4ee4\u884c\u5de5\u5177<\/h3>\n<p>\u5728Redis\u7684\u547d\u4ee4\u884c\u754c\u9762\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u7372\u53d6\u7576\u524d\u6578\u64da\u5eab\u7684\u7d22\u5f15\uff1a<\/p>\n<pre><code>SELECT 0<\/code><\/pre>\n<p>\u9019\u500b\u547d\u4ee4\u6703\u9078\u64c7\u7d22\u5f15\u70ba0\u7684\u6578\u64da\u5eab\u3002\u8981\u67e5\u770b\u7576\u524d\u6578\u64da\u5eab\u7684\u6578\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>CONFIG GET databases<\/code><\/pre>\n<p>\u9019\u500b\u547d\u4ee4\u6703\u8fd4\u56de\u4e00\u500b\u5305\u542b\u6578\u64da\u5eab\u6578\u91cf\u7684\u5217\u8868\uff0c\u901a\u5e38\u9ed8\u8a8d\u503c\u70ba16\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8redis%e5%ae%a2%e6%88%b6%e7%ab%af%e5%ba%ab-unojCzWJLa\">2. \u4f7f\u7528Redis\u5ba2\u6236\u7aef\u5eab<\/h3>\n<p>\u5982\u679c\u4f60\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528Redis\uff0c\u53ef\u4ee5\u901a\u904e\u5ba2\u6236\u7aef\u5eab\u4f86\u7372\u53d6\u6578\u64da\u5eab\u7684\u500b\u6578\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u7684redis-py\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u7372\u53d6\u6578\u64da\u5eab\u7684\u500b\u6578\ndb_count = r.config_get('databases')['databases']\nprint(f\"Redis\u4e2d\u53ef\u7528\u7684\u6578\u64da\u5eab\u500b\u6578: {db_count}\")\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u9996\u5148\u9023\u63a5\u5230Redis\u670d\u52d9\u5668\uff0c\u7136\u5f8c\u4f7f\u7528`config_get`\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u5eab\u7684\u500b\u6578\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e5%85%b6%e4%bb%96%e7%b7%a8%e7%a8%8b%e8%aa%9e%e8%a8%80-unojCzWJLa\">3. \u4f7f\u7528\u5176\u4ed6\u7de8\u7a0b\u8a9e\u8a00<\/h3>\n<p>\u9664\u4e86Python\uff0c\u5176\u4ed6\u7de8\u7a0b\u8a9e\u8a00\u4e5f\u6709\u76f8\u61c9\u7684Redis\u5ba2\u6236\u7aef\u5eab\u3002\u4f8b\u5982\uff0c\u5728Node.js\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528`ioredis`\u5eab\u4f86\u7372\u53d6\u6578\u64da\u5eab\u7684\u500b\u6578\uff1a<\/p>\n<pre><code>const Redis = require('ioredis');\nconst redis = new Redis();\n\nredis.config('GET', 'databases').then((result) =&gt; {\n    console.log(`Redis\u4e2d\u53ef\u7528\u7684\u6578\u64da\u5eab\u500b\u6578: ${result[1]}`);\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-unojCzWJLa\">\u7e3d\u7d50<\/h2>\n<p>\u5728Redis\u4e2d\uff0c\u7372\u53d6\u6578\u64da\u5eab\u7684\u500b\u6578\u662f\u4e00\u500b\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u7121\u8ad6\u662f\u901a\u904e\u547d\u4ee4\u884c\u5de5\u5177\u9084\u662f\u7de8\u7a0b\u63a5\u53e3\uff0c\u90fd\u80fd\u8f15\u9b06\u5be6\u73fe\u3002\u4e86\u89e3\u5982\u4f55\u7ba1\u7406\u548c\u4f7f\u7528\u9019\u4e9b\u6578\u64da\u5eab\u5c0d\u65bc\u958b\u767c\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Redis\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728Redis\u6578\u64da\u5eab\u4e2d\u7372\u53d6DB\u8a08\u6578\uff0c\u638c\u63e1DB\u7ba1\u7406\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u64cd\u4f5c\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-129265","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/129265","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=129265"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/129265\/revisions"}],"predecessor-version":[{"id":129266,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/129265\/revisions\/129266"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=129265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=129265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=129265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}