-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_mysql_purge_binlog.py
More file actions
61 lines (53 loc) · 2.06 KB
/
admin_mysql_purge_binlog.py
File metadata and controls
61 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!//bin/env python
#coding:utf-8
import os
import sys
import string
import time
import datetime
import MySQLdb
path='./include'
sys.path.insert(0,path)
import functions as func
from multiprocessing import Process;
def admin_mysql_purge_binlog(host,port,user,passwd,binlog_store_days):
datalist=[]
try:
connect=MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=2,charset='utf8')
cur=connect.cursor()
connect.select_db('information_schema')
master_thread=cur.execute("select * from information_schema.processlist where COMMAND = 'Binlog Dump';")
datalist=[]
if master_thread >= 1:
now=datetime.datetime.now()
delta=datetime.timedelta(days=binlog_store_days)
n_days=now-delta
before_n_days= n_days.strftime('%Y-%m-%d %H:%M:%S')
cur.execute("purge binary logs before '%s'" %(before_n_days));
print ("mysql %s:%s binlog been purge" %(host,port) )
except MySQLdb.Error,e:
pass
print "Mysql Error %d: %s" %(e.args[0],e.args[1])
def main():
user = func.get_config('mysql_db','username')
passwd = func.get_config('mysql_db','password')
servers=func.mysql_query("select host,port,binlog_store_days from db_servers_mysql where is_delete=0 and monitor=1 and binlog_auto_purge=1;")
if servers:
print("%s: admin mysql purge binlog controller started." % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),));
plist = []
for row in servers:
host=row[0]
port=row[1]
binlog_store_days=row[2]
p = Process(target = admin_mysql_purge_binlog, args = (host,port,user,passwd,binlog_store_days))
plist.append(p)
for p in plist:
p.start()
time.sleep(60)
for p in plist:
p.terminate()
for p in plist:
p.join()
print("%s: admin mysql purge binlog controller finished." % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),))
if __name__=='__main__':
main()