MYSQL correct way to delete binlog

1. Find:

?
1
2
3
4
5
6
7
8
MySQL> show binary logs;
+—————-+———–+
| Log_name | File_size |
+—————-+———–+
| mysql-bin.000001 | 150462942 |
| mysql-bin.000002 | 125 |
| mysql-bin.000003 | 106 |
+—————-+———–+

2. Delete the bin-log (before deleting mysql-bin.000003 and does not contain mysql-bin.000003)

?
1
2
mysql> purge binary logs to 'mysql-bin.000003' ;
Query OK, 0 rows affected (0.16 sec)

3. Query results (now only one record.)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mysql> show binlog events/G
*************************** 1. row ***************************
Log_name: mysql-bin.000003
Pos: 4
Event_type: Format_desc
Server_id: 1
End_log_pos: 106
Info: Server ver: 5.1.26-rc-log, Binlog ver: 4
1 row in set (0.01 sec)
(mysql-bin.000001和mysql-bin.000002已被删除)
mysql> show binary logs;
+—————-+———–+
| Log_name | File_size |
+—————-+———–+
| mysql-bin.000003 | 106 |
+—————-+———–+
1 row in set (0.00 sec)

(Other formats using deleted!)

?
1
2
PURGE {MASTER | BINARY } LOGS TO 'log_name'
PURGE {MASTER | BINARY } LOGS BEFORE 'date'

It is used to delete all binary logs listed in the log index prior to the specified date or log in. These logs will be deleted from the list recorded in the log index file, so that is a given log becomes the first.

E.g:

?
1
2
PURGE MASTER LOGS TO 'mysql-bin.010′;
PURGE MASTER LOGS BEFORE ' 2008-06-22 13:00:00′;

Clear binlog 3 days ago

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

BEFORE date variable argument may be 'YYYY-MM-DD hh: mm: ss' format. MASTER and BINARY are synonyms.

If you have an active slave that currently is reading one you're trying to delete the log, this statement does not work, but will fail, and accompanied by an error. However, if a slave is dormant and you happen to purge one of the logs it wants to read, you can not copy the slave server starts. When the slave server is replicated, this statement is safe to run. You do not need to stop them.

To clean up the log, the following steps are needed:

1. On each slave server, use SHOW SLAVE STATUS to check which log it is reading.

2. Use the SHOW MASTER LOGS obtain a series of logs on the primary server.

3. Determine the earliest log all the slaves. This is the target log. If all the slaves are updated, this is the last log on the list.

4. Make a backup of all the logs you want to delete. (This step is optional, but recommended.)

5. Clean all the logs, but not including the target log.

In contab settings:

Copy the code code is as follows:
0 1 * * *  `mysql -uroot -e 'PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 3 DAY);'`

Guess you like

Origin www.cnblogs.com/xzlive/p/12156250.html