{"id":1388,"date":"2014-12-05T06:25:16","date_gmt":"2014-12-05T06:25:16","guid":{"rendered":"https:\/\/qbytes.cloud\/?p=1388"},"modified":"2014-12-05T06:25:16","modified_gmt":"2014-12-05T06:25:16","slug":"delete-mysql-bin-files-safely","status":"publish","type":"post","link":"https:\/\/www.qbytes.cloud\/index.php\/2014\/12\/05\/delete-mysql-bin-files-safely\/","title":{"rendered":"Delete mysql-bin files safely"},"content":{"rendered":"<p>The file mysql-bin.index keeps a list of all binary logs mysqld has generated and auto-rotated. The mechanisms for cleaning out the binlogs in conjunction with mysql-bin.index are:<\/p>\n<p>PURGE BINARY LOGS TO &#8216;binlogname&#8217;;<br \/>\nPURGE BINARY LOGS BEFORE &#8216;datetimestamp&#8217;;<\/p>\n<p>These will clear all binary logs before the binlog or timestamp you just specified.<\/p>\n<p>For example, if you login to mysql run<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nmysql&gt; PURGE BINARY LOGS TO &#039;mysql-bin.000223&#039;;\n\n<\/pre>\n<p>this will erase all binary logs before &#8216;mysql-bin.000223&#8217;.<\/p>\n<p>If you run<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nmysql&gt; PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;\n\n<\/pre>\n<p>this will erase all binary logs before midnight 3 days ago.<\/p>\n<p>If you want to have binlog rotated away automatically and keep 3 days woth, simply set this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nmysql&gt; SET GLOBAL expire_logs_days = 3;\n\n<\/pre>\n<p>then add this to \/etc\/my.cnf<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\n&#x5B;mysqld]\nexpire-logs-days=3\n\n<\/pre>\n<p>and mysqld will delete them logs for you<br \/>\nSHOW SLAVE STATUS\\G<\/p>\n<p>This is critical. When you run SHOW SLAVE STATUS\\G, you will see two binary logs from the Master:<\/p>\n<p>    Master_Log_File<br \/>\n    Relay_Master_Log_File<\/p>\n<p>When replication has little or no lag these are usually the same value. When there is a lot of replication lag, these values are different. Just to make it simple, choose whatever Relay_Master_Log_File is, and go back to the Master and run<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nPURGE BINARY LOGS TO &#039;Whatever Relay_Master_Log_File Is&#039;;\n\n<\/pre>\n<p>To clean up Binary Log on Master Server<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nshell&gt; mysql -u username -p\nmysql&gt; RESET MASTER;\n\n<\/pre>\n<p>To clean up Binary Log on Slave Server<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nmysql -u username -p\nmysql&gt; RESET SLAVE;\n\n<\/pre>\n<p>Remove MySQL Binary Log with PURGE BINARY LOGS Statement<br \/>\nPURGE BINARY LOGS statement can remove Binary Log based on date or up to a Binary Log sequence number<br \/>\nBase on the binary logs example shown above, I would like to remove binary up to mysql-bin.000015<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nshell&gt; mysql -u username -p\nmysql&gt;PURGE BINARY LOGS TO &#039;mysql-bin.000015&#039;;\n\n<\/pre>\n<p>Alternatively, you can remove the binary older than a specific date.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nshell&gt; mysql -u username -p\nmysql&gt; PURGE BINARY LOGS BEFORE &#039;2009-11-01 00:00:00&#039;;\n\n<\/pre>\n<p>Remove MySQL Binary Log with mysqladmin flush-logs Command<br \/>\nAnother method is running mysqladmin flush-logs command, it will remove binary logs more than 3 days old.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nshell&gt; mysqladmin -u username -p flush-logs\n\n<\/pre>\n<p>Keep MySQL Binary Log for X Days<\/p>\n<p>All of the methods above required monitoring on disk usage, to \u201crotate\u201d and keep the binary logs for x number of day. The option below can be configured on MySQL\u2019s config file, my.cnf<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nexpire_logs_days = 7\n\n<\/pre>\n<p>Consider turning off MySQL Binary Log if MySQL Replication is not deploy on the database server and recovery is not the main concern.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nnano \/etc\/my.cnf\n# binary logging - not required for slaves, but recommended\n#log-bin=mysql-bin\n\n\n<\/pre>\n<p>If you drive is 100% full <\/p>\n<p>Mysql error log shows &#8211; <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nERROR 3 (HY000): Error writing file &#039;.\/mysql-bin.~rec~&#039; (Errcode: 28)\n\n<\/pre>\n<p>and the commands do not work, try this:<\/p>\n<p>Stop mysql or kill the process<br \/>\nZero out the first log file<br \/>\nremove the line for the first binary file in mysql-bin.index<br \/>\nrestart mysql<br \/>\nThen run the commands to purge<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The file mysql-bin.index keeps a list of all binary logs mysqld has generated and auto-rotated. The mechanisms for cleaning out the binlogs in conjunction with mysql-bin.index are: PURGE BINARY LOGS TO &#8216;binlogname&#8217;; PURGE BINARY LOGS BEFORE &#8216;datetimestamp&#8217;; These will clear all binary logs before the binlog or timestamp you just specified. For example, if you &#8230; <a title=\"Delete mysql-bin files safely\" class=\"read-more\" href=\"https:\/\/www.qbytes.cloud\/index.php\/2014\/12\/05\/delete-mysql-bin-files-safely\/\" aria-label=\"Read more about Delete mysql-bin files safely\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-1388","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts\/1388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/comments?post=1388"}],"version-history":[{"count":0,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/posts\/1388\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/media?parent=1388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/categories?post=1388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qbytes.cloud\/index.php\/wp-json\/wp\/v2\/tags?post=1388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}