search
HomeDatabaseMysql TutorialMySQL 死锁检测

MySQL 死锁检测

Jun 07, 2016 pm 05:28 PM
MySQL死锁ordatabaseDeadlock detection

对于死锁,MySQL并没有提供提供直接的变量来表示。对于5.5版本之后的performance_shcema可以提供锁的详细信息(但我们还是5.0呢)

对于死锁,MySQL并没有提供提供直接的变量来表示。对于5.5版本之后的performance_shcema可以提供锁的详细信息(但我们还是5.0呢),,对于InnoDB自带的监控器 Innodb_lock_monitor 其输出总是输出到错误日志中,不方便进行对比。

我监控采用的是zabbix,采用agent 被动方式向zabbix server传送数据。脚本为shell,采用show innodb status 重定向

核心代码:

检测是否为新的死锁信息:

  New_deadlock() {
    new_line_tile=$(grep -n "LATEST DETECTED DEADLOCK" $1 | cut -d ':' -f 1)
    new_line_time=$(echo "$new_line_tile + 2" | bc)
    last_dect_time="$(head -n $new_line_time $1 | tail -n 1) ###截取死锁发生的时间戳
   
    [ -e $2 ] || cp $1 $2 #拿这次输出信息,和上次输出信息对比;如果是第一次检测,将这次输出信息cp作为上次输出信息
 
    old_line_tile=$(grep -n "LATEST DETECTED DEADLOCK" $2 | cut -d ':' -f 1)
 
    if [ -z $old_line_tile ];then
      echo 1
      mv $1  $2  ##判断上次输出是否为死锁,不是的话,直接返回1 表明最近一次是新的死锁。并将 此次输出信息重命名
      exit 1
  else      ##否则对比两次的时间戳
      old_line_time=$(echo "$old_line_tile + 2" | bc) 
      old_last_dect_time="$(head -n $old_line_time  $2 | tail -n 1)"
      mv $1  $2 #输出信息为下一次检测做准备
 
      if [ "$last_dect_time" = "$old_last_dect_time" ];then
        echo 0
      else
        cp $1 /tmp/$1_detail #已判定为死锁,需要保留作案信息
        echo 1
      fi
  fi
}

deadlock_check() {
  case $1 in
  1)
    $MYSQL_BIN -u $user -p$password  -S $SOC1 -e "show engine innodb status\G" > /tmp/innodb_status_1_$dthm
    have_dead_lock=$(grep -c  "LATEST DETECTED DEADLOCK" /tmp/innodb_status_1_$dthm)
    #判断这次检测是否包含死锁信息,包含的话 让 New_dead_lock 做死锁对比否则 返回0
    if [ $have_dead_lock -gt 0 ];then
      New_deadlock /tmp/innodb_status_1_$dthm /tmp/innodb_status_1_$dt
    else
      echo 0
    fi;;
  2)
    $MYSQL_BIN -u $user -p$password  -S $SOC2 -e "show engine innodb status\G" > /tmp/innodb_status_2_$dthm
    have_dead_lock=$(grep -c  "LATEST DETECTED DEADLOCK" /tmp/innodb_status_2_$dthm)
    if [ $have_dead_lock -gt 0 ];then
      New_deadlock /tmp/innodb_status_2_$dthm /tmp/innodb_status_2_$dt
    else
      echo 0
    fi;;
  3)
    $MYSQL_BIN -u $user -p$password  -S $SOC3 -e "show engine innodb status\G" > /tmp/innodb_status_3_$dthm
    have_dead_lock=$(grep -c  "LATEST DETECTED DEADLOCK" /tmp/innodb_status_3_$dthm)
    if [ $have_dead_lock -gt 0 ];then
      New_deadlock  /tmp/innodb_status_3_$dthm /tmp/innodb_status_3_$dt
    else
      echo 0
    fi;;
  4)
    $MYSQL_BIN -u $user -p$password  -S $SOC4 -e "show engine innodb status\G" > /tmp/innodb_status_4_$dthm
    have_dead_lock=$(grep -c  "LATEST DETECTED DEADLOCK" /tmp/innodb_status_4_$dthm)
    if [ $have_dead_lock -gt 0 ];then
      New_deadlock /tmp/innodb_status_4_$dthm /tmp/innodb_status_4_$dt
    else
      echo 0
    fi;;
  *)
    echo $ERROR
    exit 1;;
  esac
}

case  $1中的变量是针对不同机器上多个实例的情况,传送socket参数。

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools