mysql 清理 binlog 二进制日志文件

 

mysql>   PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 30 DAY);
(清理30天前的二进制日志文件,如果主库在使用30天前的日志文件 会报错的)

修改my.cnf文件
【mysqld】
expire_logs_day=20;
 
重启数据库

 

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.1/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.1/en/purge-binary-logs.html

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

 


1. 查找binlog

mysql> show binary logs;
+---------------+------------+
| Log_name      | File_size  |
+---------------+------------+
| binlog.000031 | 1078645043 |
| binlog.000032 | 1076374846 |
| binlog.000033 | 1079182704 |
| binlog.000034 |  511795531 |
| binlog.000035 |        125 |
| binlog.000036 |   11942121 |
| binlog.000037 |        125 |
| binlog.000038 |        146 |
| binlog.000039 |        106 |
| binlog.000040 |      64616 |
| binlog.000041 |        125 |
| binlog.000042 |        125 |
| binlog.000043 |       3875 |
| binlog.000044 |        386 |
| binlog.000045 |        125 |
| binlog.000046 |        125 |
| binlog.000047 |        125 |
| binlog.000048 |       2875 |
| binlog.000049 |       2081 |
| binlog.000050 |        125 |
| binlog.000051 |        125 |
| binlog.000052 |        125 |
| binlog.000053 |      62110 |
| binlog.000054 |  788661684 |
| binlog.000055 |        146 |
| binlog.000056 |        106 |
+---------------+------------+

 

2. 删除 bin-log

mysql> purge binary logs to 'binlog.000033';
Query OK, 0 rows affected (3.54 sec)

3.  查询结果

mysql> show binlog events\G

 

mysql> show binary logs;

 

1 row in set (0.00 sec)

 

    PURGE {MASTER | BINARY} LOGS TO 'log_name'
   PURGE {MASTER | BINARY} LOGS BEFORE 'date'
  用于删除列于在指定的日志或日期之前的日志索引中的所有二进制日志。这些日志也会从记录在日志索引文件中的清单中被删除,这样被给定的日志成为第一个。
  例如:
   PURGE MASTER LOGS TO 'mysql-bin.010';
   PURGE MASTER LOGS BEFORE '2008-06-22 13:00:00';
    清除 3 天前的 binlog
    PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);
   BEFORE 变量的 date 自变量可以为 'YYYY-MM-DD hh:mm:ss' 格式。 MASTERBINARY 是同义词。
  如果您有一个活性的从属服务器,该服务器当前正在读取您正在试图删除的日志之一,则本语句不会起作用,而是会失败,并伴随一个错误。不过,如果从属服务器是休止的,并且您碰巧清理了其想要读取的日志之一,则从属服务器启动后不能复制。当从属服务器正在复制时,本语句可以安全运行。您不需要停止它们。
  要清理日志,需按照以下步骤:
   1. 在每个从属服务器上,使用 SHOW SLAVE STATUS 来检查它正在读取哪个日志。
   2. 使用 SHOW MASTER LOGS 获得主服务器上的一系列日志。
   3. 在所有的从属服务器中判定最早的日志。这个是目标日志。如果所有的从属服务器是更新的,这是清单上的最后一个日志。

   4. 制作您将要删除的所有日志的备份。(这个步骤是自选的,但是建议采用。)

   5. 清理所有的日志,但是不包括目标日志

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值