mysql的binlog安全删除

 1.手动清除binlog文件    

        理论上,应该在配置文件/etc/my.cnf中加上binlog过期时间的配置项,expire_logs_days = 10.但是如果没有加这一项,随着产生越来越多的binlog,磁盘被吃掉了不少。可以直接删除binlog文件,但是可以通过mysql提供的工具来删除更安全。因为purge会更新mysql-bin.index中的条目,而直接删除的话,mysql-bin.index文件不会更新。mysql-bin.index的作用是加快查找binlog文件的速度。

       先help一下吧mysql的binlog安全删除  

 mysql> help purge
 Name: 'PURGE MASTER LOGS'
 Description:
 Syntax:
 PURGE {MASTER | BINARY} LOGS TO 'log_name'
 PURGE {MASTER | BINARY} LOGS BEFORE 'date'

 Deletes all the binary logs listed in the log index prior to the
 specified log or date. The logs also are removed from the list recorded
 in the log index file, so that the given log becomes the first.

 This statement has no effect if the --log-bin option has not been
 enabled.

 URL: http://dev.mysql.com/doc/refman/5.0/en/purge-master-logs.html

 Examples:
 PURGE MASTER LOGS TO 'mysql-bin.010';
 PURGE MASTER LOGS BEFORE '2003-04-02 22:46:26';

 两种方法都可用。第一个是删除至某一个文件为止,第二个是删除到某个日期为止。

 比如我们删除2017-05-31之前的log,可以这样

 mysql>PURGE MASTER LOGS BEFORE '2017-05-31 00:00:00';

2.设置expire_logs_days

# vim /etc/my.cnf  //修改expire_logs_days,x是自动删除的天数,一般将x设置为短点,如10            
 expire_logs_days = x  //二进制日志自动删除的天数。默认值为0,表示“没有自动删除”

      此方法需要重启mysql,附录有关于expire_logs_days的英文说明

      当然也可以不重启mysql,开启mysql主从,直接在mysql里设置expire_logs_days

   > show binary logs;
  > show variables like '%log%';
  > set global expire_logs_days = 10;

需要注意的是:最好到slave上面去看下当前同步到那个binlog文件了,用show slave status查看。否则,master上删多了的话,就造成slave缺失日志文件而导致数据不一致了。