当前位置:网站首页>Mysql database logs binlog save aging (expire\u logs\u days)

Mysql database logs binlog save aging (expire\u logs\u days)

2022-06-25 12:23:00 lihongbao80

One 、 Set Syntax

1、 Method 1 :

edit /etc/my.cnf file , stay [mysqld] Add the following two lines to the node

max_binlog_size = 500M
expire_logs_days = 15

max_binlog_size:bin log Every time the log reaches the set size , Will use the new bin log journal . Such as mysql-bin.000002 achieve 500M after , Create and use mysql-bin.000003 File as logging .

expire_logs_days: Keep... Within the specified date range bin log Historical Journal , The 15 Days. .

2、 Method 2 :

-- mysql8.0 The following versions view the current database logs binlog Time limit of preservation   In days , Default 0  Never expire , You can only set at most 99 God 
show variables like 'expire_logs_days';
set global expire_logs_days=60;

-- mysql8.0 The above versions set global parameters binlog_expire_logs_seconds modify binlog Storage time   In seconds ; Default 2592000 30 God  14400 4 Hours ;86400 1 God ;259200 3 God 
show variables like '%binlog_expire_logs_seconds%';

set global binlog_expire_logs_seconds=259200;

Two 、 Expiration deletion policy

summary :
Through the following experiments , about mysql binlog Expiration deletion policy , We can speculate as follows .
1. mysql It's based on binlog The last time the operating system of the file was modified , To judge binlog Is it overdue ( Not based on binlog The time of occurrence in the log ).
2. It's triggering mysql On expiration deletion ,mysql First check *bin.index file , Find the oldest binlog, Then check the system time of the file , There are two situations :
1) If you find that the file is not expired , They think that there is no binlog Log expiration , Do not delete , Even if there are other binlog Has expired .
2) If the file is found to be out of date , Will find the next binlog, Judge whether it is overdue , There are also two cases ( Expired or not expired ), So back and forth , Until you find the first expired binlog That is, stop searching , And delete the binlog And all that binlog All previous logs

1.  view the database binlog list 
1)
mysql> show binary logs;
+-----------------+------------+
| Log_name        | File_size  |
+-----------------+------------+
| 3306-bin.000006 | 1074742033 |
| 3306-bin.000007 | 1074580678 |
| 3306-bin.000008 | 1074739627 |
| 3306-bin.000009 |  250635228 |
| 3306-bin.000010 |        120 |
+-----------------+------------+
5 rows in set (0.00 sec)
 
2) 
ls -lrt
total 7402312
-rw-r--r-- 1 root  root        1747 Mar 25 10:06 test
-rw-rw---- 1 mysql mysql 1074742032 May  4 16:48 3306-bin.000006
-rw-rw---- 1 mysql mysql 1074580678 May  4 16:54 3306-bin.000007
-rw-rw---- 1 mysql mysql 1074739627 May  4 17:00 3306-bin.000008
-rw-rw---- 1 mysql mysql        168 May  4 17:00 3306-bin.index
-rw-rw---- 1 mysql mysql  250635182 May  4 17:02 3306-bin.000009
-rw-rw---- 1 mysql mysql   10448198 May 27 05:04 slow3306.log
-rw-r----- 1 mysql root     1096759 May 27 05:06 error3306.log
-rw-rw---- 1 mysql mysql 4093675902 May 27 05:06 general3306.log
## We see mysql For the time being 4 individual binlog journal , The serial number from 6 To 9, The serial number currently in use is 9 Log 
 
2.  Set up binlog Expiration time 
set global expire_logs_days=7;
## In the 1 In this step we can see 3306-bin.000006,3306-bin.000007,3306-bin.000008  All three logs are expired ( because 3306-bin.000009 Is the log currently in use , So it is not an expired log )
 
3.  modify 3306-bin.000006 File system time 
 Use  vi  see 3306-bin.000006 file , Save and exit without any changes .
 
4.  Check after modification binlog File time 
ls -lrt
total 7402312
-rw-r--r-- 1 root  root        1747 Mar 25 10:06 test
-rw-rw---- 1 mysql mysql 1074580678 May  4 16:54 3306-bin.000007
-rw-rw---- 1 mysql mysql 1074739627 May  4 17:00 3306-bin.000008
-rw-rw---- 1 mysql mysql        168 May  4 17:00 3306-bin.index
-rw-rw---- 1 mysql mysql  250635182 May  4 17:02 3306-bin.000009
-rw-rw---- 1 mysql mysql   10448198 May 27 05:04 slow3306.log
-rw-r----- 1 mysql root     1096759 May 27 05:06 error3306.log
-rw-rw---- 1 mysql mysql 4093675902 May 27 05:06 general3306.log
-rw-rw---- 1 mysql mysql 1074742033 May 27 10:13 3306-bin.000006
##3306-bin.000006  The log file has been changed. The system log has become the time of the day 
 
5. flush logs
## We know flush logs; Will trigger the expired log cleanup operation 
 
6.  View existing binlog
1)
mysql> show binary logs;
+-----------------+------------+
| Log_name        | File_size  |
+-----------------+------------+
| 3306-bin.000006 | 1074742033 |
| 3306-bin.000007 | 1074580678 |
| 3306-bin.000008 | 1074739627 |
| 3306-bin.000009 |  250635228 |
| 3306-bin.000010 |        120 |
+-----------------+------------+
5 rows in set (0.00 sec)
 
2)
ls -lrt
total 7402316
-rw-r--r-- 1 root  root        1747 Mar 25 10:06 test
-rw-rw---- 1 mysql mysql 1074580678 May  4 16:54 3306-bin.000007
-rw-rw---- 1 mysql mysql 1074739627 May  4 17:00 3306-bin.000008
-rw-r----- 1 mysql root     1096759 May 27 05:06 error3306.log
-rw-rw---- 1 mysql mysql 1074742033 May 27 10:13 3306-bin.000006
-rw-rw---- 1 mysql mysql   10448370 May 27 10:14 slow3306.log
-rw-rw---- 1 mysql mysql 4093676334 May 27 10:14 general3306.log
-rw-rw---- 1 mysql mysql  250635228 May 27 10:14 3306-bin.000009
-rw-rw---- 1 mysql mysql        120 May 27 10:14 3306-bin.000010
-rw-rw---- 1 mysql mysql        210 May 27 10:14 3306-bin.index
## We found that 3306-bin.000006,3306-bin.000007,3306-bin.000008 Three logs have not been deleted ( here 3306-bin.000006 The file system time is the same day ,3306-bin.000007 and 3306-bin.000008 The file time is 4 Number )
 
7. purge  One of the earliest 3306-bin.000006 journal 
1)
mysql> purge master logs to '3306-bin.000007';
Query OK, 0 rows affected (0.31 sec)
 
2)
mysql> show binary logs;
+-----------------+------------+
| Log_name        | File_size  |
+-----------------+------------+
| 3306-bin.000007 | 1074580678 |
| 3306-bin.000008 | 1074739627 |
| 3306-bin.000009 |  250635228 |
| 3306-bin.000010 |        120 |
+-----------------+------------+
4 rows in set (0.01 sec)
 
3)
ls -lrt
total 6352756
-rw-r--r-- 1 root  root        1747 Mar 25 10:06 test
-rw-rw---- 1 mysql mysql 1074580678 May  4 16:54 3306-bin.000007
-rw-rw---- 1 mysql mysql 1074739627 May  4 17:00 3306-bin.000008
-rw-r----- 1 mysql root     1096759 May 27 05:06 error3306.log
-rw-rw---- 1 mysql mysql   10448370 May 27 10:14 slow3306.log
-rw-rw---- 1 mysql mysql  250635228 May 27 10:14 3306-bin.000009
-rw-rw---- 1 mysql mysql        120 May 27 10:14 3306-bin.000010
-rw-rw---- 1 mysql mysql        168 May 27 10:17 3306-bin.index
-rw-rw---- 1 mysql mysql 4093676603 May 27 10:17 general3306.log
 
##3306-bin.000006 The log has been deleted 
 
8.  Again flush logs;
1) flush logs;
mysql> flush logs;
Query OK, 0 rows affected (0.30 sec)
 
2) see  binlog
mysql> show binary logs;
+-----------------+-----------+
| Log_name        | File_size |
+-----------------+-----------+
| 3306-bin.000009 | 250635228 |
| 3306-bin.000010 |       166 |
| 3306-bin.000011 |       120 |
+-----------------+-----------+
3 rows in set (0.00 sec)
 
ls -lrt
total 4253804
-rw-r--r-- 1 root  root        1747 Mar 25 10:06 test
-rw-r----- 1 mysql root     1096759 May 27 05:06 error3306.log
-rw-rw---- 1 mysql mysql  250635228 May 27 10:14 3306-bin.000009
-rw-rw---- 1 mysql mysql   10448542 May 27 10:17 slow3306.log
-rw-rw---- 1 mysql mysql 4093676814 May 27 10:17 general3306.log
-rw-rw---- 1 mysql mysql        166 May 27 10:17 3306-bin.000010
-rw-rw---- 1 mysql mysql        120 May 27 10:17 3306-bin.000011
-rw-rw---- 1 mysql mysql        126 May 27 10:17 3306-bin.index
 
## At this time, we found that 3306-bin.000007,3306-bin.000008 The log has been automatically deleted 
原网站

版权声明
本文为[lihongbao80]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200534500579.html

随机推荐