当开启mysql数据库主从时,会产生大量如mysql-bin.00000* log的文件,这会大量耗费您的硬盘空间。 如:
mysql-bin.000001
mysql-bin.000002
mysql-bin.000003
mysql-bin.000004
mysql-bin.000005
1、 关闭mysql主从,关闭binlog
# vim /etc/my.cnf //注释掉log-bin,binlog_format
# Replication Master Server (default)
# binary logging is required for replication
# log-bin=mysql-bin
# binary logging format - mixed recommended
# binlog_format=mixed
操作完之后记得重启数据库
2、 开启mysql主从,设置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;
3、 登陆mysql,执行以下SQL语句手动清除binlog文件
PURGE MASTER LOGS TO 'MySQL-bin.010'; // 清除MySQL-bin.010日志
PURGE MASTER LOGS BEFORE '2008-06-22 13:00:00'; // 清除2008-06-22 13:00:00前binlog日志
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY); //清除3天前binlog日志BEFORE,变量的date自变量可以为’YYYY-MM-DD hh:mm:ss’格式。
也可以重置master,删除所有binlog文件: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、 清除binlog时,对从mysql的影响 如果您有一个活性的从属服务器,该服务器当前正在读取您正在试图删除的日志之一,则本语句不会起作用,而是会失败,并伴随一个错误。不过,如果从属服务器是休止的,并且您碰巧清理了其想要读取的日志之一,则从属服务器启动后不能复制。当从属服务器正在复制时,本语句可以安全运行。您不需要停止它们。