mysql binlog purge_主库binlog被purge的情形

1.演示实验环境

--主库版本:

root@(none)>show variables like 'version';

+---------------+---------------------+

| Variable_name | Value |

+---------------+---------------------+

| version | 5.7.23-23-31.31-log |

+---------------+---------------------+

1 row in set (0.01 sec)

--复制模式

root@(none)>show variables like '%gtid_mode%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| gtid_mode | ON |

+---------------+-------+

1 row in set (0.00 sec)

--复制进程

root@(none)>show processlist;

+----+-----------------+---------------------+-------+------------------+-------+---------------------------------------------------------------+------------------+-----------+---------------+

| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined |

+----+-----------------+---------------------+-------+------------------+-------+---------------------------------------------------------------+------------------+-----------+---------------+

| 1 | event_scheduler | localhost | NULL | Daemon | 14232 | Waiting on empty queue | NULL | 0 | 0 |

| 9 | repl | 192.168.2.101:53422 | NULL | Binlog Dump GTID | 11941 | Master has sent all binlog to slave; waiting for more updates | NULL | 0 | 0 |

| 14 | root | localhost | NULL | Query | 0 | starting | show processlist | 0 | 0 |

+----+-----------------+---------------------+-------+------------------+-------+---------------------------------------------------------------+------------------+-----------+---------------+

8 rows in set (0.00 sec)

root@(none)>

--从库状态

root@(none)>show slave status \G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.2.100

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000014

Read_Master_Log_Pos: 194

Relay_Log_File: relay-bin.000005

Relay_Log_Pos: 407

Relay_Master_Log_File: mysql-bin.000014

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 194

Relay_Log_Space: 695

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

Master_UUID: 015d4d11-0363-11e9-bb6c-0800279a3030

Master_Info_File: /mysql/3306/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set: 015d4d11-0363-11e9-bb6c-0800279a3030:1-4

Executed_Gtid_Set: 015d4d11-0363-11e9-bb6c-0800279a3030:1-4

Auto_Position: 1

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

root@(none)>

2.主库binlog被purge的情形

--停止从库,模拟从库宕机状态

root@pxc01>stop slave;

Query OK, 0 rows affected (0.02 sec)

root@pxc01>show slave status \G;

*************************** 1. row ***************************

Slave_IO_State:

Master_Host: 192.168.2.100

Master_User: repl

Master_Port: 3306

............

Slave_IO_Running: No

Slave_SQL_Running: No

--此时主库上的gtid_purged为空

root@(none)>show variables like '%gtid_purged%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| gtid_purged | |

+---------------+-------+

1 row in set (0.01 sec)

root@(none)>

--查看主库binlog

root@pxc01>show binary logs;

+------------------+-----------+

| Log_name | File_size |

+------------------+-----------+

| mysql-bin.000009 | 154 |

| mysql-bin.000010 | 635 |

| mysql-bin.000011 | 201 |

| mysql-bin.000012 | 177 |

| mysql-bin.000013 | 1087 |

| mysql-bin.000014 | 706 |

+------------------+-----------+

--查看测试数据

root@pxc01>select * from tbx;

+------+

| id |

+------+

| 1 |

| 2 |

| 3 |

+------+

3 rows in set (0.00 sec)

root@pxc01>

--从主库删除记录

root@pxc01>delete from tbx;

Query OK, 3 rows affected (0.09 sec)

root@pxc01>

--切换日志

root@pxc01>flush logs;

Query OK, 0 rows affected (0.08 sec)

root@pxc01>

--新增记录

root@pxc01>insert into tbx values(4);

root@pxc01>insert into tbx values(5);

root@pxc01>insert into tbx values(6);

--查看主库binlog

root@pxc01>show binary logs;

+------------------+-----------+

| Log_name | File_size |

+------------------+-----------+

| mysql-bin.000009 | 154 |

| mysql-bin.000010 | 635 |

| mysql-bin.000011 | 201 |

| mysql-bin.000012 | 177 |

| mysql-bin.000013 | 1087 |

| mysql-bin.000014 | 1019 |

| mysql-bin.000015 | 962 |

+------------------+-----------+

--清理binlog

root@pxc01>purge binary logs to 'mysql-bin.000015';

Query OK, 0 rows affected (0.08 sec)

root@pxc01>show binary logs;

+------------------+-----------+

| Log_name | File_size |

+------------------+-----------+

| mysql-bin.000015 | 962 |

+------------------+-----------+

1 row in set (0.00 sec)

root@pxc01>

--此时可以看到相应的gtid_purged值

root@pxc01>show variables like '%gtid_purged%';

+---------------+------------------------------------------+

| Variable_name | Value |

+---------------+------------------------------------------+

| gtid_purged | 015d4d11-0363-11e9-bb6c-0800279a3030:1-7 |

+---------------+------------------------------------------+

1 row in set (0.01 sec)

root@pxc01>

--下面启动从库

root@(none)>start slave;

Query OK, 0 rows affected (0.06 sec)

root@(none)>show slave status \G;

*************************** 1. row ***************************

Slave_IO_State:

Slave_IO_Running: No

Slave_SQL_Running: Yes

Last_IO_Errno: 1236

Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'

Retrieved_Gtid_Set: 015d4d11-0363-11e9-bb6c-0800279a3030:1-6

Executed_Gtid_Set: 015d4d11-0363-11e9-bb6c-0800279a3030:1-6

Auto_Position: 1

-- 从库上gtid_purged参数,此时为空

root@(none)>show variables like '%gtid_purged%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| gtid_purged | |

+---------------+-------+

1 row in set (0.01 sec)

root@(none)>

--停止从库

root@(none)>stop slave;

--下面尝试使用gtid_purged进行跳过事务。

root@(none)>set global gtid_purged = '015d4d11-0363-11e9-bb6c-0800279a3030:1-7';

ERROR 1840 (HY000): @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

root@(none)>

提示:仅仅当GLOBAL.GTID_EXECUTED为空才能被设置

--查看从库当前的gtid_executed。gtid_executed肯定是不为空,且这些gtid记录在从库的binary log中

root@(none)>show global variables like '%gtid_executed%'\G

*************************** 1. row ***************************

Variable_name: gtid_executed

Value: 015d4d11-0363-11e9-bb6c-0800279a3030:1-6

*************************** 2. row ***************************

Variable_name: gtid_executed_compression_period

Value: 1000

2 rows in set (0.01 sec)

root@(none)>

--下面我们在从库上reset master,即清空从库binlog

root@(none)>reset master;

Query OK, 0 rows affected (0.10 sec)

--再次查看gtid_executed已经为空值

root@(none)>show global variables like '%gtid_executed%'\G

*************************** 1. row ***************************

Variable_name: gtid_executed

Value:

*************************** 2. row ***************************

Variable_name: gtid_executed_compression_period

Value: 1000

2 rows in set (0.01 sec)

root@(none)>

--此时再次设置gtid_purged的值

root@(none)>set global gtid_purged = '015d4d11-0363-11e9-bb6c-0800279a3030:1-7';

Query OK, 0 rows affected (0.06 sec)

--启动从库

root@(none)>start slave;

root@(none)>show slave status \G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.2.100

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000015

Read_Master_Log_Pos: 962

Relay_Log_File: relay-bin.000006

Relay_Log_Pos: 1175

Relay_Master_Log_File: mysql-bin.000015

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 962

Relay_Log_Space: 2141

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

Master_UUID: 015d4d11-0363-11e9-bb6c-0800279a3030

Master_Info_File: /mysql/3306/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set: 015d4d11-0363-11e9-bb6c-0800279a3030:1-6:8-10

Executed_Gtid_Set: 015d4d11-0363-11e9-bb6c-0800279a3030:1-10

Auto_Position: 1

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

注当前主从记录是不一致的:前期在主库delete的事务未传到从库,需要手动删除!

主库记录:

root@pxc01>select * from tbx;

+------+

| id |

+------+

| 4 |

| 5 |

| 6 |

+------+

3 rows in set (0.00 sec)

root@pxc01>

从库记录:

root@pxc01>select * from tbx;

+------+

| id |

+------+

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

| 6 |

+------+

6 rows in

根据提供的引用内容,mysql内存参数"binlog_expire_logs_auto_purge"用于控制自动清理(binlog)日志的功能。该参数的作用是设置binlog日志的自动清理功能是否开启。默认情况下,该参数的值为"ON",表示开启自动清理功能。当开启自动清理功能后,系统将会根据"binlog_expire_logs_seconds"参数的设置来自动清理过期的binlog日志。 需要注意的是,在MySQL 8.0以下版本中,binlog日志的保存时效是以天为单位,通过"expire_logs_days"参数来设置,默认为0,表示永不过期。而在MySQL 8.0以上版本中,binlog日志的保存时效是以秒为单位,通过"binlog_expire_logs_seconds"参数来设置,默认为2592000秒(30天)。 要对binlog日志进行手动清理,有几种常用的方法。一种是使用"PURGE BINARY LOGS"语句,可以清理指定的binlog文件之前的所有binlog日志。例如: PURGE BINARY LOGS TO 'mysql-bin.000035'; 另一种是使用"PURGE MASTER LOGS"语句,可以清理指定时间点之前的所有binlog日志。例如: PURGE MASTER LOGS BEFORE '2019-09-09 13:00:00'; 通过这些方法,可以手动清理过期的binlog日志,从而释放数据库的存储空间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mysql8.0设置binlog保存时间,并清除过期日志释放空间](https://blog.csdn.net/Qingyunya/article/details/128395035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Mysql数据库清理binlog日志命令详解](https://download.csdn.net/download/weixin_38687277/14907673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值