Get the App
SLTechnology News&Howtos  ›  Database  › 

How to get MySQL error log information through grep

Shulou Source: shulou.com Published: 2022-06-01 02:40:27 09月15日 Update

Editor to share with you how to get MySQL error log information through grep, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

To facilitate the maintenance of MySQL, a script is written to provide an interface for collecting error messages. These error messages come from the MySQL error log, and the path to the error-log can be obtained through grep mysql.

#! / usr/bin/env python2.7#-*- encoding: utf-8-*-"" this module is used to extract exception or error messages from daily mysql logs author: xiaomoemail: moxiaomomo@gmail.com "" import osimport sysimport stringfrom datetime import * # default character parser is utf-8reload (sys) sys.setdefaultencoding ('utf-8') COMMON_FLAGS = ["error", "exception", "fail", "crash" "repair"] def _ contain_flag (cur_str): for flag in COMMON_FLAGS: if flag in string.lower (cur_str): return True return False "get the error_log file path of the current mysql instance" def _ get_mysql_error_log_path (): log_path =''grep_infos = os.popen (' ps aux | grep mysql | grep "log-error"') .read () if len (grep_infos) > 1: grep_infos = grep_infos.split ("log-error=") if len (grep_infos) > 1: grep_infos = grep_infos [1] .split ('') if len (grep_infos) > 1: log_path = grep_infos [0] return log_path "" read mysql error log contains exception or error message Line of information "" def _ get_error_info (error_log) Begin_date): error_infos = [] f = open (error_log,'r') lines = f.readlines () for line in lines: data_array = line.split ('') if len (data_array) > 0 and len (data_array [0]) = = 10: dt_strs = data_array [0] .split ('-') cur_date = date (int (dt_strs [0]) Int (dt_strs [1]) Int (dt_ strs [2]) if cur_date > = begin_date and _ contain_flag (line): error_infos.append (line) f.close () return error_infos "" assemble and return mysql error log message "def get_mysql_errors (begin_date=date.today ()-timedelta (1)): try: err_log_path = _ get_mysql_ Error_log_path () if len (err_log_path) > 1: return _ get_error_info (err_log_path Begin_date) except Exception,e: print "[get_mysql_errors]% s"% e return [] above is how to get all the contents of MySQL error log information through grep Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Errors messages logs content articles paths not much most characters instances interfaces files more modules for knowledge scripts industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Huawei MariaDB Shulou Technology