{"id":201354,"date":"2025-05-10T10:04:06","date_gmt":"2025-05-10T02:04:06","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201354\/"},"modified":"2025-05-10T10:04:06","modified_gmt":"2025-05-10T02:04:06","slug":"%e4%bb%8emysql%e5%88%b0redis%e7%9a%84%e7%ae%80%e5%8d%95%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%81%e7%a7%bb%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201354\/","title":{"rendered":"\u4eceMySQL\u5230Redis\u7684\u7b80\u5355\u6570\u636e\u5e93\u8fc1\u79fb\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4eceMySQL\u5230Redis\u7684\u7b80\u5355\u6570\u636e\u5e93\u8fc1\u79fb\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u4eceMySQL\u5230Redis\u7684\u7b80\u5355\u6570\u636e\u5e93\u8fc1\u79fb\u65b9\u6cd5\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>&nbsp;\u4ecemysql\u642c\u4e00\u4e2a\u5927\u8868\u5230redis\u4e2d\uff0c\u4f60\u4f1a\u53d1\u73b0\u5728\u63d0\u53d6\u3001\u8f6c\u6362\u6216\u662f\u8f7d\u5165\u4e00\u884c\u6570\u636e\u65f6\uff0c\u901f\u5ea6\u6162\u7684\u8ba9\u4f60\u96be\u4ee5\u5fcd\u53d7\u3002\u8fd9\u91cc\u6211\u5c31\u8981\u544a\u8bc9\u4e00\u4e2a\u8ba9\u4f60\u89e3\u8131\u7684\u5c0f\u6280\u5de7\u3002\u4f7f\u7528\u201c\u7ba1\u9053\u8f93\u51fa\u201d\u7684\u65b9\u5f0f\u628amysql\u547d\u4ee4\u884c\u4ea7\u751f\u7684\u5185\u5bb9\u76f4\u63a5\u4f20\u9012\u7ed9redis-cli\uff0c\u4ee5\u7ed5\u8fc7\u201c\u4e2d\u95f4\u4ef6\u201d\u7684\u65b9\u5f0f\u4f7f\u4e24\u8005\u5728\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u65f6\u8fbe\u5230\u6700\u4f73\u901f\u5ea6\u3002<\/p>\n<p>\u4e00\u4e2a\u7ea6\u516b\u767e\u4e07\u884c\u6570\u636e\u7684mysql\u8868\uff0c\u539f\u672c\u5bfc\u5165\u5230redis\u4e2d\u9700\u898190\u5206\u949f\uff0c\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u540e\uff0c\u53ea\u9700\u8981\u4e24\u5206\u949f\u3002\u4e0d\u7ba1\u4f60\u4fe1\u4e0d\u4fe1\uff0c\u53cd\u6b63\u6211\u662f\u4fe1\u4e86\u3002<\/p>\n<p><strong>Mysql\u5230Redis\u7684\u6570\u636e\u534f\u8bae<\/strong><\/p>\n<p>redis-cli\u547d\u4ee4\u884c\u5de5\u5177\u6709\u4e00\u4e2a\u6279\u91cf\u63d2\u5165\u6a21\u5f0f\uff0c\u662f\u4e13\u95e8\u4e3a\u6279\u91cf\u6267\u884c\u547d\u4ee4\u8bbe\u8ba1\u7684\u3002\u8fd9\u7b2c\u4e00\u6b65\u5c31\u662f\u628aMysql\u67e5\u8be2\u7684\u5185\u5bb9\u683c\u5f0f\u5316\u6210redis-cli\u53ef\u7528\u7684\u6570\u636e\u683c\u5f0f\u3002here we go!<\/p>\n<p> \u6211\u7684\u7edf\u8ba1\u8868\uff1a<br \/> &nbsp;<\/p>\n<pre>\nCREATE TABLE events_all_time (\n id int(11) unsigned NOT NULL AUTO_INCREMENT,\n action varchar(255) NOT NULL,\n count int(11) NOT NULL DEFAULT 0,\n PRIMARY KEY (id),\n UNIQUE KEY uniq_action (action)\n);\n<\/pre>\n<p>\u51c6\u5907\u5728\u6bcf\u884c\u6570\u636e\u4e2d\u6267\u884c\u7684redis\u547d\u4ee4\u5982\u4e0b\uff1a<br \/> &nbsp;<br \/> HSET events_all_time [action] [count]<br \/> \u6309\u7167\u4ee5\u4e0aredis\u547d\u4ee4\u89c4\u5219\uff0c\u521b\u5efa\u4e00\u4e2aevents_to_redis.sql\u6587\u4ef6\uff0c\u5185\u5bb9\u662f\u7528\u6765\u751f\u6210redis\u6570\u636e\u534f\u8bae\u683c\u5f0f\u7684SQL\uff1a<br \/> &nbsp;<br \/> &#8212; events_to_redis.sql<br \/> &nbsp;<\/p>\n<pre>\nSELECT CONCAT(\n \"*4\\r\\n\",\n '$', LENGTH(redis_cmd), '\\r\\n',\n redis_cmd, '\\r\\n',\n '$', LENGTH(redis_key), '\\r\\n',\n redis_key, '\\r\\n',\n '$', LENGTH(hkey), '\\r\\n',\n hkey, '\\r\\n',\n '$', LENGTH(hval), '\\r\\n',\n hval, '\\r'\n)\nFROM (\n SELECT\n 'HSET' as redis_cmd,\n 'events_all_time' AS redis_key,\n action AS hkey,\n count AS hval\n FROM events_all_time\n) AS t\n<\/pre>\n<p>ok, \u7528\u4e0b\u9762\u7684\u547d\u4ee4\u6267\u884c\uff1a<br \/> &nbsp;<\/p>\n<pre>\nmysql stats_db --skip-column-names --raw \n\n<p>\u5f88\u91cd\u8981\u7684mysql\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<\/pre>\n<ul>\n<li>&#8211;raw: \u4f7fmysql\u4e0d\u8f6c\u6362\u5b57\u6bb5\u503c\u4e2d\u7684\u6362\u884c\u7b26\u3002<\/li>\n<li>&#8211;skip-column-names: \u4f7fmysql\u8f93\u51fa\u7684\u6bcf\u884c\u4e2d\u4e0d\u5305\u542b\u5217\u540d\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4eceMySQL\u5230Redis\u7684\u7b80\u5355\u6570&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201354","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201354","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201354"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201354\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}