Cleaning and switching of Mysqlbinlog log

(1) The role of binary log

1. Copy

The MySQL Master binary log is sent to the slave, and the slave redo according to the log to achieve the purpose of master-slave replication.

2. Recovery

Whether you use mysqldump or xtrabackup, you can only restore the database to the moment when there is a backup. If you want to restore the database to any moment, you need to use the binary log


(Two), binlog cleanup

If the amount of data is large, the binlog log grows quickly, and it needs to be cleaned regularly:

Method 1: Use the "reset master" command, this command will delete all binlog logs, the log number starts from "000001"

mysql> reset master;

Query OK, 0 rows affected (0.00 sec)


Method 2: Use "purge master logs to'hostname-bin.******'" command;  this command will delete the logs before ******

[root@k8s-master1 mysql]# ls -lrt

-rw-r----- 1 mysql mysql 168 January 7 16:18 mysql-bin.000001

-rw-r----- 1 mysql mysql 168 January 7 16:19 mysql-bin.000002

-rw-r----- 1 mysql mysql 168 January 7 16:19 mysql-bin.000003

-rw-r----- 1 mysql mysql 168 January 7 16:20 mysql-bin.000004

-rw-r----- 1 mysql mysql 168 January 7 16:21 mysql-bin.000005

-rw-r----- 1 mysql mysql 168 January 7 16:21 mysql-bin.000006

-rw-r----- 1 mysql mysql 168 January 7 16:25 mysql-bin.000007

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000008

[root@k8s-master1 mysql]# 

mysql> purge master logs to 'mysql-bin.000007';

Query OK, 0 rows affected (0.00 sec)

[root@k8s-master1 mysql]# ls -lrt

-rw-r----- 1 mysql mysql 168 January 7 16:25 mysql-bin.000007

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000008


Method 3: Use "purge master logs before'yyyy-mm-dd hh24:mi:ss'" command, this command will delete the logs before the specified log, and  delete the logs before 2020-01-07 00:24:00

[root@k8s-master1 mysql]# ls -lrt /mysql/mysql-bin*

-rw-r----- 1 mysql mysql 168 January 7 16:25 mysql-bin.000007

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000008

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000009

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000010

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000011

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000012

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000013

-rw-r----- 1 mysql mysql 156 January 7 16:37 mysql-bin.000014

mysql> purge master logs before '2020-01-07 00:24:00';

Query OK, 0 rows affected (0.00 sec)


Method 4: Add the "expire_logs_days" parameter to the configuration file, specify the number of days to expire, and the expiration will be deleted automatically.


(Three), binlog switch

Use flush logs or flush binary logs to switch logs

mysql> show master status;

image

1 row in set (0.00 sec)

mysql> flush logs;

Query OK, 0 rows affected (0.00 sec)


mysql> show master status;

image

mysql> flush binary logs;

Query OK, 0 rows affected (0.00 sec)


mysql> show master status;

image


image


Guess you like

Origin blog.51cto.com/15127516/2657669