Regular cleaning of mysql Master/Slave logs

Source: Internet
Author: User

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.

 
 
  1. #! /Bin/env python
  2. #-*-Coding: UTF-8 -*-
  3. ######################################## ######################
  4. # View the directory location with binlog and find the last bin-log File Name three days ago
  5. # Delete the binlog file 3 days ago. After deletion, the hard disk usage mounted to the data1 directory exceeds
  6. # If it exceeds 80%, delete the log file two days ago and keep the log for at least one day.
  7. ######################################## ######################
  8.  
  9. Import OS, sys, time, MySQLdb
  10.  
  11. Def log_w (text ):
  12. Logfile = "/usr/local/zabbix/bin/delet. log"
  13. Now = time. strftime ("% Y-% m-% d % H: % M: % S ")
  14. Tt = now + "\ t" + str (text) + "\ n"
  15. F = open (logfile, 'a + ')
  16. F. write (tt)
  17. F. close ()
  18.  
  19. Def mysql_conn (port, lastlog, days ):
  20. Try:
  21. Center_ip = '2017. 0.0.1'
  22. Center_user = 'repl _ monitor'
  23. Center_passwd = 'vqmqlgwtaw3k0uv8'
  24. SQL = "PURGE MASTER LOGS TO '% s';" % lastlog
  25. Conn = MySQLdb. connect (host = center_ip, port = int (port), user = center_user, passwd = center_passwd, connect_timeout = 5)
  26. Cursor = conn. cursor ()
  27. Cursor.exe cute (SQL)
  28. Alldata = cursor. fetchall ()
  29. Cursor. close ()
  30. Conn. close ()
  31. Text = "Deltet before % s days binlog, deltet % s before! "% (Days, lastlog)
  32. Log_w (text)
  33. Except t Exception, e:
  34. Log_w (e)
  35.  
  36. Def find_logdir ():
  37. Conn = "find/-name binlog | grep-v usr"
  38. Logdir_list = OS. popen (conn). readlines ()
  39. If len (logdir_list )! = 0:
  40. For logdir in logdir_list:
  41. Datadir = logdir. strip (). split ("/") [1]
  42. If "mysql_log" in logdir. strip ():
  43. Port = 3306
  44. Else:
  45. Port = logdir. strip (). split ("/") [3]. split ("-") [-1]
  46. Days = 3
  47. While 1:
  48. Conn = "find % s-mtime % s | sort" % (logdir. strip (), days)
  49. Count = OS. popen (conn). readlines ()
  50. If len (count )! = 0:
  51. Lastlog = count [-1]. strip (). split ("/") [-1]
  52. Mysql_conn (port, lastlog, days)
  53.  
  54. Df = "df-h | grep-e '% s $' | awk '{print $5}' | awk-F' %'' {print $1 }'" % datadir
  55. Disk = OS. popen (df). read (). strip ()
  56. If not disk:
  57. Break
  58. Else:
  59. If int (disk) <80:
  60. Break
  61. Else:
  62. Days = days-1
  63. If days = 1:
  64. Break
  65. Else:
  66. Sys. exit ()
  67.  
  68. If _ name _ = "_ main __":
  69. Find_logdir ()

 

This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1123088

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.