mysql5.5 清理日志_设置自动清理MySQL binlog日志

开启MySQL binlog日志的服务器,如果不设置自动清理日志,默认binlog日志一直保留着,时间一长,服务器磁盘空间被binlog日志占满,导致MySQL数据库出错。

172b9a3516e8754f1820cb421596e0ca.png

下面讲解下如何安全清理binlog日志

一、没有主从同步的情况下清理日志

mysql -uroot -ptest123 -e 'PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ),INTERVAL 5 DAY)';

#mysql 定时清理5天前的binlog

mysql -u root -p  #进入mysql 控制台

reset master;  #重置binlog

二、MySQL主从同步下安全清理binlog日志

1、mysql  -u root -p   #进入从服务器mysql控制台

show slave statusG;   #检查从服务器正在读取哪个日志,有多个从服务器,选择时间最早的一个做为目标日志。

2、进入主服务器mysql控制台

show master log;   #获得主服务器上的一系列日志

PURGE MASTER LOGS TO 'binlog.000058';   #删除binlog.000005之前的,不包括binlog.000058

PURGE MASTER LOGS BEFORE '2018-06-22 13:00:00';  #清除2018-06-22 13:00:00前binlog日志

PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 5 DAY);  #清除5天前binlog日志

三、设置自动清理MySQL binlog日志

vi  /etc/my.cnf  #编辑配置

expire_logs_days = 15  #自动删除15天前的日志。默认值为0,表示从不删除。

log-bin=mysql-bin   #注释掉之后,会关闭binlog日志

binlog_format=mixed   #注释掉之后,会关闭binlog日志

:wq!  #保存退出

扩展阅读:

mysql> help purge;

Name: 'PURGE BINARY LOGS'

Description:

Syntax:

PURGE { BINARY | MASTER } LOGS

{ TO 'log_name' | BEFORE datetime_expr }

The binary log is a set of files that contain information about data

modifications made by the MySQL server. The log consists of a set of

binary log files, plus an index file (see

http://dev.mysql.com/doc/refman/5.5/en/binary-log.html).

The PURGE BINARY LOGS statement deletes all the binary log files listed

in the log index file prior to the specified log file name or date.

BINARY and MASTER are synonyms. Deleted log files also are removed from

the list recorded in the index file, so that the given log file becomes

the first in the list.

This statement has no effect if the server was not started with the

--log-bin option to enable binary logging.

URL: http://dev.mysql.com/doc/refman/5.5/en/purge-binary-logs.html

Examples:

PURGE BINARY LOGS TO 'mysql-bin.010';

PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';

温馨提示

感谢您浏览《设置自动清理MySQL binlog日志》

现邀请您关注我们的【5分享】(fivsha)公众号,你将获取更多的IT资源。

如果有疑问也可以在公众号里面回复您遇到的问题或是点击在线帮助联系我们。

d7f1741ee1b9d626d57a052393cf338e.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值