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. 清理所有的日志,但是不包括目标日志
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com