{"id":2086,"date":"2023-04-01T21:54:56","date_gmt":"2023-04-01T13:54:56","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2086"},"modified":"2023-04-06T15:48:04","modified_gmt":"2023-04-06T07:48:04","slug":"mysql-57-recovering-data-through-binlog","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/mysql-57-recovering-data-through-binlog\/","title":{"rendered":"MySQL 5.7 &#8211; \u901a\u8fc7 BINLOG \u6062\u590d\u6570\u636e"},"content":{"rendered":"<p>\u65e5\u5e38\u5f00\u53d1\uff0c\u8fd0\u7ef4\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u51fa\u73b0\u8bef\u5220\u6570\u636e\u7684\u60c5\u51b5\u3002\u8bef\u5220\u6570\u636e\u7684\u7c7b\u578b\u5927\u81f4\u53ef\u5206\u4e3a\u4ee5\u4e0b\u51e0\u7c7b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 delete \u8bef\u5220\u884c<\/li>\n<li>\u4f7f\u7528 drop table \u6216 truncate table \u8bef\u5220\u8868<\/li>\n<li>\u4f7f\u7528 drop database \u8bed\u53e5\u8bef\u5220\u6570\u636e\u5e93<\/li>\n<li>\u4f7f\u7528 rm \u547d\u4ee4\u8bef\u5220\u6574\u4e2a MySQL \u5b9e\u4f8b<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<p>\u4e0d\u540c\u7684\u60c5\u51b5\uff0c\u90fd\u4f1a\u6709\u5176\u4f18\u5148\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>\u9488\u5bf9\u8bef\u5220\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7 Flashback \u5de5\u5177\u5c06\u6570\u636e\u6062\u590d<\/li>\n<li>\u9488\u5bf9\u8bef\u5220\u8868\u6216\u5e93\uff0c\u4e00\u822c\u91c7\u7528\u901a\u8fc7 BINLOG \u5c06\u6570\u636e\u6062\u590d\u3002<\/li>\n<li>\u800c\u5bf9\u4e8e\u8bef\u5220 MySQL \u5b9e\u4f8b\uff0c\u5219\u9700\u8981\u6211\u4eec\u642d\u5efa HA \u7684 MySQL \u96c6\u7fa4\uff0c\u5e76\u4fdd\u8bc1\u6211\u4eec\u7684\u6570\u636e\u8de8\u673a\u623f\uff0c\u8de8\u57ce\u5e02\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u4e3b\u8981\u8ba8\u8bba\u7684\u5185\u5bb9\u662f\u8bef\u5220\u8868\u6216\u8005\u5e93\uff0c\u4f1a\u5148\u4ecb\u7ecd\u6709\u5173 BINLOG \u7684\u64cd\u4f5c\u547d\u4ee4\uff0c\u7136\u540e\u4f1a\u5bf9\u8bef\u5220\u8868\u7684\u8fd9\u79cd\u60c5\u51b5\u8fdb\u884c\u5b9e\u9645\u7684\u6a21\u62df\u3002<\/p>\n<h2>BINLOG \u5e38\u89c1\u64cd\u4f5c\u547d\u4ee4<\/h2>\n<p>BINLOG \u7684\u67e5\u8be2\u65b9\u5f0f\u4e00\u822c\u5206\u4e3a\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u8fdb\u5165 MySQL \u63a7\u5236\u53f0\u8fdb\u884c\u67e5\u8be2\uff0c\u53e6\u4e00\u79cd\u662f\u901a\u8fc7 MySQL \u63d0\u4f9b\u7684\u5de5\u5177 mysqlbinlog \u8fdb\u884c\u67e5\u8be2\uff0c\u4e24\u8005\u7684\u4e0d\u540c\u4f1a\u5728\u4e0b\u9762\u4ecb\u7ecd\u3002<\/p>\n<h3>\u901a\u8fc7 MySQL Cli \u67e5\u8be2 BINLOG \u4fe1\u606f<\/h3>\n<p>\u5728 cli \u4e2d\uff0c\u5e38\u89c1\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u67e5\u8be2 BINLOG \u683c\u5f0f\nshow VARIABLES like &#039;binlog_format&#039;;\n\n# \u67e5\u8be2 BINLOG \u4f4d\u7f6e\nshow VARIABLES like &#039;datadir&#039;;\n\n# \u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\u4e2d BINLOG \u540d\u79f0\u53ca\u5927\u5c0f\nshow binary logs;\n\n# \u67e5\u770b master \u6b63\u5728\u5199\u5165\u7684 BINLOG \u4fe1\u606f\nshow master status\\G;\n\n# \u901a\u8fc7 offset \u67e5\u770b BINLOG \u4fe1\u606f\nshow BINLOG events in &#039;mysql-bin.000034&#039; limit 9000, 10;\n\n# \u901a\u8fc7 position \u67e5\u770b binlog \u4fe1\u606f\nshow BINLOG events in &#039;mysql-bin.000034&#039; from 1742635 limit 10;<\/code><\/pre>\n<p>\u4f7f\u7528<code>show BINLOG events<\/code>\u7684\u95ee\u9898\uff1a<\/p>\n<p>\u4f7f\u7528\u8be5\u547d\u4ee4\u65f6\uff0c\u5982\u679c\u5f53\u524d binlog \u6587\u4ef6\u5f88\u5927\uff0c\u800c\u4e14\u6ca1\u6709\u6307\u5b9a limit\uff0c\u4f1a\u5f15\u53d1\u5bf9\u8d44\u6e90\u7684\u8fc7\u5ea6\u6d88\u8017\u3002\u56e0\u4e3a MySQL \u5ba2\u6237\u7aef\u9700\u8981\u5c06 binlog \u7684\u5168\u90e8\u5185\u5bb9\u5904\u7406\uff0c\u8fd4\u56de\u5e76\u663e\u793a\u51fa\u6765\u3002\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\uff0cmysqlbinlog \u5de5\u5177\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<h3>\u901a\u8fc7 mysqlbinlog \u67e5\u8be2 BINLOG \u4fe1\u606f<\/h3>\n<p>\u5728\u4ecb\u7ecd mysqlbinlog \u5de5\u5177\u4f7f\u7528\u524d\uff0c\u5148\u6765\u770b\u4e0b BINLOG \u6587\u4ef6\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u67e5\u8be2 BINLOG \u7684\u4fe1\u606f\nmysqlbinlog  --no-defaults mysql-bin.000034 | less<\/code><\/pre>\n<pre><code class=\"language-sql\"># at 141\n#100309  9:28:36 server id 123  end_log_pos 245\n  Query thread_id=3350  exec_time=11  error_code=0<\/code><\/pre>\n<ul>\n<li><code>at<\/code> \u8868\u793a offset \u6216\u8005\u8bf4\u4e8b\u4ef6\u5f00\u59cb\u7684\u8d77\u59cb\u4f4d\u7f6e<\/li>\n<li><code>100309 9:28:36 server id 123<\/code> \u8868\u793a server 123 \u5f00\u59cb\u6267\u884c\u4e8b\u4ef6\u7684\u65e5\u671f<\/li>\n<li><code>end_log_pos 245<\/code> \u8868\u793a\u4e8b\u4ef6\u7684\u7ed3\u675f\u4f4d\u7f6e + 1\uff0c\u6216\u8005\u8bf4\u662f\u4e0b\u4e00\u4e2a\u4e8b\u4ef6\u7684\u8d77\u59cb\u4f4d\u7f6e<\/li>\n<li><code>exec_time<\/code> \u8868\u793a\u5728 master \u4e0a\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\u5728 salve \u4e0a\uff0c\u8bb0\u5f55\u7684\u65f6\u95f4\u662f\u4ece Master \u8bb0\u5f55\u5f00\u59cb\uff0c\u4e00\u76f4\u5230 Slave \u7ed3\u675f\u5b8c\u6210\u6240\u82b1\u8d39\u7684\u65f6\u95f4<\/li>\n<li><code>rror_code=0<\/code> \u8868\u793a\u6ca1\u6709\u9519\u8bef\u53d1\u751f<\/li>\n<\/ul>\n<p>\u5728\u5927\u81f4\u4e86\u89e3 binlog \u7684\u5185\u5bb9\u540e\uff0cmysqlbinlog \u7684\u7528\u9014\u6709\u54ea\u4e9b\uff1f<\/p>\n<ul>\n<li><code>mysqlbinlog<\/code>\u53ef\u4ee5\u4f5c\u4e3a\u4ee3\u66ff cli \u8bfb\u53d6 binlog \u7684\u5de5\u5177<\/li>\n<li><code>mysqlbinlog<\/code>\u53ef\u4ee5\u5c06\u6267\u884c\u8fc7\u7684 SQL \u8bed\u53e5\u8f93\u51fa\uff0c\u7528\u4e8e\u6570\u636e\u7684\u6062\u590d\u6216\u5907\u4efd<\/li>\n<\/ul>\n<p>\u67e5\u8be2 BINLOG \u65e5\u5fd7\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u67e5\u8be2\u89c4\u5b9a\u65f6\u5019\u540e\u53d1\u751f\u7684 BINLOG \u65e5\u5fd7\nmysqlbinlog --no-defaults --base64-output=decode-rows -v \\\n --start-datetime  &quot;2019-11-22 14:00:00&quot; \\\n --database sync_test  mysql-bin.000034 | less<\/code><\/pre>\n<p>\u5bfc\u51fa BINLOG \u65e5\u5fd7\uff0c\u7528\u4e8e\u5206\u6790\u548c\u6392\u67e5 sql \u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"language-bash\">mysqlbinlog --no-defaults --base64-output=decode-rows -v \\\n --start-datetime  &quot;2019-11-22 14:00:00&quot; \\\n --database sync_test \\\n mysql-bin.000034 &gt; \/home\/mysql_backup\/binlog_raw.sql<\/code><\/pre>\n<p>\u5bfc\u5165 BINLOG \u65e5\u5fd7<\/p>\n<pre><code class=\"language-bash\"># \u901a\u8fc7 BINLOG \u8fdb\u884c\u6062\u590d\u3002\nmysqlbinlog --start-position=1038 --stop-position=1164 \\\n --database=db_name  mysql-bin.000034 | \\\n mysql  -u cisco -p db_name\n\n# \u901a\u8fc7 BINLOG \u5bfc\u51fa\u7684 sql \u8fdb\u884c\u6062\u590d\u3002\nmysql -u cisco -p db_name &lt; binlog_raw.sql<\/code><\/pre>\n<p>mysqlbinlog \u7684\u5e38\u7528\u53c2\u6570\uff1a<\/p>\n<ul>\n<li><code>--database<\/code> \u4ec5\u4ec5\u5217\u51fa\u914d\u7f6e\u7684\u6570\u636e\u5e93\u4fe1\u606f<\/li>\n<li><code>--no-defaults<\/code> \u8bfb\u53d6\u6ca1\u6709\u9009\u9879\u7684\u6587\u4ef6\uff0c\u6307\u5b9a\u7684\u539f\u56e0\u662f\u7531\u4e8e mysqlbinlog \u65e0\u6cd5\u8bc6\u522b BINLOG \u4e2d\u7684<code>default-character-set=utf8<\/code>\u6307\u4ee4<\/li>\n<li><code>--offset<\/code> \u8df3\u8fc7 log \u4e2d N \u4e2a\u6761\u76ee<\/li>\n<li><code>--verbose<\/code> \u5c06\u65e5\u5fd7\u4fe1\u606f\u91cd\u5efa\u4e3a\u539f\u59cb\u7684 SQL \u9648\u8ff0<\/li>\n<li>\n<ul>\n<li><code>-v<\/code> \u4ec5\u4ec5\u89e3\u91ca\u884c\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>-vv<\/code> \u4e0d\u4f46\u89e3\u91ca\u884c\u4fe1\u606f\uff0c\u8fd8\u5c06 SQL \u5217\u7c7b\u578b\u7684\u6ce8\u91ca\u4fe1\u606f\u4e5f\u89e3\u6790\u51fa\u6765<\/li>\n<\/ul>\n<\/li>\n<li><code>--start-datetime<\/code> \u663e\u793a\u4ece\u6307\u5b9a\u7684\u65f6\u95f4\u6216\u4e4b\u540e\u7684\u65f6\u95f4\u7684\u4e8b\u4ef6<\/li>\n<li>\n<ul>\n<li>\u63a5\u6536<code>DATETIME<\/code>\u6216\u8005<code>TIMESTRAMP<\/code>\u683c\u5f0f<\/li>\n<\/ul>\n<\/li>\n<li><code>--base64-output=decode-rows<\/code> \u5c06 BINLOG \u8bed\u53e5\u4e2d\u4e8b\u4ef6\u4ee5 base-64 \u7684\u7f16\u7801\u663e\u793a\uff0c\u5bf9\u4e00\u4e9b\u4e8c\u8fdb\u5236\u7684\u5185\u5bb9\u8fdb\u884c\u5c4f\u853d<\/li>\n<li>\n<ul>\n<li><code>AUTO<\/code> \u9ed8\u8ba4\u53c2\u6570\uff0c\u81ea\u52a8\u663e\u793a BINLOG \u4e2d\u7684\u5fc5\u8981\u7684\u8bed\u53e5<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>NEVER<\/code> \u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u7684 BINLOG \u8bed\u53e5\uff0c\u5982\u679c\u9047\u5230\u5fc5\u987b\u663e\u793a\u7684 BINLOG \u8bed\u8a00\uff0c\u5219\u4f1a\u62a5\u9519\u9000\u51fa<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>DECODE-ROWS<\/code> \u663e\u793a\u901a\u8fc7<code>-v<\/code>\u663e\u793a\u51fa\u6765\u7684 SQL \u4fe1\u606f\uff0c\u8fc7\u6ee4\u5230\u4e00\u4e9b BINLOG \u4e8c\u8fdb\u5236\u6570\u636e<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>MySQL Cli \u548c mysqlbinlog \u5de5\u5177\u4e4b\u95f4\u7684\u6bd4\u8f83<\/h3>\n<p>\u5982\u679c\u60f3\u77e5\u9053\u5f53\u524d MySQL \u4e2d\u6b63\u5728\u5199\u5165\u7684 BINLOG \u7684\u540d\u79f0\uff0c\u5927\u5c0f\u7b49\u57fa\u672c\u4fe1\u606f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 Cli \u76f8\u5173\u7684\u547d\u4ee4\u6765\u67e5\u8be2\u3002<\/p>\n<p>\u4f46\u60f3\u67e5\u8be2\uff0c\u5b9a\u4f4d\uff0c\u6062\u590d BINLOG \u4e2d\u5177\u4f53\u7684\u6570\u636e\u65f6\uff0c\u8981\u901a\u8fc7 mysqlbinlog \u5de5\u5177\uff0c\u56e0\u4e3a\u76f8\u8f83\u4e8e Cli \u6765\u8bf4\uff0cmysqlbinlog \u63d0\u4f9b\u4e86<code>--start-datetime<\/code>\uff0c<code>--stop-position<\/code>\u7b49\u8fd9\u6837\u66f4\u4e3a\u4e30\u5bcc\u7684\u53c2\u6570\u4f9b\u6211\u4eec\u9009\u62e9\u3002\u8fd9\u65f6 Cli \u4e2d<code>SHOW BINLOG EVENTS<\/code>\u7684\u7b80\u8981\u8bed\u6cd5\u5c31\u53d8\u5f97\u76f8\u5f62\u89c1\u7ecc\u4e86\u3002<\/p>\n<h2>\u4f7f\u7528 BINLOG \u6062\u590d\u6570\u636e<\/h2>\n<p>\u6062\u590d\u7684\u5927\u81f4\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f1a\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\uff0c\u5e76\u63d2\u5165\u6570\u636e<\/li>\n<li>\u8bef\u5220\u4e00\u6761\u6570\u636e<\/li>\n<li>\u7ee7\u7eed\u63d2\u5165\u6570\u636e<\/li>\n<li>\u8bef\u5220\u8868<\/li>\n<li>\u6700\u540e\u5c06\u539f\u6765\u4ee5\u53ca\u4e4b\u540e\u63d2\u5165\u7684\u6570\u636e\u8fdb\u884c\u6062\u590d<\/li>\n<\/ol>\n<h3>\u51c6\u5907\u6570\u636e<\/h3>\n<p>\u51c6\u5907\u6570\u636e\u5e93\uff0c\u8868\u53ca\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u521b\u5efa\u4e34\u65f6\u6570\u636e\u5e93\nCREATE DATABASE IF NOT EXISTS test_binlog \\\ndefault charset utf8 COLLATE utf8_general_ci; \n\n# \u521b\u5efa\u4e34\u65f6\u8868\nCREATE TABLE `sync_test` (`id` int(11) \\\nNOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL,  \\\nPRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n# \u6dfb\u52a0\u6570\u636e\ninsert into sync_test (id, name) values (null, &#039;xiaoa&#039;);\ninsert into sync_test (id, name) values (null, &#039;xiaob&#039;);\ninsert into sync_test (id, name) values (null, &#039;xiaoc&#039;);\n\n# \u67e5\u770b\u6dfb\u52a0\u7684\u6570\u636e\nselect * from sync_test;<\/code><\/pre>\n<h3>\u5220\u9664\u8868\u6216\u8005\u6570\u636e<\/h3>\n<p>\u8bef\u5220\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u5220\u9664 name=xiaob \u7684\u6570\u636e\ndelete from sync_test where id=3\n\n# \u63d2\u5165\u51e0\u6761\u6570\u636e\ninsert into sync_test (id, name) values (null, &#039;xiaod&#039;);\ninsert into sync_test (id, name) values (null, &#039;xiaoe&#039;);\ninsert into sync_test (id, name) values (null, &#039;xiaof&#039;);\n\n# \u5220\u9664\u8868\nDROP TABLE sync_test;<\/code><\/pre>\n<h3>\u6570\u636e\u7684\u6062\u590d<\/h3>\n<p>\u5728\u6267\u884c\u6570\u636e\u6062\u590d\u524d\uff0c\u5982\u679c\u64cd\u4f5c\u7684\u662f\u751f\u4ea7\u73af\u5883\uff0c\u4f1a\u6709\u5982\u4e0b\u7684\u5efa\u8bae\uff1a<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>flush logs<\/code>\u547d\u4ee4\uff0c\u66ff\u6362\u5f53\u524d\u4e3b\u5e93\u4e2d\u6b63\u5728\u4f7f\u7528\u7684 binlog \u6587\u4ef6\uff0c\u597d\u5904\u5982\u4e0b\uff1a<\/p>\n<\/li>\n<li>\n<ul>\n<li>\u53ef\u5c06\u8bef\u5220\u64cd\u4f5c\uff0c\u5b9a\u4f4d\u5728\u4e00\u4e2a BINLOG \u6587\u4ef6\u4e2d\uff0c\u4fbf\u4e8e\u4e4b\u540e\u7684\u6570\u636e\u5206\u6790\u548c\u6062\u590d<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>\u907f\u514d\u64cd\u4f5c\u6b63\u5728\u88ab\u4f7f\u7528\u7684 BINLOG \u6587\u4ef6\uff0c\u9632\u6b62\u53d1\u751f\u610f\u5916\u60c5\u51b5<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6570\u636e\u7684\u6062\u590d\u4e0d\u8981\u5728\u751f\u4ea7\u5e93\u4e2d\u6267\u884c\uff0c\u5148\u5728\u4e34\u65f6\u5e93\u6062\u590d\uff0c\u786e\u8ba4\u65e0\u8bef\u540e\uff0c\u518d\u5012\u56de\u751f\u4ea7\u5e93\u3002\u9632\u6b62\u5bf9\u6570\u636e\u7684\u4e8c\u6b21\u4f24\u5bb3\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u901a\u5e38\u6765\u8bf4\uff0c\u6062\u590d\u4e3b\u8981\u6709\u4e24\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5728\u4e34\u65f6\u5e93\u4e2d\uff0c\u6062\u590d\u5b9a\u671f\u6267\u884c\u7684\u5168\u91cf\u5907\u4efd\u6570\u636e<\/li>\n<li>\u7136\u540e\u57fa\u4e8e\u5168\u91cf\u5907\u4efd\u7684\u6570\u636e\u70b9\uff0c\u901a\u8fc7 BINLOG \u6765\u6062\u590d\u8bef\u64cd\u4f5c\u548c\u6b63\u5e38\u7684\u6570\u636e<\/li>\n<\/ol>\n<p>\u4f7f\u7528 BINLOG \u505a\u6570\u636e\u6062\u590d\u524d\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u67e5\u770b\u6b63\u5728\u4f7f\u7528\u7684 Binlog \u6587\u4ef6\nshow master status\\G;\n# \u663e\u793a\u7ed3\u679c\u662f\uff1a mysql-bin.000034\n\n# \u6267\u884c flush logs \u64cd\u4f5c\uff0c\u751f\u6210\u65b0\u7684 BINLOG\nflush logs;\n\n# \u67e5\u770b\u6b63\u5728\u4f7f\u7528\u7684 Binlog \u6587\u4ef6\nshow master status\\G;\n# \u7ed3\u679c\u662f\uff1amysql-bin.000035<\/code><\/pre>\n<p>\u786e\u5b9a\u6062\u590d\u6570\u636e\u7684\u6b65\u9aa4\uff1a<\/p>\n<p>\u8fd9\u91cc\u4e3b\u8981\u662f\u6709\u4e24\u6761\u8bef\u5220\u7684\u64cd\u4f5c\uff0c\u6570\u636e\u884c\u7684\u8bef\u5220\u548c\u8868\u7684\u8bef\u5220\u3002\u6709\u4e24\u79cd\u65b9\u5f0f\u8fdb\u884c\u6062\u590d\u3002<\/p>\n<ul>\n<li>\u65b9\u5f0f\u4e00\uff1a\u9996\u5148\u6062\u590d\u5230\u5220\u9664\u8868\u64cd\u4f5c\u4e4b\u524d\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u518d\u5355\u72ec\u6062\u590d\u8bef\u5220\u7684\u6570\u636e\u884c<\/li>\n<li>\u65b9\u5f0f\u4e8c\uff1a\u9996\u5148\u6062\u590d\u5230\u8bef\u5220\u6570\u636e\u884c\u7684\u4e4b\u524d\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u8df3\u8fc7\u8bef\u5220\u4e8b\u4ef6\u518d\u6062\u590d\u6570\u636e\u8868\u64cd\u4f5c\u4e4b\u524d\u7684\u4f4d\u7f6e<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u91c7\u7528\u65b9\u5f0f\u4e00\u7684\u65b9\u6848\u8fdb\u884c\u6f14\u793a\uff0c\u7531\u4e8e\u662f\u6f14\u793a\uff0c\u5c31\u4e0d\u989d\u5916\u627e\u4e00\u4e2a\u4e34\u65f6\u5e93\u8fdb\u884c\u5168\u91cf\u6062\u590d\u4e86\uff0c\u76f4\u63a5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u67e5\u8be2\u521b\u5efa\u8868\u7684\u4e8b\u4ef6\u4f4d\u7f6e\u548c\u5220\u9664\u8868\u7684\u4e8b\u4ef6\u4f4d\u7f6e<\/p>\n<pre><code class=\"language-bash\">#  \u6839\u636e\u65f6\u95f4\u786e\u5b9a\u4f4d\u7f6e\u4fe1\u606f\nmysqlbinlog --no-defaults --base64-output=decode-rows -v \\\n --start-datetime  &quot;2019-11-22 14:00:00&quot; \\\n --database test_binlog  mysql-bin.000034 | less<\/code><\/pre>\n<p>\u521b\u5efa\u8868\u7684\u5f00\u59cb\u4f4d\u7f6e\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL-BINLOG-\u521b\u5efa\u8868\u7684\u5f00\u59cb\u4f4d\u7f6e.png\" alt=\"MySQL-BINLOG-\u521b\u5efa\u8868\u7684\u5f00\u59cb\u4f4d\u7f6e\" \/><\/p>\n<p>\u5220\u9664\u8868\u7684\u7ed3\u675f\u4f4d\u7f6e\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL-BINLOG-\u5220\u9664\u8868\u7684\u7ed3\u675f\u4f4d\u7f6e.png\" alt=\"MySQL-BINLOG-\u5220\u9664\u8868\u7684\u7ed3\u675f\u4f4d\u7f6e\" \/><\/p>\n<p>\u63d2\u5165 name=&#8217;xiaob&#8217; \u7684\u4f4d\u7f6e\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL-BINLOG-\u63d2\u5165\u6570\u636e\u4f4d\u7f6e.png\" alt=\"MySQL-BINLOG-\u63d2\u5165\u6570\u636e\u4f4d\u7f6e\" \/><\/p>\n<pre><code class=\"language-sql\"># \u6839\u636e\u4f4d\u7f6e\u5bfc\u51fa SQL \u6587\u4ef6\nmysqlbinlog --no-defaults --base64-output=decode-rows -v \\\n --start-position &quot;2508132&quot; --stop-position &quot;2511004&quot; \\\n --database test_binlog  mysql-bin.000034 \\\n &gt; \/home\/mysql_backup\/test_binlog_step1.sql\n\nmysqlbinlog --no-defaults --base64-output=decode-rows -v \\\n --start-position &quot;2508813&quot; --stop-position &quot;2509187&quot; \\\n --database test_binlog  mysql-bin.000034 \\\n &gt; \/home\/mysql_backup\/test_binlog_step2.sql\n\n# \u4f7f\u7528 mysql \u8fdb\u884c\u6062\u590d\nmysql -u cisco -p &lt; \/home\/mysql_backup\/test_binlog_step1.sql\nmysql -u cisco -p &lt; \/home\/mysql_backup\/test_binlog_step2.sql<\/code><\/pre>\n<blockquote>\n<p>MySQL 5.7 \u4e2d\u65e0\u8bba\u662f\u5426\u6253\u5f00 GTID \u7684\u914d\u7f6e\uff0c\u5728\u6bcf\u6b21\u4e8b\u52a1\u5f00\u542f\u65f6\uff0c\u90fd\u9996\u5148\u4f1a\u51fa GTID \u7684\u4e00\u4e2a\u4e8b\u52a1\uff0c\u7528\u4e8e\u5e76\u884c\u590d\u5236\u3002\u6240\u4ee5\u5728\u786e\u5b9a\u5bfc\u51fa\u5f00\u59cb\u4e8b\u52a1\u4f4d\u7f6e\u65f6\uff0c\u8981\u7b97\u4e0a\u8fd9\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<p>\u5728\u4f7f\u7528<code>--stop-position<\/code>\u5bfc\u51fa\u65f6\uff0c\u4f1a\u5bfc\u51fa\u5728\u6307\u5b9a\u4f4d\u7f6e\u7684\u524d\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u6240\u4ee5\u8fd9\u91cc\u8981\u63a8\u540e\u4e00\u4e2a\u4e8b\u52a1\u3002<\/p>\n<p>\u5bf9\u4e8e DML \u7684\u8bed\u53e5\uff0c\u4e3b\u8981\u7ed3\u675f\u4f4d\u7f6e\u8981\u7b97\u4e0a COMMIT \u7684\u4f4d\u7f6e\u3002<\/p>\n<\/blockquote>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728\u6587\u7ae0\u5f00\u59cb\u65f6\uff0c\u6211\u4eec\u719f\u6089\u4e86\u64cd\u4f5c BINLOG \u7684\u4e24\u79cd\u65b9\u5f0f CLI \u548c mysqlbinlog \u5de5\u5177\uff0c\u63a5\u7740\u4ecb\u7ecd\u4e86\u5176\u95f4\u7684\u533a\u522b\u548c\u4f7f\u7528\u573a\u666f\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u5927\u578b\u7684 BINLOG \u6587\u4ef6\uff0c\u4f7f\u7528 mysqlbinlog \u4f1a\u66f4\u52a0\u7684\u65b9\u4fbf\u548c\u6548\u7387\u3002\u5e76\u5bf9 mysqlbinlog \u7684\u4e00\u4e9b\u5e38\u89c1\u53c2\u6570\u8fdb\u884c\u4e86\u4ecb\u7ecd\u3002<\/p>\n<p>\u63a5\u7740\u901a\u8fc7\u4f7f\u7528 mysqlbinlog \u5b9e\u9645\u6a21\u62df\u4e86\u6570\u636e\u6062\u590d\u7684\u8fc7\u7a0b\uff0c\u5e76\u5728\u6062\u590d\u6570\u636e\u65f6\uff0c\u63d0\u51fa\u4e86\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\uff0c\u6bd4\u5982 flush logs \u7b49\u3002<\/p>\n<p>\u6700\u540e\u5728\u6062\u590d\u6570\u636e\u65f6\uff0c\u8981\u6ce8\u610f start-position \u548c end-position \u7684\u4e00\u4e9b\u5c0f\u7ec6\u8282\uff0c\u6765\u4fdd\u8bc1\u627e\u5230\u5408\u9002\u7684\u4f4d\u7f6e\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/point-in-time-recovery.html\" title=\"point-in-time-recovery\">point-in-time-recovery<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/recovery-from-backups.html\" title=\"recovery-from-backups\">recovery-from-backups<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/backup-policy.html\" title=\"backup-policy\">backup-policy<\/a><\/li>\n<\/ul>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.cnblogs.com\/michael9\/p\/11923483.html\">https:\/\/www.cnblogs.com\/michael9\/p\/11923483.html<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u65e5\u5e38\u5f00\u53d1\uff0c\u8fd0\u7ef4\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u51fa\u73b0\u8bef\u5220\u6570\u636e\u7684\u60c5\u51b5\u3002\u8bef\u5220\u6570\u636e\u7684\u7c7b\u578b\u5927\u81f4\u53ef\u5206\u4e3a\u4ee5\u4e0b\u51e0\u7c7b\uff1a \u4f7f\u7528 delete \u8bef\u5220\u884c \u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[524],"class_list":["post-2086","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-binlog"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2086"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2086\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}