日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql主从日志的定期清理

發(fā)布時間:2025/3/17 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql主从日志的定期清理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?mysql主從的binlog定時刪除是很重要的,一般是通過expire_logs_days = 10來設置binlog保留的天數(shù)(mysql5.0一下版本不支持),但有時這還不夠,假如有幾天的日志量非常大,硬盤可能會滿,所以不但要設置保留的天數(shù),還要監(jiān)控硬盤的空間使用情況。寫了一個腳本,適合各個版本的mysql,保留3天的日志,當存放日志的硬盤使用率超過80%,則保留2天,但至少會保留一天的binlog日志文件。

  • #!/bin/env?python?
  • #?-*-?coding:?utf-8?-*-?
  • ##############################################################?
  • #查看存在binlog的目錄位置并找出3天前的最后一個bin-log文件名字?
  • #刪除3天以前的binlog文件,刪除之后data1目錄掛載的硬盤使用率超?
  • #過的80%的話,繼續(xù)刪除2天前的日志文件,至少保留一天的日志。?
  • ##############################################################?
  • ?
  • 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?=?'127.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.execute(sql)?
  • ????????alldata?=?cursor.fetchall()?
  • ????????cursor.close()?
  • ????????conn.close()?
  • ????????text?=?"Deltet?before?%s?days?binlog,deltet?%s?before?!"?%?(days,lastlog)?
  • ????????log_w(text)?
  • ????except?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()?
  • ?

    總結(jié)

    以上是生活随笔為你收集整理的mysql主从日志的定期清理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。