mysql-bin日志自动清理及手动删除

mysql

在MySQL主从复制的时候会开启bin日志:
#/etc/my.conf
log-bin=mysql-bin

当该功能打开时会产生大量如mysql-bin.00000*的log文件,这会消耗大量的硬盘空间。在保持MySQL主从复制的功能下有两种解决方法:1. 设置日志expire_logs_days 2. 手动清楚bin日志文件。

实现

设置日志expire_logs_days

修改MySQL配置文件,设置expire_logs_days,重启MySQL。
# vim /etc/my.cnf
expire_logs_days = x //日志自动删除的天数。一般讲x设置的短点,如10

直接在MySQL里设置expire_logs_days,无需重启MySQL。

# mysql -u root -p
> show binary logs;
> show variables like '%log%';
> set global expire_logs_days = 10;

手动清理bin日志文件

# mysql -u root -p
> purge master logs before date_sub(current_date, interval 10 day);  //删除10天前的mysql-bin日志
> show master logs;

也可以重置master,删除所有bin文件
# mysql -u root -p
> reset master;

延伸

1.expire_logs_days英文说明

Where X is the number of days you’d like to keep them around. I would recommend 10, but this depends on how busy your MySQL server is and how fast these log files grow. Just make sure it is longer than the slowest slave takes to replicate the data from your master.
Just a side note: You know that you should do this anyway, but make sure you back up your mysql database. The binary log can be used to recover the database in certain situations; so having a backup ensures that if your database server does crash, you will be able to recover the data.

2.PURGE MASTER LOGS手动删除用法示例
# mysql -u root -p
> purge master logs to 'mysql-bin.010’; //清除mysql-bin.010日志
> purge master logs before '2016-02-28 13:00:00'; //清除2016-02-28 13:00:00前的日志
> purge master logs before date_sub(now(), interval 3 day); //清除3天前的bin日志

参考链接

  1. My MySQL Binary Log files are taking up all my disk space!

你可能感兴趣的:(mysql-bin日志自动清理及手动删除)