MySQLinnodb_monitor及死锁检测
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功能。
不包含独立表空间信息。
我监控采用的是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” 博客,请务必保留此出处

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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.

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.

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 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.

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 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.
