- 浏览: 77629 次
- 性别:
- 来自: 上海
文章分类
最新评论
mysql死锁-查询锁表进程-分析锁表原因
2、查询进程
showprocesslist
查询到相对应的进程===然后killid
补充:
查看正在锁的事务
SELECT*FROMINFORMATION_SCHEMA.INNODB_LOCKS;
查看等待锁的事务
SELECT*FROMINFORMATION_SCHEMA.INNODB_LOCK_WAITS;
参考:http://blog.itpub.net/12679300/viewspace-1420031/
在InnoDB Plugin之前,一般通过show full processlist和show engine innodb status命令查看当前的数据库请求,然后再判断当前事务中锁的情况。随着mysql的发展,已经提供更加便捷的方法来监控数据库中的锁等待现象了。
在information_schema下面有三张表:INNODB_TRX、INNODB_LOCKS、INNODB_LOCK_WAITS,通过这三张表,可以更简单地监控当前的事务并分析可能存在的问题。
- INNODB_TRX表及结构
Column name | Description |
TRX_ID | Unique transaction ID number, internal to InnoDB. (Starting in MySQL 5.6, these IDs are not created for transactions that are read-only and non-locking. See Optimizing InnoDB Read-Only Transactions for details.) |
TRX_WEIGHT | The weight of a transaction, reflecting (but not necessarily the exact count of) the number of rows altered and the number of rows locked by the transaction. To resolve a deadlock, InnoDB selects the transaction with the smallest weight as the “victim” to rollback. Transactions that have changed non-transactional tables are considered heavier than others, regardless of the number of altered and locked rows. |
TRX_STATE | Transaction execution state. One of RUNNING, LOCK WAIT, ROLLING BACK or COMMITTING. |
TRX_STARTED | Transaction start time. |
TRX_REQUESTED_LOCK_ID | ID of the lock the transaction is currently waiting for (if TRX_STATE is LOCK WAIT, otherwise NULL). Details about the lock can be found by joining with INNODB_LOCKS on LOCK_ID. |
TRX_WAIT_STARTED | Time when the transaction started waiting on the lock (if TRX_STATE is LOCK WAIT, otherwise NULL). |
TRX_MYSQL_THREAD_ID | MySQL thread ID. Can be used for joining with PROCESSLIST on ID. See Section 14.17.2.3.1, “Potential Inconsistency with PROCESSLIST Data”. |
TRX_QUERY | The SQL query that is being executed by the transaction. |
TRX_OPERATION_STATE | The transaction's current operation, or NULL. |
TRX_TABLES_IN_USE | The number of InnoDB tables used while processing the current SQL statement of this transaction. |
TRX_TABLES_LOCKED | Number of InnoDB tables that the current SQL statement has row locks on. (Because these are row locks, not table locks, the tables can usually still be read from and written to by multiple transactions, despite some rows being locked.) |
TRX_LOCK_STRUCTS | The number of locks reserved by the transaction. |
TRX_LOCK_MEMORY_BYTES | Total size taken up by the lock structures of this transaction in memory. |
TRX_ROWS_LOCKED | Approximate number or rows locked by this transaction. The value might include delete-marked rows that are physically present but not visible to the transaction. |
TRX_ROWS_MODIFIED | The number of modified and inserted rows in this transaction. |
TRX_CONCURRENCY_TICKETS | A value indicating how much work the current transaction can do before being swapped out, as specified by the innodb_concurrency_tickets option. |
TRX_ISOLATION_LEVEL | The isolation level of the current transaction. |
TRX_UNIQUE_CHECKS | Whether unique checks are turned on or off for the current transaction. (They might be turned off during a bulk data load, for example.) |
TRX_FOREIGN_KEY_CHECKS | Whether foreign key checks are turned on or off for the current transaction. (They might be turned off during a bulk data load, for example.) |
TRX_LAST_FOREIGN_KEY_ERROR | Detailed error message for last FK error, or NULL. |
TRX_ADAPTIVE_HASH_LATCHED | Whether or not the adaptive hash index is locked by the current transaction. (Only a single transaction at a time can modify the adaptive hash index.) |
TRX_ADAPTIVE_HASH_TIMEOUT | Whether to relinquish the search latch immediately for the adaptive hash index, or reserve it across calls from MySQL. When there is no AHI contention, this value remains zero and statements reserve the latch until they finish. During times of contention, it counts down to zero, and statements release the latch immediately after each row lookup. |
TRX_IS_READ_ONLY | A value of 1 indicates the transaction is read-only. (5.6.4 and up.) |
TRX_AUTOCOMMIT_NON_LOCKING | A value of 1 indicates the transaction is a SELECT statement that does not use the FOR UPDATE or LOCK IN SHARED MODE clauses, and is executing with the autocommit setting turned on so that the transaction will only contain this one statement. (5.6.4 and up.) When this column and TRX_IS_READ_ONLY are both 1, InnoDB optimizes the transaction to reduce the overhead associated with transactions that change table data. |
比较常用的列:
trx_id:InnoDB存储引擎内部唯一的事物ID
trx_status:当前事务的状态
trx_status:事务的开始时间
trx_requested_lock_id:等待事务的锁ID
trx_wait_started:事务等待的开始时间
trx_weight:事务的权重,反应一个事务修改和锁定的行数,当发现死锁需要回滚时,权重越小的值被回滚
trx_mysql_thread_id:MySQL中的进程ID,与show processlist中的ID值相对应
trx_query:事务运行的SQL语句
- INNODB_LOCKS
Column name | Description |
LOCK_ID | Unique lock ID number, internal to InnoDB. Treat it as an opaque string. Although LOCK_ID currently contains TRX_ID, the format of the data in LOCK_ID is not guaranteed to remain the same in future releases. Do not write programs that parse the LOCK_ID value. |
LOCK_TRX_ID | ID of the transaction holding this lock. Details about the transaction can be found by joining with INNODB_TRX on TRX_ID. |
LOCK_MODE | Mode of the lock. One of S, X, IS, IX, S_GAP, X_GAP, IS_GAP, IX_GAP, or AUTO_INC for shared, exclusive, intention shared, intention exclusive row locks, shared and exclusive gap locks, intention shared and intention exclusive gap locks, and auto-increment table level lock, respectively. Refer to the sections Section 14.5.3, “InnoDB Lock Modes” and Section 14.5.2, “The InnoDB Transaction Model and Locking” for information on InnoDB locking. |
LOCK_TYPE | Type of the lock. One of RECORD or TABLE for record (row) level or table level locks, respectively. |
LOCK_TABLE | Name of the table that has been locked or contains locked records. |
LOCK_INDEX | Name of the index if LOCK_TYPE='RECORD', otherwise NULL. |
LOCK_SPACE | Tablespace ID of the locked record if LOCK_TYPE='RECORD', otherwise NULL. |
LOCK_PAGE | Page number of the locked record if LOCK_TYPE='RECORD', otherwise NULL. |
LOCK_REC | Heap number of the locked record within the page if LOCK_TYPE='RECORD', otherwise NULL. |
LOCK_DATA | Primary key value(s) of the locked record if LOCK_TYPE='RECORD', otherwise NULL. This column contains the value(s) of the primary key column(s) in the locked row, formatted as a valid SQL string (ready to be copied to SQL commands). If there is no primary key then the InnoDB internal unique row ID number is used. If a gap lock is taken for key values or ranges above the largest value in the index, LOCK_DATA reports “supremum pseudo-record”. When the page containing the locked record is not in the buffer pool (in the case that it was paged out to disk while the lock was held), InnoDB does not fetch the page from disk, to avoid unnecessary disk operations. Instead, LOCK_DATA is set to NULL. |
- INNODB_LOCK_WAITS
Column name | Description |
REQUESTING_TRX_ID | ID of the requesting transaction. |
REQUESTED_LOCK_ID | ID of the lock for which a transaction is waiting. Details about the lock can be found by joining with INNODB_LOCKS on LOCK_ID. |
BLOCKING_TRX_ID | ID of the blocking transaction. |
BLOCKING_LOCK_ID | ID of a lock held by a transaction blocking another transaction from proceeding. Details about the lock can be found by joining with INNODB_LOCKS on LOCK_ID. |
以上这些表,其实只要知道其中比较常用的字段,就差不多能够满足日常的工作需求了,下面通过测试进行演示;
一、准备工作
1、在test下面随便创建一张表john,并取消自动commit操作,脚本如下:
mysql> use information_schema Database changed
mysql> select count(*) from tables;
mysql>create table test.john as select * from tables;
mysql>insert into john select * from john; (经过几次插入后john表的数据671744行)
mysql>set @@autocommit=0; (取消数据库的自动commit) |
二、进行表john加锁操作,脚本如下:
mysql> select count(*) from john for update; |
在另外一个窗口中监控innodb锁的状态;
mysql> SELECT * FROM INNODB_TRX\G; |
trx_id: B14 只是持有锁,但并没有产生锁等待;
三、模拟锁等待
3.1 在另外一个窗口中,执行语句:
mysql> select count(*) from john where table_name='CHARACTER_SETS' for update; |
3.2 查看当前锁等待的情况
INNODB_TRX的锁情况:
mysql> SELECT * FROM INNODB_TRX\G; |
请注意:因为我们只有模拟两个session,所以这边只有两个会话。(因此一个处于锁等待,另外一个必然就是持有锁的对象。实际的生产环境中可能这边会出现很多列,所以需要用下面的语句才能判断:锁等待和持有锁对象的匹配关系)
3.3 锁等待和持有锁的相互关系
mysql> SELECT * FROM INNODB_LOCK_WAITS\G;
ERROR: |
通过视图INNODB_LOCK_WAITS可以清晰的看到B14持有锁,而B15处于锁等待;
3.4 锁等待的原因
mysql> SELECT * FROM INNODB_LOCKS\G; |
可以看到持有锁的模式、对象
3.5 在进程里面查看状态
Id值为8的进程,Info显示为NULL值,可以推断当前的session由于未进行commit导致锁未释放的;
总结:通过以上几个视图,就可以很快速的判断出锁等待的对象及原因了,从这上面也可以看出mysql管理更加便捷和容易了;
innodb_lock_wait_timeout的参数说明
innodb_lock_wait_timeout指的是事务等待获取资源等待的最长时间,超过这个时间还未分配到资源则会返回应用失败;
参数的时间单位是秒,最小可设置为1s(一般不会设置得这么小),最大可设置1073741824秒(34年,一条语句锁等待超过30分钟估计业务该有反馈了)
默认安装时这个值是50s(公司的默认参数设置)
二、参数的修改
参数支持范围为Session和Global,且支持动态修改,所以可以通过两种方法修改;
2.1 通过语句修改
set innodb_lock_wait_timeout=100;
set global innodb_lock_wait_timeout=100;
注意global的修改对当前线程是不生效的,只有建立新的连接才生效
2.2 修改参数文件/etc/my.cnf
innodb_lock_wait_timeout = 50
三、当等待超过阀值时的情况
当锁等待超过设置时间的时候,就会报如下的错误;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
总结:本文档只是对MySQL锁等待的相关参数做一个简单的了解,后续会有整理相关锁等待及管理的文档;
相关推荐
如果查询条件未指定主键或者无法唯一确定一行,如使用`<>`或`LIKE`操作符,MySQL可能会退化为表级锁,锁定整个表。例如: ```sql SELECT * FROM products WHERE id<>'3' FOR UPDATE; SELECT * FROM products WHERE ...
在 MySQL 中,死锁是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象。死锁的产生原因是由于两个或两个以上的 Session 加锁的顺序不一致。解决死锁问题的关键就是让不同的 Session 加锁...
它允许开发人员将MySQL直接集成到他们的应用程序中,而无需独立的服务器进程。 3. **mysql-community-test-5.7.27-1.el7.x86_64.rpm**:包含了MySQL服务器的测试套件,用于验证MySQL的功能和性能,确保安装正确无误...
### MySQL 死锁产生原因与解决方案详解 #### 一、MySQL锁类型介绍 MySQL支持多种锁机制来确保数据的一致性和事务的隔离性。锁的主要目的是控制并发操作时的资源访问,防止数据不一致的问题。根据锁的作用范围不同...
通过上述分析,我们可以看到MySQL运维中的故障处理涉及多个方面,包括错误日志分析、锁机制理解以及死锁处理等。这些知识点对于确保数据库稳定运行至关重要。希望本手册能为MySQL运维人员提供实用的指导和支持。
此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等的进程称为死锁进程。 死锁的表现 死锁的具体表现有两种: Mysql 增改语句无法正常生效 使用Mysql GUI 工具编辑字段的值时,会出现异常。 如何避免死锁 ...
mysql数据库在查询量大的情况下,有出现死锁状况,有时候死锁导致服务挂了的情况,这脚本就能很好的管理mysql死锁进程。
##### 查询表级锁争用情况 可以通过MySQL的状态变量`table_locks_waited`和`table_locks_immediate`来评估系统上的表锁定竞争情况。如果`table_locks_waited`的值较高,表明存在较严重的表级锁竞争。 ##### MySQL...
通过SHOW STATUS命令可以查询表级锁争用情况,如果Table_locks_waited的值较高,则表明存在较多的锁争用。 MySQL的表级锁分为表共享读锁(TableReadLock)和表独占写锁(TableWriteLock)。其中表共享读锁允许多个...
MySQL的表锁分为表共享读锁(TableReadLock)和表独占写锁(TableWriteLock)。表锁的特点是实现简单,开销小,加锁速度快,但在高并发环境下容易发生锁竞争,导致并发性能下降。MyISAM存储引擎偏向于使用表锁,尤其...
在Oracle数据库管理中,了解如何查询表是否被锁定以及如何执行解锁操作是至关重要的技能,尤其是在处理并发事务、性能调优或解决死锁问题时。本文将深入探讨Oracle中的表锁机制,包括如何识别锁定的表,理解不同类型...
MySQL 锁分析 MySQL 锁机制是 MySQL 数据库中用于管理并发访问的机制, MySQL 锁机制主要有三种类型:表锁、行锁、页锁。每种锁机制都有其特点和应用场景。 表锁 表锁是 MySQL 中最基本的锁机制,表锁分为共享锁...
页级的典型代表引擎为BDB。...2)、进行查询时比页级锁和表级锁需要的I/O要多,所以我们经常把行级锁用在写操作而不是读操作。 3)、容易出现死锁。 对于写锁定如下: 1)、如果表没有加锁,那么对其加写锁定。
MyISAM在执行查询语句(SELECT)前,会自动给涉及的所有表加读锁,在执行更新操作(UPDATE、DELETE、INSERT等)前,会自动给涉及的表加写锁。因此,对MyISAM表进行操作,会有以下情况: * 对MyISAM表的读操作(加读...
在MySQL中,可以通过设置合适的事务隔离级别、优化查询语句(如避免索引使用的循环依赖)和调整应用程序逻辑来预防死锁。例如,确保事务按相同的顺序锁定资源,或者在更新时尽可能直接引用主键,而不是通过非主键...
- 锁机制:行级锁、表级锁、页级锁的理解及使用场景。 - 存储过程和触发器的创建与应用。 2. **数据库设计与优化**: - 正确设计数据库模式,避免数据冗余和异常。 - 数据库范式理论:1NF、2NF、3NF、BCNF等的...
- **分析死锁进程的详细信息**:通过`sys.sysprocesses`表和相关的动态管理视图来获取被阻塞进程的上下文信息,包括执行的SQL语句、数据库名称、主机名等。 综上所述,这些SQL脚本对于诊断和解决MySQL数据库中的...
InnoDB是MySQL中最常用的存储引擎,支持更高级别的锁机制——行级锁,这意味着锁可以作用于单行数据上,而不是整个表。 **InnoDB行级锁的操作方法**: - 在`SELECT`语句中,可以通过`LOCK IN SHARE MODE`为符合条件...