{"id":2132,"date":"2025-08-08T15:52:13","date_gmt":"2025-08-08T07:52:13","guid":{"rendered":"https:\/\/www.hgfwq.com\/blog\/?p=2132"},"modified":"2025-08-08T15:52:13","modified_gmt":"2025-08-08T07:52:13","slug":"%e8%a7%a3%e6%9e%90binlog%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e5%b7%a5%e5%85%b7%e4%bd%bf%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.hgfwq.com\/blog\/2132.html","title":{"rendered":"\u89e3\u6790binlog\u4ece\u57fa\u7840\u5230\u5de5\u5177\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"<p><h3>\u4ec0\u4e48\u662fbinlog\uff1f<\/h3>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.hgfwq.com\/blog\/wp-content\/uploads\/2025\/07\/bh4LX.jpg\" alt=\"\u89e3\u6790binlog\u4ece\u57fa\u7840\u5230\u5de5\u5177\u4f7f\u7528\u8be6\u89e3\" title=\"\u89e3\u6790binlog\u4ece\u57fa\u7840\u5230\u5de5\u5177\u4f7f\u7528\u8be6\u89e3\"><\/p>\n<p><p>binlog\uff0c\u5373Binary Log\uff0c\u662fMySQL\u6570\u636e\u5e93\u7684\u4e00\u79cd\u65e5\u5fd7\u8bb0\u5f55\u65b9\u5f0f\uff0c\u7528\u4e8e\u8bb0\u5f55\u6570\u636e\u5e93\u7684\u53d8\u66f4\u64cd\u4f5c\u3002binlog\u8bb0\u5f55\u4e86\u6240\u6709\u5bf9\u6570\u636e\u5e93\u6570\u636e\u7684\u53d8\u66f4\uff0c\u5305\u62ec\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u8fd9\u4e9b\u53d8\u66f4\u8bb0\u5f55\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6570\u636e\u6062\u590d\u3001\u5ba1\u8ba1\u3001\u590d\u5236\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>binlog\u5305\u542b\u54ea\u4e9b\u5185\u5bb9\uff1f<\/h3>\n<\/p>\n<p><p>binlog\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u53d8\u66f4\u524d\u7684\u6570\u636e<\/p>\n<\/li>\n<li>\n<p><p>\u53d8\u66f4\u540e\u7684\u6570\u636e<\/p>\n<\/li>\n<li>\n<p><p>\u53d8\u66f4\u7684\u7c7b\u578b\uff0c\u5982INSERT\u3001UPDATE\u3001DELETE\u7b49<\/p>\n<\/li>\n<li>\n<p><p>\u53d8\u66f4\u7684\u65f6\u95f4\u6233<\/p>\n<\/li>\n<li>\n<p><p>\u6267\u884c\u53d8\u66f4\u7684SQL\u8bed\u53e5<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u5982\u4f55\u89e3\u6790binlog\uff1f<\/h3>\n<\/p>\n<p><p>\u89e3\u6790binlog\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u4f7f\u7528MySQL\u81ea\u5e26\u7684binlog\u5de5\u5177\uff0c\u5982mysqlbinlog<\/p>\n<\/li>\n<li>\n<p><p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u5982MySQL Workbench\u3001Navicat\u7b49<\/p>\n<\/li>\n<li>\n<p><p>\u7f16\u5199\u7a0b\u5e8f\uff0c\u5982Python\u3001Java\u7b49\uff0c\u901a\u8fc7\u8bfb\u53d6binlog\u6587\u4ef6\u6765\u89e3\u6790\u6570\u636e<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u63a8\u8350\u4f7f\u7528\u54ea\u4e9b\u5de5\u5177\u6765\u89e3\u6790binlog\uff1f<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u51e0\u4e2a\u63a8\u8350\u7684\u89e3\u6790binlog\u7684\u5de5\u5177\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>mysqlbinlog\uff1aMySQL\u5b98\u65b9\u63d0\u4f9b\u7684binlog\u89e3\u6790\u5de5\u5177\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u8f83\u4e3a\u590d\u6742<\/p>\n<\/li>\n<li>\n<p><p>MySQL Workbench\uff1a\u56fe\u5f62\u5316\u754c\u9762\uff0c\u64cd\u4f5c\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005<\/p>\n<\/li>\n<li>\n<p><p>Navicat\uff1a\u529f\u80fd\u4e30\u5bcc\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u754c\u9762\u53cb\u597d<\/p>\n<\/li>\n<li>\n<p><p>Python\uff1a\u901a\u8fc7\u7f16\u5199Python\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u89e3\u6790binlog\uff0c\u5e76\u5b9e\u73b0\u81ea\u5b9a\u4e49\u529f\u80fd<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e3a\u4ec0\u4e48\u9700\u8981\u89e3\u6790binlog\uff1f<\/h3>\n<\/p>\n<p><p>\u89e3\u6790binlog\u6709\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u6570\u636e\u6062\u590d\uff1a\u5f53\u6570\u636e\u5e93\u51fa\u73b0\u6545\u969c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7binlog\u8fdb\u884c\u6570\u636e\u6062\u590d<\/p>\n<\/li>\n<li>\n<p><p>\u6570\u636e\u5ba1\u8ba1\uff1a\u901a\u8fc7\u89e3\u6790binlog\uff0c\u53ef\u4ee5\u4e86\u89e3\u6570\u636e\u5e93\u7684\u53d8\u66f4\u60c5\u51b5\uff0c\u8fdb\u884c\u6570\u636e\u5ba1\u8ba1<\/p>\n<\/li>\n<li>\n<p><p>\u6570\u636e\u540c\u6b65\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790binlog\u5b9e\u73b0\u6570\u636e\u540c\u6b65<\/p>\n<\/li>\n<li>\n<p><p>\u5f00\u53d1\u6d4b\u8bd5\uff1a\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790binlog\u8fdb\u884c\u6570\u636e\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/p>\n<\/li>\n<\/ul>\n<p><b>\u95ee\u98981\uff1a\u5982\u4f55\u4f7f\u7528mysqlbinlog\u5de5\u5177\u89e3\u6790binlog\uff1f<\/b><\/p>\n<p><p>\u4f7f\u7528mysqlbinlog\u5de5\u5177\u89e3\u6790binlog\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>mysqlbinlog \/path\/to\/binlog\/file | grep -v 'Checksum' | less<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><b>\u95ee\u98982\uff1a\u5982\u4f55\u901a\u8fc7Python\u7a0b\u5e8f\u89e3\u6790binlog\uff1f<\/b><\/p>\n<p><p>\u901a\u8fc7Python\u7a0b\u5e8f\u89e3\u6790binlog\uff0c\u53ef\u4ee5\u4f7f\u7528pymysql\u5e93\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code>import pymysql<\/p>\r\n\r\n<p> \u8fde\u63a5MySQL\u6570\u636e\u5e93<\/p>\r\n<p>conn = pymysql.connect(host='localhost', user='root', password='password', db='database')<\/p>\r\n\r\n<p> \u521b\u5efa\u6e38\u6807<\/p>\r\n<p>cursor = conn.cursor()<\/p>\r\n\r\n<p> \u6267\u884c\u67e5\u8be2<\/p>\r\n<p>cursor.execute(\"SHOW BINARY LOGS\")<\/p>\r\n\r\n<p> \u83b7\u53d6binlog\u5217\u8868<\/p>\r\n<p>binlogs = cursor.fetchall()<\/p>\r\n\r\n<p>for binlog in binlogs:<\/p>\r\n<p>    print(binlog)<\/p>\r\n\r\n<p> \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/p>\r\n<p>cursor.close()<\/p>\r\n<p>conn.close()<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><b>\u95ee\u98983\uff1a\u89e3\u6790binlog\u65f6\uff0c\u5982\u4f55\u8fc7\u6ee4\u51fa\u7279\u5b9a\u7684SQL\u8bed\u53e5\uff1f<\/b><\/p>\n<p><p>\u5728\u89e3\u6790binlog\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fc7\u6ee4\u51fa\u7279\u5b9a\u7684SQL\u8bed\u53e5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code>import pymysql<\/p>\r\n<p>import re<\/p>\r\n\r\n<p> \u8fde\u63a5MySQL\u6570\u636e\u5e93<\/p>\r\n<p>conn = pymysql.connect(host='localhost', user='root', password='password', db='database')<\/p>\r\n\r\n<p> \u521b\u5efa\u6e38\u6807<\/p>\r\n<p>cursor = conn.cursor()<\/p>\r\n\r\n<p> \u6267\u884c\u67e5\u8be2<\/p>\r\n<p>cursor.execute(\"SHOW BINARY LOGS\")<\/p>\r\n\r\n<p> \u83b7\u53d6binlog\u5217\u8868<\/p>\r\n<p>binlogs = cursor.fetchall()<\/p>\r\n\r\n<p> \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5339\u914dINSERT\u8bed\u53e5<\/p>\r\n<p>pattern = re.compile(r\"INSERT INTO `table` VALUES \\(([^)]+)\\)\")<\/p>\r\n\r\n<p>for binlog in binlogs:<\/p>\r\n<p>    print(binlog)<\/p>\r\n<p>    cursor.execute(f\"SHOW BINARY LOGS {binlog[0]}\")<\/p>\r\n\r\n<p>     \u83b7\u53d6binlog\u5185\u5bb9<\/p>\r\n<p>    binlog_content = cursor.fetchall()<\/p>\r\n<p>    for line in binlog_content:<\/p>\r\n<p>         \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dSQL\u8bed\u53e5<\/p>\r\n<p>        match = pattern.search(line[0])<\/p>\r\n<p>        if match:<\/p>\r\n<p>            print(match.group(0))<\/p>\r\n\r\n<p> \u5173\u95ed\u6e38\u6807\u548c\u8fde\u63a5<\/p>\r\n<p>cursor.close()<\/p>\r\n<p>conn.close()<\/p>\r\n<p><\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fbinlog\uff1f binlog\uff0c\u5373Binary Log\uff0c\u662fMySQL\u6570\u636e\u5e93\u7684\u4e00\u79cd\u65e5\u5fd7\u8bb0\u5f55\u65b9\u5f0f\uff0c\u7528\u4e8e\u8bb0\u5f55\u6570\u636e\u5e93\u7684\u53d8\u66f4\u64cd\u4f5c\u3002binlog\u8bb0\u5f55\u4e86\u6240\u6709\u5bf9\u6570\u636e\u5e93\u6570\u636e\u7684\u53d8\u66f4\uff0c\u5305\u62ec\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u8fd9\u4e9b\u53d8\u66f4\u8bb0\u5f55\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6570\u636e\u6062\u590d\u3001\u5ba1\u8ba1\u3001\u590d&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2133,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[716,326,717],"topic":[],"class_list":["post-2132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haiwai","tag-binlog","tag-mysql","tag-717"],"_links":{"self":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/2132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/comments?post=2132"}],"version-history":[{"count":1,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/2132\/revisions"}],"predecessor-version":[{"id":2135,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/2132\/revisions\/2135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media\/2133"}],"wp:attachment":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media?parent=2132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/categories?post=2132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/tags?post=2132"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/topic?post=2132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}