{"id":66455,"date":"2024-10-14T17:40:14","date_gmt":"2024-10-14T09:40:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/66455\/"},"modified":"2024-10-14T17:40:14","modified_gmt":"2024-10-14T09:40:14","slug":"mysql-bin%e6%97%a5%e8%aa%8c%e6%96%87%e4%bb%b6%e6%b8%85%e9%99%a4sh%e7%9a%84%e5%af%a6%e4%be%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/66455\/","title":{"rendered":"mysql bin\u65e5\u8a8c\u6587\u4ef6\u6e05\u9664sh\u7684\u5be6\u4f8b"},"content":{"rendered":"<h1 id=\"mysql-bin%e6%97%a5%e8%aa%8c%e6%96%87%e4%bb%b6%e6%b8%85%e9%99%a4sh%e7%9a%84%e5%af%a6%e4%be%8b-ZtKrKFpebc\">MySQL Bin\u65e5\u8a8c\u6587\u4ef6\u6e05\u9664sh\u7684\u5be6\u4f8b<\/h1>\n<p>\u5728\u4f7f\u7528MySQL\u6578\u64da\u5eab\u7684\u904e\u7a0b\u4e2d\uff0cBin\u65e5\u8a8c\uff08Binary Log\uff09\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\u3002\u5b83\u8a18\u9304\u4e86\u6240\u6709\u66f4\u6539\u6578\u64da\u7684\u64cd\u4f5c\uff0c\u9019\u5c0d\u65bc\u6578\u64da\u6062\u5fa9\u548c\u4e3b\u5f9e\u8907\u88fd\u975e\u5e38\u91cd\u8981\u3002\u7136\u800c\uff0c\u96a8\u8457\u6642\u9593\u7684\u63a8\u79fb\uff0c\u9019\u4e9b\u65e5\u8a8c\u6587\u4ef6\u53ef\u80fd\u6703\u4f54\u7528\u5927\u91cf\u7684\u78c1\u789f\u7a7a\u9593\uff0c\u56e0\u6b64\u5b9a\u671f\u6e05\u9664\u904e\u671f\u7684Bin\u65e5\u8a8c\u6587\u4ef6\u662f\u5fc5\u8981\u7684\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Shell\u8173\u672c\u4f86\u6e05\u9664MySQL\u7684Bin\u65e5\u8a8c\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afmysql-bin%e6%97%a5%e8%aa%8c%ef%bc%9f-ZtKrKFpebc\">\u4ec0\u9ebc\u662fMySQL Bin\u65e5\u8a8c\uff1f<\/h2>\n<p>MySQL\u7684Bin\u65e5\u8a8c\u662f\u4e00\u7a2e\u8a18\u9304\u6240\u6709\u66f4\u6539\u6578\u64da\u7684\u65e5\u8a8c\u6587\u4ef6\u3002\u9019\u4e9b\u65e5\u8a8c\u4e0d\u50c5\u7528\u65bc\u6578\u64da\u6062\u5fa9\uff0c\u9084\u7528\u65bc\u4e3b\u5f9e\u8907\u88fd\u3002\u7576\u6578\u64da\u5eab\u9032\u884c\u63d2\u5165\u3001\u66f4\u65b0\u6216\u522a\u9664\u64cd\u4f5c\u6642\uff0c\u9019\u4e9b\u64cd\u4f5c\u6703\u88ab\u8a18\u9304\u5230Bin\u65e5\u8a8c\u4e2d\u3002\u9019\u4f7f\u5f97\u5728\u7cfb\u7d71\u5d29\u6f70\u6216\u6545\u969c\u6642\uff0c\u53ef\u4ee5\u901a\u904e\u9019\u4e9b\u65e5\u8a8c\u4f86\u6062\u5fa9\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e6%b8%85%e9%99%a4bin%e6%97%a5%e8%aa%8c%ef%bc%9f-ZtKrKFpebc\">\u70ba\u4ec0\u9ebc\u9700\u8981\u6e05\u9664Bin\u65e5\u8a8c\uff1f<\/h2>\n<p>\u96a8\u8457\u6642\u9593\u7684\u63a8\u79fb\uff0cBin\u65e5\u8a8c\u6587\u4ef6\u6703\u4e0d\u65b7\u589e\u9577\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li>\u4f54\u7528\u5927\u91cf\u78c1\u789f\u7a7a\u9593\uff0c\u5f71\u97ff\u7cfb\u7d71\u6027\u80fd\u3002<\/li>\n<li>\u5f71\u97ff\u6578\u64da\u5eab\u7684\u5099\u4efd\u548c\u6062\u5fa9\u901f\u5ea6\u3002<\/li>\n<li>\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u5eab\u7121\u6cd5\u6b63\u5e38\u904b\u884c\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u5b9a\u671f\u6e05\u9664\u904e\u671f\u7684Bin\u65e5\u8a8c\u6587\u4ef6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<h2 id=\"%e6%b8%85%e9%99%a4bin%e6%97%a5%e8%aa%8c%e7%9a%84%e5%9f%ba%e6%9c%ac%e5%91%bd%e4%bb%a4-ZtKrKFpebc\">\u6e05\u9664Bin\u65e5\u8a8c\u7684\u57fa\u672c\u547d\u4ee4<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u6e05\u9664Bin\u65e5\u8a8c\uff1a<\/p>\n<pre><code>PURGE BINARY LOGS TO 'mysql-bin.00000X';<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u6703\u6e05\u9664\u6240\u6709\u5728\u6307\u5b9aBin\u65e5\u8a8c\u4e4b\u524d\u7684\u65e5\u8a8c\u6587\u4ef6\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u6e05\u9664\u6307\u5b9a\u6642\u9593\u4e4b\u524d\u7684\u6240\u6709Bin\u65e5\u8a8c\uff1a<\/p>\n<pre><code>PURGE BINARY LOGS BEFORE 'YYYY-MM-DD HH:MM:SS';<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8shell%e8%85%b3%e6%9c%ac%e8%87%aa%e5%8b%95%e6%b8%85%e9%99%a4bin%e6%97%a5%e8%aa%8c-ZtKrKFpebc\">\u4f7f\u7528Shell\u8173\u672c\u81ea\u52d5\u6e05\u9664Bin\u65e5\u8a8c<\/h2>\n<p>\u70ba\u4e86\u81ea\u52d5\u5316\u6e05\u9664Bin\u65e5\u8a8c\u7684\u904e\u7a0b\uff0c\u6211\u5011\u53ef\u4ee5\u7de8\u5beb\u4e00\u500bShell\u8173\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>#!\/bin\/bash\n\n# \u8a2d\u5b9aMySQL\u7528\u6236\u540d\u548c\u5bc6\u78bc\nMYSQL_USER=\"your_username\"\nMYSQL_PASSWORD=\"your_password\"\n\n# \u8a2d\u5b9a\u8981\u4fdd\u7559\u7684Bin\u65e5\u8a8c\u6578\u91cf\nKEEP_LOGS=5\n\n# \u7372\u53d6\u7576\u524d\u7684Bin\u65e5\u8a8c\u6587\u4ef6\nCURRENT_LOG=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e \"SHOW BINARY LOGS;\" | awk 'NR==2 {print $1}')\n\n# \u7372\u53d6\u8981\u522a\u9664\u7684Bin\u65e5\u8a8c\u6587\u4ef6\nDELETE_LOG=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e \"SHOW BINARY LOGS;\" | awk -v keep=\"$KEEP_LOGS\" 'NR&gt;keep {print $1}')\n\n# \u6e05\u9664Bin\u65e5\u8a8c\nfor log in $DELETE_LOG; do\n    mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e \"PURGE BINARY LOGS TO '$log';\"\ndone\n\necho \"\u5df2\u6210\u529f\u6e05\u9664\u904e\u671f\u7684Bin\u65e5\u8a8c\u6587\u4ef6\u3002\"\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u8173\u672c\u4e2d\uff0c\u6211\u5011\u9996\u5148\u8a2d\u7f6e\u4e86MySQL\u7684\u7528\u6236\u540d\u548c\u5bc6\u78bc\uff0c\u7136\u5f8c\u6307\u5b9a\u8981\u4fdd\u7559\u7684Bin\u65e5\u8a8c\u6578\u91cf\u3002\u63a5\u8457\uff0c\u6211\u5011\u7372\u53d6\u7576\u524d\u7684Bin\u65e5\u8a8c\u6587\u4ef6\uff0c\u4e26\u6839\u64da\u4fdd\u7559\u7684\u6578\u91cf\u8a08\u7b97\u51fa\u9700\u8981\u522a\u9664\u7684\u65e5\u8a8c\u6587\u4ef6\u3002\u6700\u5f8c\uff0c\u6211\u5011\u4f7f\u7528\u5faa\u74b0\u4f86\u6e05\u9664\u9019\u4e9b\u904e\u671f\u7684Bin\u65e5\u8a8c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ZtKrKFpebc\">\u7e3d\u7d50<\/h2>\n<p>\u5b9a\u671f\u6e05\u9664MySQL\u7684Bin\u65e5\u8a8c\u6587\u4ef6\u662f\u7dad\u8b77\u6578\u64da\u5eab\u6027\u80fd\u7684\u91cd\u8981\u6b65\u9a5f\u3002\u901a\u904e\u4f7f\u7528Shell\u8173\u672c\u81ea\u52d5\u5316\u9019\u4e00\u904e\u7a0b\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u78c1\u789f\u7a7a\u9593\uff0c\u4e26\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u904b\u884c\u3002\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u6258\u7ba1\u60a8\u7684MySQL\u6578\u64da\u5eab\uff0c\u4eab\u53d7\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u6e05\u9664MySQL bin\u65e5\u8a8c\u6587\u4ef6\u7684\u5be6\u4f8b\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u904b\u884c\u6548\u7387\uff0c\u907f\u514d\u7a7a\u9593\u6d6a\u8cbb\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-66455","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=66455"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66455\/revisions"}],"predecessor-version":[{"id":66456,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/66455\/revisions\/66456"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=66455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=66455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=66455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}