{"id":356,"date":"2022-06-28T11:58:35","date_gmt":"2022-06-28T03:58:35","guid":{"rendered":"https:\/\/frogvps.com\/?p=356"},"modified":"2022-06-29T13:14:19","modified_gmt":"2022-06-29T05:14:19","slug":"mysql-binlog-%e9%85%8d%e7%bd%ae%e5%92%8c%e7%9b%b8%e5%85%b3%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/frogvps.com\/?p=356","title":{"rendered":"MYSQL binlog \u914d\u7f6e\u548c\u76f8\u5173\u547d\u4ee4"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u4e8c\u8fdb\u5236\u65e5\u5fd7<\/h2>\n\n\n\n<p>\u4e8c\u8fdb\u5236\u65e5\u5fd7\u8bb0\u5f55\u5bfc\u81f4\u6570\u636e\u6539\u53d8\u7684SQL\u8bed\u53e5\uff0c\u8bb0\u5f55\u5df2\u63d0\u4ea4\u7684\u65e5\u5fd7\uff0c\u4e0d\u4f9d\u8d56\u4e8e\u5b58\u50a8\u5f15\u64ce\u7c7b\u578b\u3002 \u4e8c\u8fdb\u5236\u65e5\u5fd7\u4e0d\u4f46\u53ef\u4ee5\u8bb0\u5f55\u8bed\u53e5\uff0c\u8fd8\u80fd\u591f\u901a\u8fc7\u201d\u91cd\u653e\u201c\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u4e8b\u4ef6\u6765\u751f\u6210\u6570\u636e\u526f\u672c\uff0c\u4ee5\u5b9e\u73b0\u5907\u4efd\u7684\u529f\u80fd\u3002\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5efa\u8bae\u5c06\u4e8c\u8fdb\u5236\u65e5\u5fd7\u548c\u6570\u636e\u6587\u4ef6\u5206\u5f00\u5b58\u653e\u3002<\/p>\n\n\n\n<p><strong>\u67e5\u770b\u4e8c\u8fdb\u5236\u65e5\u5fd7\u662f\u5426\u5f00\u542f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT @@sql_log_bin;#\u503c\u4e3a1\u6216\u8005ON\u5c31\u8868\u793a\u5df2\u7ecf\u5f00\u542f<\/code><\/pre>\n\n\n\n<p><strong>\u5982\u679c\u6ca1\u6709\u5f00\u542f\uff0c\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6b64\u9009\u9879\uff08\u6ce8\u610f\u4f7f\u7528\u6a2a\u7ebf\u800c\u4e0d\u662f\u4e0b\u5212\u7ebf\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nlog-bin<\/code><\/pre>\n\n\n\n<p><strong>\u5f00\u542f\u4e8c\u8fdb\u5236\u65e5\u5fd7\uff0c\u5e76\u4fee\u6539\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u8def\u5f84(\u5b58\u653e\u65e5\u5fd7\u7684\u6587\u4ef6\u5939\u6240\u6709\u8005\u5fc5\u987b\u6539\u6210mysql\u5426\u5219\u4f1a\u63d0\u793a\u6743\u9650\u4e0d\u591f)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nlog-bin=\/data\/binlog\/binlog<\/code><\/pre>\n\n\n\n<p><strong>\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u662fubuntu\uff0c\u914d\u7f6e\u6587\u4ef6\u8bed\u6cd5\u6b63\u786e\uff0c\u6587\u4ef6\u5939\u4e5f\u6709\u8bbf\u95ee\u6743\u9650\uff0c\u4fee\u6539\u4e86\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u5b58\u653e\u8def\u5f84\u540e\u670d\u52a1\u542f\u52a8\u4e0d\u4e86\uff0c\u63d0\u793a\u914d\u7f6e\u9519\u8bef\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p>\u4fee\u6539\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim \/etc\/apparmor.d\/usr.sbin.mysqld<\/code><\/pre>\n\n\n\n<p>\u627e\u5230# Allow log file access\u8fd9\u4e2a\u914d\u7f6e\u5757\uff0c\u5728\u540e\u9762\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff0c\u8def\u5f84\uff08\u8fd9\u91cc\u662f\/data\/binlog\uff09\u66ff\u6362\u6210\u4f60\u5b58\u653e\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u6587\u4ef6\u5939\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/data\/binlog\/ r,\n\/data\/binlog\/** rw,<\/code><\/pre>\n\n\n\n<p>\u6700\u540e\u91cd\u8bfb\u914d\u7f6e\u751f\u6548<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/etc\/init.d\/apparmor reload<\/code><\/pre>\n\n\n\n<p><strong>\u4e34\u65f6\u7981\u7528\u4e8c\u8fdb\u5236\u65e5\u5fd7\uff08\u8fd8\u539f\u65f6\u5e38\u7528\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set sql_log_bin=0;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u8fdb\u5236\u65e5\u5fd7\u683c\u5f0f<\/h2>\n\n\n\n<p>\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u683c\u5f0f\u6709\u4e09\u79cd\uff0c\u5206\u522b\u662fSTATEMENT\u3001ROW\u3001MIXED\u3002\u53ef\u4ee5\u901a\u8fc7binlog_format\u6765\u914d\u7f6e\uff0c\u5728\u5b98\u65b9\u6587\u6863\u91cc\uff0c\u5b83\u65e2\u662f\u914d\u7f6e\u6587\u4ef6\u9009\u9879\u53c8\u662f\u7cfb\u7edf\u53d8\u91cf(BOTH)\u3002\u5982\u679c\u9700\u8981\u5b9e\u73b0\u5907\u4efd\u529f\u80fd\uff0c\u5efa\u8bae\u4f7f\u7528ROW\u683c\u5f0f\uff0c\u66f4\u52a0\u5b89\u5168\u53ef\u9760\u3002\uff08STATEMENT\u7684\u7f3a\u70b9\uff1a\u8bb0\u5f55\u6570\u636e\u4e0d\u5168\uff0c\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u6267\u884c\u7ed3\u679c\u53ef\u80fd\u4f1a\u4e0d\u540c\uff0c\u8fd8\u539f\u7684\u65f6\u95f4\u70b9\u4e00\u5b9a\u662f\u672a\u6765\u67d0\u4e2a\u65f6\u95f4\uff0c\u8d77\u4e0d\u5230\u5907\u4efd\u6548\u679c\uff09<\/p>\n\n\n\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u4fee\u6539\u914d\u7f6e\u6587\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]\nbinlog_format=ROW<\/code><\/pre>\n\n\n\n<p><strong>\u65b9\u6cd5\u4e8c\uff1a\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET GLOBAL binlog_format=ROW;#\u9000\u51fa\u91cd\u65b0\u767b\u5f55MYSQL\u540e\u751f\u6548<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u8fdb\u5236\u65e5\u5fd7\u76f8\u5173\u547d\u4ee4<\/h2>\n\n\n\n<p><strong>\u67e5\u770b\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6\u5217\u8868\u548c\u5927\u5c0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW MASTER LOGS;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW BINARY LOGS;<\/code><\/pre>\n\n\n\n<p><strong>\u67e5\u770b\u4f7f\u7528\u4e2d\u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW MASTER STATUS;<\/code><\/pre>\n\n\n\n<p><strong>\u67e5\u770b\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5185\u5bb9<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW BINLOG EVENTS &#91;IN 'log_name'] &#91;FROM pos] &#91;LIMIT &#91;offset,] row_count]<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>show binlog events in 'binlog.000001' from 125;#\u67e5\u770bbinlog.000001\u4e2d\u7684\u4ece125\u5f00\u59cb\u7684\u65e5\u5fd7<\/code><\/pre>\n\n\n\n<p><strong>\u5237\u65b0\u65e5\u5fd7<\/strong><\/p>\n\n\n\n<p>\u6bcf\u6b21\u91cd\u542fMYSQL\u670d\u52a1\u4f1a\u81ea\u52a8\u5237\u65b0\uff0c\u751f\u6210\u65b0\u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FLUSH LOGS;<\/code><\/pre>\n\n\n\n<p><strong>\u5220\u9664\u4e8c\u8fdb\u5236\u65e5\u5fd7<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetime_expr }<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PURGE MASTER LOGS TO 'binlog.000003';#\u5220\u9664binlog.000003\u4e4b\u524d\u7684\u65e5\u5fd7\nPURGE MASTER LOGS BEFORE '2022-06-28 10:38:00';#\u5220\u9664\u67d0\u4e2a\u65f6\u95f4\u70b9\u4e4b\u524d\u7684\u65e5\u5fd7<\/code><\/pre>\n\n\n\n<p><strong>\u5220\u9664\u6240\u6709\u4e8c\u8fdb\u5236\u65e5\u5fd7\uff0c\u91cd\u65b0\u8ba1\u6570<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RESET MASTER &#91;TO #];\u5220\u9664\u6240\u6709\u65e5\u5fd7\uff0c\u5e76\u4ece#\u5f00\u59cb\u8ba1\u6570\uff0c\u9ed8\u8ba4\u4ece1\u5f00\u59cb<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">MYSQLBINLOG<\/h2>\n\n\n\n<p>mysqlbinlog\u662f\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u5ba2\u6237\u7aef\u547d\u4ee4\u5de5\u5177\uff0c\u652f\u6301\u79bb\u7ebf\u67e5\u770b\u4e8c\u8fdb\u5236\u65e5\u5fd7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqlbinlog &#91;OPTIONS] log_file<\/code><\/pre>\n\n\n\n<p><strong>\u9009\u9879<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--start-position= #\u5f00\u59cb\u4f4d\u7f6e\n--stop-position= #\u7ed3\u675f\u4f4d\u7f6e\n--start-datetime= #\u65f6\u95f4\u683c\u5f0f\uff1aYYYY-MM-DD hh:mm:ss\n--stop-datetime=\n--base64-output&#91;=name]\n-v -vvv<\/code><\/pre>\n\n\n\n<p><strong>\u8303\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqlbinlog \/var\/lib\/mysql\/binlog.000001 -v #\u67e5\u770b\u66f4\u8be6\u7ec6\u7684\u65e5\u5fd7\nmysqlbinlog \/var\/lib\/mysql\/binlog.000001 --stop-position=313 #\u4ece\u5934\u67e5\u770b\u65e5\u5fd7\u5230Pos 313\u4e3a\u6b62<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u4e8c\u8fdb\u5236\u65e5\u5fd7 \u4e8c\u8fdb\u5236\u65e5\u5fd7\u8bb0\u5f55\u5bfc\u81f4\u6570\u636e\u6539\u53d8\u7684SQL\u8bed\u53e5\uff0c\u8bb0\u5f55\u5df2\u63d0\u4ea4\u7684\u65e5\u5fd7\uff0c\u4e0d\u4f9d\u8d56\u4e8e\u5b58\u50a8\u5f15\u64ce\u7c7b\u578b\u3002 \u4e8c\u8fdb\u5236\u65e5\u5fd7\u4e0d\u4f46\u53ef\u4ee5\u8bb0\u5f55\u8bed\u53e5\uff0c\u8fd8\u80fd\u591f\u901a\u8fc7\u201d\u91cd\u653e\u201c\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u4e8b\u4ef6\u6765\u751f\u6210\u6570\u636e\u526f\u672c\uff0c\u4ee5\u5b9e\u73b0\u5907\u4efd\u7684\u529f\u80fd\u3002\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5efa\u8bae\u5c06\u4e8c\u8fdb\u5236\u65e5\u5fd7\u548c\u6570\u636e\u6587\u4ef6\u5206\u5f00\u5b58&#8230;<\/p>\n","protected":false},"author":1,"featured_media":283,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[36,34],"topic":[],"class_list":["post-356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-35","tag-mysql","tag-34"],"_links":{"self":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=356"}],"version-history":[{"count":6,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/356\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/posts\/356\/revisions\/363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=\/wp\/v2\/media\/283"}],"wp:attachment":[{"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/frogvps.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}