`
cocos
  • 浏览: 393562 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

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' 格式。 MASTER BINARY 是同义词。
  如果您有一个活性的从属服务器,该服务器当前正在读取您正在试图删除的日志之一,则本语句不会起作用,而是会失败,并伴随一个错误。不过,如果从属服务器是休止的,并且您碰巧清理了其想要读取的日志之一,则从属服务器启动后不能复制。当从属服务器正在复制时,本语句可以安全运行。您不需要停止它们。
  要清理日志,需按照以下步骤:
   1. 在每个从属服务器上,使用 SHOW SLAVE STATUS 来检查它正在读取哪个日志。
   2. 使用 SHOW MASTER LOGS 获得主服务器上的一系列日志。
   3. 在所有的从属服务器中判定最早的日志。这个是目标日志。如果所有的从属服务器是更新的,这是清单上的最后一个日志。

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

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics