{"id":1306,"date":"2020-05-21T11:23:07","date_gmt":"2020-05-21T03:23:07","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1306"},"modified":"2020-05-21T11:23:07","modified_gmt":"2020-05-21T03:23:07","slug":"mysql-binlog%e6%97%a5%e5%bf%97%e6%9f%a5%e7%9c%8b","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1306","title":{"rendered":"MySQL binlog\u65e5\u5fd7\u67e5\u770b"},"content":{"rendered":"<div>\n<h2 style=\\\"padding-left: 40px;\\\"><strong>\u4e00\u3001\u4ec0\u4e48\u662fbinlog<\/strong><\/h2>\n<p style=\\\"padding-left: 40px;\\\">binlog\u00a0\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u7528\u4e8e\u8bb0\u5f55\u7528\u6237\u5bf9\u6570\u636e\u5e93\u00a0\u66f4\u65b0\u7684SQL\u8bed\u53e5\u00a0\u4fe1\u606f\uff0c\u4f8b\u5982\u66f4\u6539\u6570\u636e\u5e93\u8868\u548c\u66f4\u6539\u5185\u5bb9\u7684SQL\u8bed\u53e5\u90fd\u4f1a\u8bb0\u5f55\u5230binlog\u91cc\uff0c\u4f46\u662f\u5bf9\u5e93\u8868\u7b49\u5185\u5bb9\u7684\u67e5\u8be2\u4e0d\u4f1a\u8bb0\u5f55\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cbinlog\u00a0\u65e5\u5fd7\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\uff0c\u4e0d\u80fd\u4f7f\u7528\u67e5\u770b\u6587\u672c\u5de5\u5177\u7684\u547d\u4ee4\uff08\u6bd4\u5982\uff0ccat\uff0cvi\u7b49\uff09\u67e5\u770b\uff0c\u800c\u4f7f\u7528mysqlbinlog\u00a0\u89e3\u6790\u67e5\u770b\u3002<\/p>\n<h2 id=\\\"\u4e8c\u3001binlog-\u7684\u4f5c\u7528\\\" style=\\\"padding-left: 40px;\\\"><strong>\u4e8c\u3001binlog \u7684\u4f5c\u7528<\/strong><\/h2>\n<p style=\\\"padding-left: 40px;\\\">binlog\u4e3b\u8981\u7528\u4e8e\u590d\u5236\uff08Master-Slave \u4e3b\u4ece\u540c\u6b65\uff09\u3001\u6062\u590d\u548c\u5ba1\u8ba1\u3002<\/p>\n<h2 id=\\\"\u4e09\u3001binlog-\u4e09\u79cd\u6a21\u5f0f\\\" style=\\\"padding-left: 40px;\\\"><strong>\u4e09\u3001binlog \u4e09\u79cd\u6a21\u5f0f<\/strong><\/h2>\n<\/div>\n<div>\n<h3 id=\\\"31-row-level-\u6a21\u5f0f\uff08rbr\uff09\\\" style=\\\"padding-left: 40px;\\\">3.1 Row Level \u6a21\u5f0f\uff08RBR\uff09<\/h3>\n<pre style=\\\"padding-left: 40px;\\\">\u4e0d\u8bb0\u5f55\u6bcf\u6761sql\u8bed\u53e5\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u53ea\u9700\u8bb0\u5f55\u54ea\u6761\u6570\u636e\u88ab\u4fee\u6539\u4e86\uff0c\u4fee\u6539\u6210\u4ec0\u4e48\u6837\u4e86\u3002\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u4f18\u70b9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">* \u51c6\u786e\u6027\u5f3a\uff0c\u80fd\u51c6\u786e\u590d\u5236\u6570\u636e\u7684\u53d8\u66f4\n* \u51cf\u5c11\u6570\u636e\u5e93\u9501\u7684\u4f7f\u7528\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u7f3a\u70b9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">* \u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u65e5\u5fd7,\u5bfc\u81f4\u8f83\u5927\u7684\u7f51\u7edcIO\u548c\u78c1\u76d8IO\n* \u8981\u6c42\u4e3b\u4ece\u6570\u636e\u5e93\u8868\u7ed3\u6784\u5b8c\u5168\u4e00\u81f4\n* \u65e0\u6cd5\u5728\u4ece\u6570\u636e\u5e93\u4e0a\u5355\u72ec\u6267\u884c\u89e6\u53d1\u5668\n<\/pre>\n<h3 id=\\\"32-statement-level-\u6a21\u5f0f\uff08sbr\uff09\\\" style=\\\"padding-left: 40px;\\\">3.2 Statement Level \u6a21\u5f0f\uff08SBR\uff09<\/h3>\n<pre style=\\\"padding-left: 40px;\\\">\u6bcf\u4e00\u6761\u4fee\u6539\u6570\u636e\u7684 sql \u90fd\u4f1a\u8bb0\u5f55\u5230 master \u7684 bin_log \u4e2d\uff0cslave \u5728\u590d\u5236\u7684\u65f6\u5019 sql \u8fdb\u7a0b\u4f1a\u89e3\u6790\u6210 master \u7aef\u6267\u884c\u8fc7\u7684\u76f8\u540c\u7684 sql \u5728 slave \u5e93\u4e0a\u518d\u6b21\u6267\u884c\u3002\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u4f18\u70b9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">* \u4e0d\u9700\u8981\u8bb0\u5f55\u6bcf\u4e00\u6761sql\u8bed\u53e5\u548c\u6bcf\u4e00\u884c\u7684\u6570\u636e\u53d8\u5316\uff0c\u51cf\u5c11\u4e86binlog\u65e5\u5fd7\u91cf\uff0c\u8282\u7ea6IO\uff0c\u63d0\u9ad8\u6027\u80fd\n* \u5e76\u4e0d\u5f3a\u5236\u8981\u6c42\u4e3b\u4ece\u6570\u636e\u5e93\u7684\u8868\u7ed3\u6784\u5b9a\u4e49\u5b8c\u5168\u76f8\u540c\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u7f3a\u70b9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">* \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u5bfc\u81f4 master-slave \u4e2d\u7684\u6570\u636e\u4e0d\u4e00\u81f4(\u5982sleep()\u51fd\u6570\uff0c last_insert_id()\uff0c\u4ee5\u53causer-defined functions(udf)\u7b49\u4f1a\u51fa\u73b0\u95ee\u9898)\n* \u76f8\u6bd4\u4e8e RBR \u6a21\u5f0f\uff0c\u5728\u6267\u884c\u590d\u5236\u65f6\u9700\u8981\u66f4\u591a\u7684\u884c\u9501\n<\/pre>\n<h3 id=\\\"33-mixed\u6a21\u5f0f\uff08mbr\uff09\\\" style=\\\"padding-left: 40px;\\\">3.3 MIXED\u6a21\u5f0f\uff08MBR\uff09<\/h3>\n<pre style=\\\"padding-left: 40px;\\\">\u4ee5\u4e0a\u4e24\u79cd\u6a21\u5f0f\u7684\u6df7\u5408\u4f7f\u7528\uff0c\u4e00\u822c\u7684\u590d\u5236\u4f7f\u7528 STATEMENT \u6a21\u5f0f\u4fdd\u5b58 binlog\uff0c\u5bf9\u4e8e STATEMENT \u6a21\u5f0f\u65e0\u6cd5\u590d\u5236\u7684\u64cd\u4f5c\u4f7f\u7528 ROW \u6a21\u5f0f\u4fdd\u5b58 binlog\uff0cMySQL\u4f1a\u6839\u636e\u6267\u884c\u7684 SQL \u8bed\u53e5\u9009\u62e9\u65e5\u5fd7\u4fdd\u5b58\u65b9\u5f0f\u3002\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u4f18\u70b9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">\u51c6\u786e\u6027\u5f3a\uff0c\u6587\u4ef6\u5927\u5c0f\u9002\u4e2d\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u7f3a\u70b9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">\u6709\u53ef\u80fd\u53d1\u751f\u4e3b\u4ece\u4e0d\u4e00\u81f4\u95ee\u9898\n<\/pre>\n<h3 id=\\\"34-binlog\u6a21\u5f0f\u7684\u9009\u62e9\\\" style=\\\"padding-left: 40px;\\\">3.4 binlog\u6a21\u5f0f\u7684\u9009\u62e9<\/h3>\n<pre style=\\\"padding-left: 40px;\\\">1. \u4e0d\u7528\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u3001\u51fd\u6570\uff0c\u9009\u62e9\u9ed8\u8ba4\u7684 Statement level\n2. \u7528\u5230MySQL\u7684\u7279\u6b8a\u529f\u80fd\uff08\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u3001\u51fd\u6570\uff09\u9009\u62e9Mixed\u6a21\u5f0f\n3. \u7528\u5230MySQL\u7684\u7279\u6b8a\u529f\u80fd\uff08\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u3001\u51fd\u6570\uff09\uff0c\u53c8\u5e0c\u671b\u6570\u636e\u6700\u5927\u5316\u4e00\u76f4\u5219\u9009\u62e9Row\u6a21\u5f0f\n<\/pre>\n<h2><\/h2>\n<h2 style=\\\"padding-left: 40px;\\\"><strong>\u56db\u3001binlog \u5e38\u7528\u53c2\u6570<\/strong><\/h2>\n<div>\n<h5 style=\\\"padding-left: 40px;\\\"><strong>\u7ba1\u7406Binlog\u76f8\u5173\u7684SQL\u8bed\u53e5<\/strong><\/h5>\n<table style=\\\"margin-left: 40px;\\\">\n<thead style=\\\"padding-left: 40px;\\\">\n<tr style=\\\"padding-left: 40px;\\\">\n<th style=\\\"padding-left: 40px;\\\">SQL\u8bed\u53e5<\/th>\n<th style=\\\"padding-left: 40px;\\\">\u8bed\u53e5\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody style=\\\"padding-left: 40px;\\\">\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show master logs;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u6240\u6709Binlog\u7684\u65e5\u5fd7\u5217\u8868<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show master status;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u6700\u540e\u4e00\u4e2aBinlog\u65e5\u5fd7\u7684\u7f16\u53f7\u540d\u79f0\uff0c\u53ca\u6700\u540e\u4e00\u4e2a\u4e8b\u4ef6\u7ed3\u675f\u7684\u4f4d\u7f6e\uff08pos\uff09<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">flush logs;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u5237\u65b0Binlog\uff0c\u6b64\u523b\u5f00\u59cb\u4ea7\u751f\u4e00\u4e2a\u65b0\u7f16\u53f7\u7684Binlog\u65e5\u5fd7\u6587\u4ef6<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">freset master;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u5237\u6e05\u7a7a\u6240\u6709\u7684Binlog\u65e5\u5fd7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5 style=\\\"padding-left: 40px;\\\"><strong>\u67e5\u770bBinlog\u76f8\u5173\u7684SQL\u8bed\u53e5<\/strong><\/h5>\n<p style=\\\"padding-left: 40px;\\\">\uff08show binlog events [IN \u2018log_name\u2019] [FROM pos] [LIMIT [offset,] row_cout] \uff09<\/p>\n<table style=\\\"margin-left: 40px;\\\">\n<thead style=\\\"padding-left: 40px;\\\">\n<tr style=\\\"padding-left: 40px;\\\">\n<th style=\\\"padding-left: 40px;\\\">SQL\u8bed\u53e5<\/th>\n<th style=\\\"padding-left: 40px;\\\">\u8bed\u53e5\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody style=\\\"padding-left: 40px;\\\">\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show binlog events;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u7b2c\u4e00\u4e2aBinlog\u65e5\u5fd7<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show binlog events in \u2018binlog.000030\u2019;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u6307\u5b9a\u7684Binlog\u65e5\u5fd7<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show binlog events in \u2018binlog.000030\u2019 from 931;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u4ece\u6307\u5b9a\u7684\u4f4d\u7f6e\u5f00\u59cb\uff0c\u67e5\u770b\u6307\u5b9a\u7684Binlog\u65e5\u5fd7<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show binlog events in \u2018binlog.000030\u2019 from 931 limit 2;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u4ece\u6307\u5b9a\u7684\u4f4d\u7f6e\u5f00\u59cb\uff0c\u67e5\u770b\u6307\u5b9a\u7684Binlog\u65e5\u5fd7\uff0c\u9650\u5236\u67e5\u8be2\u7684\u6761\u6570<\/td>\n<\/tr>\n<tr style=\\\"padding-left: 40px;\\\">\n<td style=\\\"padding-left: 40px;\\\">show binlog events in \u2018binlog.000030\u2019 from 931 limit 1,2;<\/td>\n<td style=\\\"padding-left: 40px;\\\">\u4ece\u6307\u5b9a\u7684\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5e26\u6709\u504f\u79fb\uff0c\u67e5\u770b\u6307\u5b9a\u7684Binlog\u65e5\u5fd7\uff0c\u9650\u5236\u67e5\u8be2\u7684\u6761\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5 style=\\\"padding-left: 40px;\\\"><strong>Binlog \u4e2d\u7684Event_type<\/strong><\/h5>\n<p style=\\\"padding-left: 40px;\\\">\u5b98\u65b9\u63d0\u4f9b\u7684\u670936\u4e2devent_type\uff0c\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u8fd9\u4e48\u591a\u79cd\uff0c\u4e0b\u9762\u5199\u51fa\u7684\u662f\u51e0\u79cd\u5e38\u7528\u7684\u3002<\/p>\n<ul>\n<li>QUERY_EVENT\uff1a\u4e0e\u6570\u636e\u65e0\u5173\u7684\u64cd\u4f5c\uff0cbegin\u3001drop table\u3001truncate table\u7b49<\/li>\n<li>TABLE_MAP_EVENT\uff1a\u8bb0\u5f55\u4e0b\u4e00\u4e2a\u64cd\u4f5c\u6240\u5bf9\u5e94\u7684\u8868\u4fe1\u606f\uff0c\u5b58\u50a8\u4e86\u6570\u636e\u5e93\u540d\u548c\u8868\u540d<\/li>\n<li>XID_EVNET\uff1a\u6807\u8bb0\u4e8b\u7269\u63d0\u4ea4<\/li>\n<li>WRITE_ROWS_EVENT\uff1a\u63d2\u5165\u6570\u636e\uff0c\u5373insert\u64cd\u4f5c<\/li>\n<li>UPDATE_ROWS_EVENT\uff1a\u66f4\u65b0\u6570\u636e\uff0c\u5373update\u64cd\u4f5c<\/li>\n<li>DELETE_ROWS_EVENT\uff1a\u5220\u9664\u6570\u636e\uff0c\u5373delete\u64cd\u4f5c<\/li>\n<\/ul>\n<\/div>\n<table style=\\\"height: 518px;\\\" width=\\\"734\\\">\n<thead>\n<tr>\n<th>\u53c2\u6570\u540d<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>log_bin = {on | off | base_name}<\/td>\n<td>\u6307\u5b9a\u662f\u5426\u542f\u7528\u8bb0\u5f55\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6216\u8005\u6307\u5b9a\u4e00\u4e2a\u65e5\u5fd7\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td>sql_log_bin ={ on | off }<\/td>\n<td>\u6307\u5b9a\u662f\u5426\u542f\u7528\u8bb0\u5f55\u4e8c\u8fdb\u5236\u65e5\u5fd7<\/td>\n<\/tr>\n<tr>\n<td>expire_logs_days<\/td>\n<td>\u6307\u5b9a\u81ea\u52a8\u5220\u9664\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7684\u65f6\u95f4\uff0c\u5373\u65e5\u5fd7\u8fc7\u671f\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>log_bin_index<\/td>\n<td>\u6307\u5b9amysql-bin.index\u6587\u4ef6\u7684\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td>binlog_format = { mixed | row | statement }<\/td>\n<td>\u6307\u5b9a\u4e8c\u8fdb\u5236\u65e5\u5fd7\u57fa\u4e8e\u4ec0\u4e48\u6a21\u5f0f\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td>max_binlog_size<\/td>\n<td>\u6307\u5b9a\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6\u6700\u5927\u503c<\/td>\n<\/tr>\n<tr>\n<td>binlog_cache_size<\/td>\n<td>\u6307\u5b9a\u4e8b\u52a1\u65e5\u5fd7\u7f13\u5b58\u533a\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>max_binlog_cache_size<\/td>\n<td>\u6307\u5b9a\u4e8c\u8fdb\u5236\u65e5\u5fd7\u7f13\u5b58\u6700\u5927\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>sync_binlog = { 0 | n }<\/td>\n<td>\u6307\u5b9a\u5199\u7f13\u51b2\u591a\u5c11\u6b21\uff0c\u5237\u4e00\u6b21\u76d8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\\\"\u4e94\u3001\u5b9e\u8df5\\\" style=\\\"padding-left: 40px;\\\">\u4e94\u3001MySQL binlog\u65e5\u5fd7\u67e5\u770b<\/h2>\n<h3 id=\\\"51-\u68c0\u67e5-binlog-\u529f\u80fd\u662f\u5426\u5f00\u542f\\\" style=\\\"padding-left: 40px;\\\">5.1 \u68c0\u67e5 binlog \u529f\u80fd\u662f\u5426\u5f00\u542f<\/h3>\n<pre style=\\\"padding-left: 40px;\\\">mysql&gt; show variables like \\'log_bin\\';\n+---------------+-------+\n| Variable_name | Value |\n+---------------+-------+\n| log_bin       | OFF   |\n+---------------+-------+\n1 row in set (0.00 sec)\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u53ef\u4ee5\u770b\u5230\u00a0binlog\u00a0\u529f\u80fd\u9ed8\u8ba4\u662f\u5173\u95ed\u7684\u3002<\/p>\n<h3 id=\\\"52-\u5f00\u542f-binlog--\u529f\u80fd\\\" style=\\\"padding-left: 40px;\\\">5.2 \u5f00\u542f binlog \u529f\u80fd<\/h3>\n<p style=\\\"padding-left: 40px;\\\">\u521b\u5efa\u00a0binlog\u00a0\u5b58\u653e\u8def\u5f84\u5e76\u8d4b\u6743<\/p>\n<pre style=\\\"padding-left: 40px;\\\">[root@VM_0_15_centos log]# mkdir -p \/var\/log\/mysql\n[root@VM_0_15_centos log]# chown -R mysql.mysql \/var\/log\/mysql\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u7f16\u8f91\u00a0my.cnf\u00a0\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre style=\\\"padding-left: 40px;\\\">[root@VM_0_15_centos log]# vim \/etc\/my.cnf\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u5728\u00a0[mysqld]\u00a0\u6807\u7b7e\u4e0b\u7f16\u8f91\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\"># binlog \u5b58\u653e\u8def\u5f84\nlog_bin=\/var\/log\/mysql\/mysql-bin\n# \u670d\u52a1Id,\u4fdd\u6301\u552f\u4e00\nserver_id=1\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u91cd\u542f MySQL \u670d\u52a1<\/p>\n<pre style=\\\"padding-left: 40px;\\\">[root@VM_0_15_centos log]# systemctl restart mysqld\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u91cd\u65b0\u68c0\u67e5\u662f\u5426\u5f00\u542f\u65e5\u5fd7\u529f\u80fd<\/p>\n<pre style=\\\"padding-left: 40px;\\\">mysql&gt; show variables like \\'log_bin\\';\n+---------------+-------+\n| Variable_name | Value |\n+---------------+-------+\n| log_bin       | ON    |\n+---------------+-------+\n1 row in set (0.00 sec)\n<\/pre>\n<h3 id=\\\"53-\u67e5\u770b-binlog-\u6a21\u5f0f\\\" style=\\\"padding-left: 40px;\\\">5.3 \u67e5\u770b binlog \u6a21\u5f0f<\/h3>\n<pre style=\\\"padding-left: 40px;\\\">mysql&gt; show variables like \\'%binlog_format%\\';\n+---------------+-------+\n| Variable_name | Value |\n+---------------+-------+\n| binlog_format | ROW   |\n+---------------+-------+\n1 row in set (0.00 sec)\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u53ef\u4ee5\u770b\u5230\u00a0MySQL 5.7\u00a0\u7248\u672c\u9ed8\u8ba4\u00a0binlog\u00a0\u6a21\u5f0f\u662f\u00a0ROW\u3002<\/p>\n<h3 id=\\\"54-\u4fee\u6539-binlog-\u6a21\u5f0f\\\" style=\\\"padding-left: 40px;\\\">5.4 \u4fee\u6539 binlog \u6a21\u5f0f<\/h3>\n<p style=\\\"padding-left: 40px;\\\">\u7f16\u8f91\u00a0my.cnf\u00a0\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre style=\\\"padding-left: 40px;\\\">[root@VM_0_15_centos log]# vim \/etc\/my.cnf\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u5728\u00a0[mysqld]\u00a0\u6807\u7b7e\u4e0b\u7f16\u8f91\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">binlog_format=mixed\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u91cd\u542f\u670d\u52a1<\/p>\n<pre style=\\\"padding-left: 40px;\\\">[root@VM_0_15_centos log]# systemctl restart mysqld\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u00a0binlog\u00a0\u6a21\u5f0f<\/p>\n<pre style=\\\"padding-left: 40px;\\\">mysql&gt; show variables like \\'%binlog_format%\\';\n+---------------+-------+\n| Variable_name | Value |\n+---------------+-------+\n| binlog_format | MIXED |\n+---------------+-------+\n1 row in set (0.00 sec)\n<\/pre>\n<h3 id=\\\"55-\u67e5\u770b-binlog--\u6587\u4ef6\\\" style=\\\"padding-left: 40px;\\\">5.5 \u67e5\u770b binlog \u6587\u4ef6<\/h3>\n<p style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u00a0statement\u00a0\u6a21\u5f0f\u6587\u4ef6\uff1a<\/p>\n<pre style=\\\"padding-left: 40px;\\\">mysqlbinlog \u6587\u4ef6\u540d\n<\/pre>\n<p style=\\\"padding-left: 40px;\\\">\u67e5\u770b\u00a0row\u00a0\u6a21\u5f0f\u6587\u4ef6<\/p>\n<pre style=\\\"padding-left: 40px;\\\">mysqlbinlog -vv\t\u6587\u4ef6\u540d\n<\/pre>\n<h3 id=\\\"56-\u5220\u9664-binlog-\u6587\u4ef6\\\" style=\\\"padding-left: 40px;\\\">5.6 \u5220\u9664 binlog \u6587\u4ef6<\/h3>\n<ul>\n<li>reset master<br \/>\n\u6b64\u547d\u4ee4\u5c06\u4f1a\u5220\u9664\u6240\u6709\u65e5\u5fd7\uff0c\u5e76\u8ba9\u65e5\u5fd7\u6587\u4ef6\u91cd\u65b0\u4ece000001\u5f00\u59cb\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u5c06\u4f1a\u6e05\u7a7a 00000X \u4e4b\u524d\u7684\u6240\u6709\u65e5\u5fd7\u6587\u4ef6<br \/>\nPURGE { BINARY | MASTER } LOGS { TO \\&#8217;log_name\\&#8217; | BEFORE datetime_expr }<br \/>\npurge master logs to \\&#8221;binlog_name.00000X\\&#8221;<\/li>\n<li>\u4fee\u6539 my.cnf \u914d\u7f6e<\/li>\n<\/ul>\n<p style=\\\"padding-left: 40px;\\\">expire_logs_days = 3 #\u4fdd\u7559\u6700\u8fd13\u5929\u7684binlog;\u9ed8\u8ba4\u503c\u662f0,\u8868\u793a\u4e0d\u81ea\u52a8\u5220\u9664.<\/p>\n<h3 id=\\\"56-\u5220\u9664-binlog-\u6587\u4ef6\\\" style=\\\"padding-left: 40px;\\\">5.7\u767b\u5f55\u5230mysql\u67e5\u770bbinlog<\/h3>\n<ul>\n<li>\u53ea\u67e5\u770b\u7b2c\u4e00\u4e2abinlog\u6587\u4ef6\u7684\u5185\u5bb9<\/li>\n<\/ul>\n<pre style=\\\"padding-left: 40px;\\\">show binlog events;<\/pre>\n<ul>\n<li>\u67e5\u770b\u6307\u5b9abinlog\u6587\u4ef6\u7684\u5185\u5bb9<\/li>\n<\/ul>\n<pre style=\\\"padding-left: 40px;\\\">show binlog events in \\'mysql-bin.000002\\';<\/pre>\n<ul>\n<li>\u67e5\u770b\u5f53\u524d\u6b63\u5728\u5199\u5165\u7684binlog\u6587\u4ef6<\/li>\n<\/ul>\n<pre style=\\\"padding-left: 40px;\\\">show master status\\\\G<\/pre>\n<ul>\n<li>\u83b7\u53d6binlog\u6587\u4ef6\u5217\u8868<\/li>\n<\/ul>\n<pre>show binary logs;<\/pre>\n<h3 id=\\\"56-\u5220\u9664-binlog-\u6587\u4ef6\\\" style=\\\"padding-left: 40px;\\\">5.8\u7528mysqlbinlog\u5de5\u5177\u67e5\u770b<\/h3>\n<ul>\n<li>\u4e0d\u8981\u67e5\u770b\u5f53\u524d\u6b63\u5728\u5199\u5165\u7684binlog\u6587\u4ef6<\/li>\n<li>\u4e0d\u8981\u52a0&#8211;force\u53c2\u6570\u5f3a\u5236\u8bbf\u95ee<\/li>\n<li>\u5982\u679cbinlog\u683c\u5f0f\u662f\u884c\u6a21\u5f0f\u7684,\u8bf7\u52a0 -vv\u53c2\u6570<\/li>\n<\/ul>\n<h2 id=\\\"h2_4\\\" style=\\\"padding-left: 40px;\\\">\u672c\u5730\u67e5\u770b<\/h2>\n<ul>\n<li>\u57fa\u4e8e\u5f00\u59cb\/\u7ed3\u675f\u65f6\u95f4<\/li>\n<\/ul>\n<pre style=\\\"padding-left: 40px;\\\">mysqlbinlog --start-datetime=\\'2013-09-10 00:00:00\\' --stop-datetime=\\'2013-09-10 01:01:01\\' -d \u5e93\u540d \u4e8c\u8fdb\u5236\u6587\u4ef6<\/pre>\n<ul>\n<li>\u57fa\u4e8epos\u503c<\/li>\n<\/ul>\n<pre style=\\\"padding-left: 40px;\\\">mysqlbinlog --start-postion=107 --stop-position=1000 -d \u5e93\u540d \u4e8c\u8fdb\u5236\u6587\u4ef6<\/pre>\n<ul>\n<li>\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6587\u672c<\/li>\n<\/ul>\n<pre>mysqlbinlog \u2013base64-output=DECODE-ROWS -v -d \u5e93\u540d \u4e8c\u8fdb\u5236\u6587\u4ef6<\/pre>\n<div style=\\\"padding-left: 40px;\\\"><\/div>\n<\/div>\n<ul>\n<li>\u63d0\u53d6\u6307\u5b9a\u7684binlog\u65e5\u5fd7\n<pre># mysqlbinlog \/opt\/data\/APP01bin.000001\n# mysqlbinlog \/opt\/data\/APP01bin.000001|grep insert\n\/*!40019 SET @@session.max_insert_delayed_threads=0*\/;\ninsert into tb values(2,\\'jack\\')<\/pre>\n<\/li>\n<\/ul>\n<ul>\n<li>\u63d0\u53d6\u6307\u5b9aposition\u4f4d\u7f6e\u7684binlog\u65e5\u5fd7\n<pre># mysqlbinlog --start-position=\\\"120\\\" --stop-position=\\\"332\\\" \/opt\/data\/APP01bin.000001<\/pre>\n<\/li>\n<\/ul>\n<ul>\n<li>\u63d0\u53d6\u6307\u5b9aposition\u4f4d\u7f6e\u7684binlog\u65e5\u5fd7\u5e76\u8f93\u51fa\u5230\u538b\u7f29\u6587\u4ef6\n<pre># mysqlbinlog --start-position=\\\"120\\\" --stop-position=\\\"332\\\" \/opt\/data\/APP01bin.000001 |gzip &gt;extra_01.sql.gz<\/pre>\n<\/li>\n<li>\u63d0\u53d6\u6307\u5b9aposition\u4f4d\u7f6e\u7684binlog\u65e5\u5fd7\u5bfc\u5165\u6570\u636e\u5e93\n<pre># mysqlbinlog --start-position=\\\"120\\\" --stop-position=\\\"332\\\" \/opt\/data\/APP01bin.000001 | mysql -uroot -p<\/pre>\n<\/li>\n<li>\u63d0\u53d6\u6307\u5b9a\u5f00\u59cb\u65f6\u95f4\u7684binlog\u5e76\u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6\n<pre># mysqlbinlog --start-datetime=\\\"2014-12-15 20:15:23\\\" \/opt\/data\/APP01bin.000002 --result-file=extra02.sql<\/pre>\n<\/li>\n<li>\u63d0\u53d6\u6307\u5b9a\u4f4d\u7f6e\u7684\u591a\u4e2abinlog\u65e5\u5fd7\u6587\u4ef6\n<pre># mysqlbinlog --start-position=\\\"120\\\" --stop-position=\\\"332\\\" \/opt\/data\/APP01bin.000001 \/opt\/data\/APP01bin.000002|more<\/pre>\n<\/li>\n<li>\u63d0\u53d6\u6307\u5b9a\u6570\u636e\u5e93binlog\u5e76\u8f6c\u6362\u5b57\u7b26\u96c6\u5230UTF8<\/li>\n<\/ul>\n<pre># mysqlbinlog --database=test --set-charset=utf8 \/opt\/data\/APP01bin.000001 \/opt\/data\/APP01bin.000002 &gt;test.sql<\/pre>\n<h3 id=\\\"h2_5\\\" style=\\\"padding-left: 40px;\\\">\u8fdc\u7a0b\u67e5\u770b<\/h3>\n<ul>\n<li>\u6307\u5b9a\u5f00\u59cb\/\u7ed3\u675f\u65f6\u95f4,\u5e76\u628a\u7ed3\u679c\u91cd\u5b9a\u5411\u5230\u672c\u5730t.binlog\u6587\u4ef6\u4e2d.<\/li>\n<\/ul>\n<pre style=\\\"padding-left: 40px;\\\">mysqlbinlog -u username -p password -h192.168.1.116 -P3306 --read-from-remote-server --start-datetime=\\'2018-09-10 20:00:00\\' --stop-datetime=\\'2018-09-10 21:00:00\\' mysql-bin.000001 &gt; t.binlog<\/pre>\n<ul>\n<li>\u8fdc\u7a0b\u63d0\u53d6\u65e5\u5fd7\uff0c\u6307\u5b9a\u7ed3\u675f\u65f6\u95f4\n<pre># mysqlbinlog -urobin -p -h192.168.1.116 -P3306 --stop-datetime=\\\"2014-12-15 20:30:23\\\" --read-from-remote-server mysql-bin.000033 |more<\/pre>\n<\/li>\n<li>\u8fdc\u7a0b\u63d0\u53d6\u4f7f\u7528row\u683c\u5f0f\u7684binlog\u65e5\u5fd7\u5e76\u8f93\u51fa\u5230\u672c\u5730\u6587\u4ef6\n<pre># mysqlbinlog -uroot -p -P3606 -h192.168.1.177 --read-from-remote-server -vv inst3606bin.000005 &gt;row.sql<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ec0\u4e48\u662fbinlog binlog\u00a0\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u7528\u4e8e\u8bb0\u5f55\u7528\u6237\u5bf9\u6570\u636e\u5e93\u00a0\u66f4\u65b0\u7684SQL\u8bed\u53e5\u00a0\u4fe1\u606f\uff0c\u4f8b &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1306\"> <span class=\"screen-reader-text\">MySQL binlog\u65e5\u5fd7\u67e5\u770b<\/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],"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":"\u4e00\u3001\u4ec0\u4e48\u662fbinlog binlog\u00a0\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u7528\u4e8e\u8bb0\u5f55\u7528\u6237\u5bf9\u6570\u636e\u5e93\u00a0\u66f4\u65b0\u7684SQL\u8bed\u53e5\u00a0\u4fe1\u606f\uff0c\u4f8b&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1306"}],"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=1306"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1306\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1306"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}