Home Database Mysql Tutorial MySQLinnodb_monitor及死锁检测

MySQLinnodb_monitor及死锁检测

Jun 07, 2016 pm 05:41 PM
Deadlock detection

showinnodbstatus只是其一种模式的直接展现,并且只能交互式开启,无法自动循环捕获信息innodb状态信息输出到err日志在如何库下都可以,推荐在test数据库下创建

show innodb status只是其一种模式的直接展现,并且只能交互式开启,无法自动循环捕获信息

 

innodb状态信息输出到 err 日志   在如何 库 下都可以,推荐在 test  数据库下创建如下表

mysql> create table innodb_monitor(a int) engine=innodb;
Query OK, 0 rows affected (0.09 sec)

mysql> create table innodb_tablespace_monitor(a int) engine=innodb;
Query OK, 0 rows affected (0.12 sec)

mysql> create table innodb_lock_monitor(a int) engine=innodb;
Query OK, 0 rows affected (0.13 sec)

mysql> create table innodb_table_monitor(a int) engine=innodb;
Query OK, 0 rows affected (0.09 sec)

创建表后innodb会每过15秒输出一次innodb状态信息到error log,通过删除表停止该monitor功能。


 

  • innodb_monitor:实际上就是show engine  innodb status\G
  • innodb_lock_monitor:和innodb_monitor差不多,香港服务器,但还会有更多关于锁的信息
  • innodb_table_monitor:系统中所有innodb的表的一些结构和内部信息输出
  • innodb_tablespace_monitor:输出的是tablespace的信息,注意该monitor输出的只是共享表空间的信息
  • 不包含独立表空间信息。

  • 对于死锁,香港服务器,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参数。

    本文出自 “My DBA life” 博客,请务必保留此出处

    Statement of this Website
    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

    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

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1671
    14
    PHP Tutorial
    1276
    29
    C# Tutorial
    1256
    24
    MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

    The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

    Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

    InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

    MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

    Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

    How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

    MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

    MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

    The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

    MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

    MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

    Explain the InnoDB Buffer Pool and its importance for performance. Explain the InnoDB Buffer Pool and its importance for performance. Apr 19, 2025 am 12:24 AM

    InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

    MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

    MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

    See all articles