{"id":2160,"date":"2020-07-30T00:17:03","date_gmt":"2020-07-29T16:17:03","guid":{"rendered":"http:\/\/39.97.231.10\/?p=2160"},"modified":"2020-07-30T00:17:03","modified_gmt":"2020-07-29T16:17:03","slug":"mysql%e6%85%a2%e6%9f%a5%e8%af%a2%e5%8f%af%e8%a7%86%e5%8c%96%e5%b7%a5%e5%85%b7anemometer%e5%ae%89%e8%a3%85%e4%b8%8e%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=2160","title":{"rendered":"MySQL\u6162\u67e5\u8be2\u53ef\u89c6\u5316\u5de5\u5177Anemometer\u5b89\u88c5\u4e0e\u4f7f\u7528"},"content":{"rendered":"<div>\n<div>\n<div>\nAnemometer\u662f\u4e00\u6b3e\u5f00\u6e90\u7684MySQL\u6162\u67e5\u8be2\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5b83\u4f7f\u7528pt-query-digest\u5de5\u5177\u89e3\u6790MySQL\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u5c06\u89e3\u6790\u7ed3\u679c\u663e\u793a\u5728Web\u9875\u9762\u4e0a\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u67e5\u8be2\u65b9\u5f0f\uff0c\u7528\u6237\u5728\u9875\u9762\u4e0a\u80fd\u591f\u66f4\u52a0\u65b9\u4fbf\u5730\u67e5\u770bMySQL\u6162\u67e5\u8be2\u4fe1\u606f\u3002\u672c\u6587\u7b80\u5355\u4ecb\u7ecdAnemometer\u5b89\u88c5\u90e8\u7f72\u65b9\u5f0f\u4ee5\u53ca\u9047\u5230\u7684\u4e00\u4e9b\u95ee\u9898\u3002<\/p>\n<h4>1. \u73af\u5883\u4f9d\u8d56<\/h4>\n<ul>\n<li>MySQL\u6570\u636e\u5e93\uff0c\u7528\u4e8e\u5b58\u50a8Anemometer\u4f7f\u7528\u7684\u4e00\u4e9b\u67e5\u8be2\u5206\u6790\u6570\u636e\u3002<\/li>\n<li>pt-query-digest\u5de5\u5177\uff0c\u7528\u4e8e\u89e3\u6790MySQL\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u89e3\u6790\u7ed3\u679c\u5c06\u5b58\u50a8\u5728MySQL\u6570\u636e\u5e93\u4e2d\u3002<\/li>\n<li>php web server\uff0cphp\u7248\u672c 5.5\u53ca\u4ee5\u4e0a\u3002<\/li>\n<\/ul>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u4e3aCentOS 7.2\uff0cMySQL\u7248\u672c 5.7.19\u3002\u5173\u4e8e\u4ee5\u4e0a\u73af\u5883\u7684\u914d\u7f6e\u548c\u5de5\u5177\u7684\u5b89\u88c5\uff0c\u672c\u6587\u4e0d\u518d\u8be6\u7ec6\u63cf\u8ff0\uff0c\u7f51\u4e0a\u6709\u5f88\u591a\u6587\u7ae0\u53ef\u4f9b\u53c2\u8003\u3002<\/p>\n<h4>2. \u5b89\u88c5Anemometer<\/h4>\n<p>\u4e0b\u8f7d\u6e90\u7801\uff1a<br \/>\ncd \/var\/www\/html<br \/>\ngit clone https:\/\/github.com\/box\/Anemometer.git anemometer<br \/>\n\u5bfc\u5165\u6570\u636e\uff1a<br \/>\ncd anemometer<br \/>\nmysql -S \/mysql\/data\/mysql.sock &lt; install.sql<br \/>\nmysql -S \/mysql\/data\/mysql.sock -e \\&#8221;grant ALL ON slow_query_log.* to \\&#8217;anemometer\\&#8217;@\\&#8217;%\\&#8217; IDENTIFIED BY \\&#8217;123456\\&#8217;;\\&#8221;<br \/>\n\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff1a<br \/>\ncd conf\/<br \/>\ncp sample.config.inc.php config.inc.php<br \/>\nvim config.inc.php<\/p>\n<pre>$conf[\\'datasources\\'][\\'localhost\\'] = array(\n        \\'host\\'  =&gt; \\'127.0.0.1\\',\n        \\'port\\'  =&gt; 3306,\n        \\'db\\'    =&gt; \\'slow_query_log\\',\n        \\'user\\'  =&gt; \\'anemometer\\',\n        \\'password\\' =&gt; \\'123456\\',\n        \\'tables\\' =&gt; array(\n                \\'global_query_review\\' =&gt; \\'fact\\',\n                \\'global_query_review_history\\' =&gt; \\'dimension\\'\n        )\n);\n<\/pre>\n<p>\u914d\u7f6e\u63d2\u4ef6explain\u8fde\u63a5MySQL\u7684\u8d26\u53f7\u548c\u5bc6\u7801\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>$conf[\\'plugins\\'] = array(\n        ...\n    \\'explain\\'       =&gt;      function ($sample) {\n        $conn[\\'user\\'] = \\'anemometer\\';\n        $conn[\\'password\\'] = \\'superSecurePass\\';\n        return $conn;\n    },\n);\n<\/pre>\n<p>\u91cd\u542fweb \u670d\u52a1\u5668\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662fapache httpd\u670d\u52a1\u5668\u3002<br \/>\nsystemctl restart httpd.service<br \/>\n\u8bbf\u95ee\u9875\u9762\uff1a<br \/>\nhttp:\/\/192.168.56.200\/anemometer\/index.php<\/p>\n<h4>3. \u5bfc\u5165\u4e00\u4e9b\u6162\u67e5\u8be2\u65e5\u5fd7<\/h4>\n<pre>pt-query-digest\n--user=anemometer\n--password=123456\n--review h=127.0.0.1,D=slow_query_log,t=global_query_review\n--history h=127.0.0.1,D=slow_query_log,t=global_query_review_history\n--no-report\n--limit=0%\n--filter=\\\" \\\\$event-&gt;{Bytes} = length(\\\\$event-&gt;{arg}) and \\\\$event-&gt;{hostname}=\\\\\\\"$HOSTNAME\\\\\\\"\\\"\n\/mysql\/data\/slow.log\n<\/pre>\n<h4>4. \u95ee\u9898<\/h4>\n<p>\uff081\uff09\u6253\u5f00Anemometer\u9996\u9875\uff0c\u63d0\u793a\u7f3a\u5c11BCMath\u6269\u5c55\u3002<br \/>\nAnemometer requires the BCMath extension<br \/>\n\u89e3\u51b3\uff1a\u5b89\u88c5php-bcmath\u6269\u5c55<br \/>\nyum install -y php-bcmath.x86_64<br \/>\n\uff082\uff09\u670d\u52a1\u5668\u65f6\u533a\u8bbe\u7f6e\u95ee\u9898<br \/>\n\u5c06\u670d\u52a1\u5668\u65f6\u533a\u8bbe\u7f6e\u4e3aUTC\u65f6\u533a\u3002<br \/>\n\uff083\uff09pt-query-digest \u89e3\u6790\u6162\u65e5\u5fd7\u5bfc\u5165\u5230\u6570\u636e\u5e93\u62a5\u9519\u3002<br \/>\nanemometer \u4e0e pt-query-digest \u751f\u6210\u7684\u8868global_query_review_history \u8868\u7ed3\u6784\u4e0d\u540c\uff0cpt-query-digest\u7248\u672c\u4e3a3.1.0\uff0c\u53ef\u80fd\u4e0eanemometer\u4f7f\u7528\u7684\u8868\u7ed3\u6784\u4e0d\u517c\u5bb9\u3002pt-query-digest\u89e3\u6790\u6162\u65e5\u5fd7\u7684\u7ed3\u679c\u5bfc\u5165\u5230anemometer\u6570\u636e\u5e93\uff0c\u8fd9\u4e2a\u903b\u8f91\u9700\u8981\u4fee\u6539\u6e90\u7801\u6216\u8005\u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1\u3002<br \/>\n\uff084\uff09group\u67e5\u8be2\u62a5\u9519<br \/>\nMySQL5.7 sql_mode\u6253\u5f00ONLY_FULL_GROUP_BY\u540e\uff0cAnemometer\u7684\u4e00\u4e9b\u4e0d\u89c4\u8303\u7684group by\u8bed\u53e5\u67e5\u8be2\u4f1a\u62a5\u9519\u3002<br \/>\nGROUP BY checksum<br \/>\n\u4fee\u6539\u4e3a<br \/>\nGROUP BY checksum,snippet<\/p>\n<pre>SELECT checksum AS `checksum`,\n  LEFT(dimension.sample,20) AS `snippet`,\n  ROUND(SUM(Rows_examined_sum)\/SUM(rows_sent_sum),2) AS `index_ratio`,\n  SUM(Query_time_sum) \/ SUM(ts_cnt) AS `query_time_avg`,\n  ROUND(SUM(Rows_sent_sum)\/SUM(ts_cnt),0) AS `rows_sent_avg`,\n  SUM(ts_cnt) AS `ts_cnt`,\n  SUM(Query_time_sum) AS `Query_time_sum`,\n  SUM(Lock_time_sum) AS `Lock_time_sum`,\n  SUM(Rows_sent_sum) AS `Rows_sent_sum`,\n  SUM(Rows_examined_sum) AS `Rows_examined_sum`,\n  SUM(Full_scan_sum) AS `Full_scan_sum`,\n  SUM(Tmp_table_sum) AS `Tmp_table_sum`,\n  SUM(Filesort_sum) AS `Filesort_sum`\n FROM `global_query_review` AS `fact`\n JOIN `global_query_review_history` AS `dimension` USING (`checksum`)\n WHERE dimension.hostname_max = \\\"127.0.0.1\\\"\n  AND dimension.ts_min &gt;= \\\"2020-03-09 13:53:29\\\"\n  AND dimension.ts_min &lt;= \\\"2020-03-10 13:53:29\\\"\n GROUP BY checksum\n ORDER BY Query_time_sum DESC\n LIMIT 20\n<\/pre>\n<h4>5. \u603b\u7ed3<\/h4>\n<p>Anemometer \u6e90\u7801\u6700\u540e\u4e00\u6b21\u66f4\u65b0\u662f2016\u5e74\uff0c\u968f\u7740pt-query-digest\u548cMySQL\u7248\u672c\u7684\u66f4\u65b0\uff0c\u53ef\u80fd\u6709\u4e00\u4e9b\u4e0d\u517c\u5bb9\uff0c\u9700\u8981\u57fa\u4e8e\u6e90\u7801\u8fdb\u884c\u6539\u9020\u6216\u8005\u4e8c\u6b21\u5f00\u53d1\uff0c\u4f46\u662f\u6539\u8d77\u6765\u4e5f\u4e0d\u592a\u56f0\u96be\uff0c\u603b\u7684\u6765\u8bf4\uff0cAnemometer\u8fd8\u7b97\u662f\u4e00\u4e2a\u4e0d\u9519\u7684MySQL\u6162\u67e5\u8be2\u53ef\u89c6\u5316\u5de5\u5177\u3002\n<\/p><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Anemometer\u662f\u4e00\u6b3e\u5f00\u6e90\u7684MySQL\u6162\u67e5\u8be2\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5b83\u4f7f\u7528pt-query-digest\u5de5\u5177\u89e3\u6790MyS &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=2160\"> <span class=\"screen-reader-text\">MySQL\u6162\u67e5\u8be2\u53ef\u89c6\u5316\u5de5\u5177Anemometer\u5b89\u88c5\u4e0e\u4f7f\u7528<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"Anemometer\u662f\u4e00\u6b3e\u5f00\u6e90\u7684MySQL\u6162\u67e5\u8be2\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5b83\u4f7f\u7528pt-query-digest\u5de5\u5177\u89e3\u6790MyS&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2160"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2160"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/2160\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2160"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}