It is very important to regularly Delete the mysql Master/Slave binlog. Generally, you can use expire_logs_days = 10 to set the number of days that binlog is retained. mysql5.0 is not supported in the current version), but sometimes this is not enough, if the log volume in a few days is very large, the hard disk may be full, so you should not only set the retention days, but also monitor the disk space usage. A script is written, which is suitable for mysql of various versions. logs are retained for three days. When the hard disk usage of logs exceeds 80%, logs are retained for two days, but at least one day of binlog log files will be retained.
- #! /Bin/env python
- #-*-Coding: UTF-8 -*-
- ######################################## ######################
- # View the directory location with binlog and find the last bin-log File Name three days ago
- # Delete the binlog file 3 days ago. After deletion, the hard disk usage mounted to the data1 directory exceeds
- # If it exceeds 80%, delete the log file two days ago and keep the log for at least one day.
- ######################################## ######################
-
- Import OS, sys, time, MySQLdb
-
- Def log_w (text ):
- Logfile = "/usr/local/zabbix/bin/delet. log"
- Now = time. strftime ("% Y-% m-% d % H: % M: % S ")
- Tt = now + "\ t" + str (text) + "\ n"
- F = open (logfile, 'a + ')
- F. write (tt)
- F. close ()
-
- Def mysql_conn (port, lastlog, days ):
- Try:
- Center_ip = '2017. 0.0.1'
- Center_user = 'repl _ monitor'
- Center_passwd = 'vqmqlgwtaw3k0uv8'
- SQL = "PURGE MASTER LOGS TO '% s';" % lastlog
- Conn = MySQLdb. connect (host = center_ip, port = int (port), user = center_user, passwd = center_passwd, connect_timeout = 5)
- Cursor = conn. cursor ()
- Cursor.exe cute (SQL)
- Alldata = cursor. fetchall ()
- Cursor. close ()
- Conn. close ()
- Text = "Deltet before % s days binlog, deltet % s before! "% (Days, lastlog)
- Log_w (text)
- Except t Exception, e:
- Log_w (e)
-
- Def find_logdir ():
- Conn = "find/-name binlog | grep-v usr"
- Logdir_list = OS. popen (conn). readlines ()
- If len (logdir_list )! = 0:
- For logdir in logdir_list:
- Datadir = logdir. strip (). split ("/") [1]
- If "mysql_log" in logdir. strip ():
- Port = 3306
- Else:
- Port = logdir. strip (). split ("/") [3]. split ("-") [-1]
- Days = 3
- While 1:
- Conn = "find % s-mtime % s | sort" % (logdir. strip (), days)
- Count = OS. popen (conn). readlines ()
- If len (count )! = 0:
- Lastlog = count [-1]. strip (). split ("/") [-1]
- Mysql_conn (port, lastlog, days)
-
- Df = "df-h | grep-e '% s $' | awk '{print $5}' | awk-F' %'' {print $1 }'" % datadir
- Disk = OS. popen (df). read (). strip ()
- If not disk:
- Break
- Else:
- If int (disk) <80:
- Break
- Else:
- Days = days-1
- If days = 1:
- Break
- Else:
- Sys. exit ()
-
- If _ name _ = "_ main __":
- Find_logdir ()
This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1123088