# 16.14 MySQL bin-log日志清理

## 自动清理

### 永久生效

需要重启mysql才能生效

修改`my.cnf`文件

添加下面一行

```
expire_logs_days = 7
```

### 临时生效

进入mysql，执行，下面的语句

```
show variables like '%expire_logs_days%';
set global expire_logs_days = 7;
```

## 手动清理

进入mysql，查看binlog日志

```
show binary logs;
```

**删除某个日志文件之前的所有日志文件**

```
purge binary logs to 'mysql-bin.000035';
```

**清理2019-09-09 13:00:00前binlog日志**

```
PURGE MASTER LOGS BEFORE '2019-09-09 13:00:00';
```

**清除3天前的bin日志**

```
PURGE MASTER LOGS BEFORE DATE_SUB(NOW( ), INTERVAL 3 DAY);
```

> 注意，不要轻易手动去删除binlog，会导致binlog.index和真实存在的binlog不匹配，而导致expire\_logs\_day失效


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.clay-wangzhi.com/16-mysql/16.14-mysql-binlog-ri-zhi-qing-li.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
