MySQL清理binlog的正确姿势

本位主要讲述如何正确的清理 MySQL的binlog,里面有哪些坑,注意点有什么。
 
一、 为什么要清理binlog 
     如果没有设置MySQL的binlog过期时间或者设置的时间过长, 会导致磁盘容量报警,当磁盘100%时时非常危险,服务会不可用,所以监控一定要做好,
磁盘容量不足时候可以清理之前的binlog来释放磁盘空间。
 
二、 如何清理binlog
     清理binlog有两个维度进行清理, 一个是根据时间的维度,一个是清理某个binlog文件之前的,具体指令如下:
 
   可以通过 help purge 查看此指令的介绍:
 1 mysql> help purge
 2 Name: 'PURGE BINARY LOGS'
 3 Description:
 4 Syntax:
 5 PURGE { BINARY | MASTER } LOGS
 6     { TO 'log_name' | BEFORE datetime_expr }
 7  
 8  
 9 The binary log is a set of files that contain information about data
10 modifications made by the MySQL server. The log consists of a set of
11 binary log files, plus an index file (see
12 http://dev.mysql.com/doc/refman/5.7/en/binary-log.html).
13  
14  
15 The PURGE BINARY LOGS statement deletes all the binary log files listed
16 in the log index file prior to the specified log file name or date.
17 BINARY and MASTER are synonyms. Deleted log files also are removed from
18 the list recorded in the index file, so that the given log file becomes
19 the first in the list.
20  
21  
22 This statement has no effect if the server was not started with the
23 --log-bin option to enable binary logging.
24  
25  
26 URL: http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html
27  
28  
29 Examples:
30 PURGE BINARY LOGS TO 'mysql-bin.010';
31 PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';

 

 
三、如何查看binlog
 
       查看binlog可以查看日志目录里面, 也可以通过mysql 指令来查看:
SHOW BINARY LOGS;
 
 
四、如何才算正确的清理(⚠️避坑)
  • 首先这个节点最好不是主从结构中的主库角色,如果是主库角色,则建议一定要保证清理的是从库已经读取完毕的binlog文件,否则从库异常。
  • 错误的清理binlog方式: reset master;  危险,绝对不要在生产环境里面使用,特别是主库,否则所有从库全部废掉
  • 建议搭建单独的binlog server 来存储binlog
  • 设置参数让mysql 自动的清理过期的binlog,一般binlog 保存七天时间,可以根据自己的环境要求自己定义:
posted @ 2020-03-20 13:25  Topic  阅读(2571)  评论(0编辑  收藏  举报
转载请注明出处. 作者: 运维的自我修养 地址: http://www.cnblogs.com/topicjie/