mysql的binlog太大太多占用大量磁盘

  • A+
所属分类:MySQL
高性能企业级服务器首台5折

现象:网站访问越来越慢,最后无法访问了,经过检查发现磁盘满了。仔细查询下来确认是由于mysql的binlog太多太大占用了空间

分析过程及解决方案:通常出现这种问题都应该登录服务器检查磁盘、内存和进程使用的情况,通过top、df –h和free –m来检查,发现磁盘空间满了。再进一步通过du –sh对可以的目录进行检查,发现是mysql的binlog占用空间过大。清理binlog的方法如下:

 

1) 设置日志保留时长expire_logs_days自动删除

查看当前日志保存天数:


1
<span class="hljs-keyword">show</span> <span class="hljs-keyword">variables</span> <span class="hljs-keyword">like</span> <span class="hljs-string">'%expire_logs_days%'</span>;

这个默认是0,也就是logs不过期,可通过设置全局的参数,使他临时生效:


1
<span class="hljs-builtin-name">set</span> global <span class="hljs-attribute">expire_logs_days</span>=7;

设置了只保留7天BINLOG, 下次重启mysql这个参数默认会失败,所以需在my.cnf中设置


1
<span class="hljs-attr">expire_logs_days</span> = <span class="hljs-number">7</span>

 

2) 手动删除BINLOG (purge binary logs)

用于删除列于在指定的日志或日期之前的日志索引中的所有二进制日志。这些日志也会从记录在日志索引文件


1
2
<span class="hljs-keyword">PURGE</span> {<span class="hljs-keyword">MASTER</span> | <span class="hljs-built_in">BINARY</span>} <span class="hljs-keyword">LOGS</span> <span class="hljs-keyword">TO</span> <span class="hljs-string">'log_name'</span>
<span class="hljs-keyword">PURGE</span> {<span class="hljs-keyword">MASTER</span> | <span class="hljs-built_in">BINARY</span>} <span class="hljs-keyword">LOGS</span> <span class="hljs-keyword">BEFORE</span> <span class="hljs-string">'date'</span>

例如:


1
2
3
<span class="hljs-keyword">PURGE</span> <span class="hljs-keyword">MASTER</span> <span class="hljs-keyword">LOGS</span> <span class="hljs-keyword">TO</span> <span class="hljs-string">'mysql-bin.010'</span>;
<span class="hljs-keyword">PURGE</span> <span class="hljs-keyword">MASTER</span> <span class="hljs-keyword">LOGS</span> <span class="hljs-keyword">BEFORE</span> <span class="hljs-string">'2008-06-22 13:00:00'</span>;
<span class="hljs-keyword">PURGE</span> <span class="hljs-keyword">MASTER</span> <span class="hljs-keyword">LOGS</span> <span class="hljs-keyword">BEFORE</span> <span class="hljs-keyword">DATE_SUB</span>( <span class="hljs-keyword">NOW</span>( ), <span class="hljs-built_in">INTERVAL</span> <span class="hljs-number">3</span> <span class="hljs-keyword">DAY</span>);

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: