Detailed explanation of how to use grep to obtain MySQL error log information

Detailed explanation of how to use grep to obtain MySQL error log information

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

Here is all the relevant code:

#!/usr/bin/env python2.7
#-*- encoding: utf-8 -*-
 
"""
This module is used to extract abnormal or error information from the daily MySQL log author: xiaomo
email: [email protected]
"""
 
import os
import sys
import string
from datetime import *
 
# Default character decoder is utf-8
reload(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 the line containing exception or error information in the MySQL error log"""
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 information"""
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 []

Friends who are interested can refer to it for learning. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Analyzing the troublesome Aborted warning in MySQL through case studies
  • How to shrink the log file in MYSQL SERVER
  • Summary of several common logs in MySQL
  • Detailed explanation of MySQL transactions and MySQL logs
  • 3 common errors in reading MySQL Binlog logs
  • How to enable the slow query log function in MySQL
  • How to view mysql binlog (binary log)
  • Basic usage tutorial of MySQL slow query log
  • Analysis of MySQL Aborted connection warning log

<<:  Implementation of Redis one master, two slaves and three sentinels based on Docker

>>:  Vue Beginner's Guide: Creating the First Vue-cli Scaffolding Program

Recommend

Summary of MySql storage engine and index related knowledge

Storage Engine What is a database storage engine?...

How to build php+nginx+swoole+mysql+redis environment with docker

Operating system: Alibaba Cloud ESC instance cent...

Django uses pillow to simply set up verification code function (python)

1. Import the module and define a validation stat...

Steps to install Pyenv under Deepin

Preface In the past, I always switched Python ver...

Installation tutorial of mysql 8.0.11 compressed version under win10

This article shares the installation tutorial of ...

Docker pull image and tag operation pull | tag

I re-read the source code of the Fabric project a...

How to deploy Angular project using Docker

There are two ways to deploy Angular projects wit...

Detailed explanation of node.js installation and HbuilderX configuration

npm installation tutorial: 1. Download the Node.j...

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...