首页 > 代码库 > mysql主从binlog日志自动清理及手动删除
mysql主从binlog日志自动清理及手动删除
问题反馈:
mysql主从,在没有设置自动清理binlog日志的情况下,会产生庞大的同步日志文件,占用磁盘空间。
1 手动清理binlog日志,purge master logs
1.1 删除在“指定日期前”或“指定日志前”的所有二进制binlog日志文件
PURGE {MASTER | BINARY} LOGS TO ‘log_name‘ PURGE {MASTER | BINARY} LOGS BEFORE ‘date‘
实例:
PURGE MASTER LOGS TO ‘MySQL-bin.010‘; PURGE MASTER LOGS BEFORE ‘2003-04-02 22:46:26‘; ##BEFORE变量的date格式为‘YYYY-MM-DD hh:mm:ss‘。MASTER/BINARY是同义词。
1.2 主从master-slave集群环境中,删除binlog日志注意问题
1.2.1 从服务器slave正在读取试图删除的binlog,则purge删除操作失败,并返回错误
1.2.2 从服务器slave stop后,master上purge删除的binlog日志,并未被slave读取,则slave启动后不能复制同步主
1.3 清理binlog日志,正确操作步骤
1.3.1 主服务器master上,show master status;获取主服务器上的binlog日志
1.3.2 所有从服务器slave上,show slave status\G;检查正在读取的binlog日志
1.3.3 判定所有从服务器上已同步完成的,最早的日志文件的位置。purge删除的binlog日志必须早于该日志文件。
1.3.4 将要删除的binlog日志备份(建议步骤)
1.3.5 清理判定之前的日志文件
2 自动定期清理binlog日志,设置expire_logs_days参数
2.1 重启mysql,停机修改my.cnf配置文件
vim /etc/my.cnf ##修改expire_logs_days,x是自动删除的天数,一般将x设置为短点,如10 expire_logs_days = 10 ##二进制日志自动删除的天数。默认值为0,表示“没有自动删除” systemctl restart mysqld
2.2 不重启mysql,线上修改mysql的exipre_logs_days参数
> show binary logs; > show variables like ‘%log%‘; > set global expire_logs_days = 10;
附录:
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.
mysql主从binlog日志自动清理及手动删除