各位用户为了找寻关于MySQL 查看事务和锁情况的常用语句分享的资料费劲了很多周折。这里教程网为您整理了关于MySQL 查看事务和锁情况的常用语句分享的相关资料,仅供查阅,以下为您介绍关于MySQL 查看事务和锁情况的常用语句分享的详细内容
一些查看数据库中事务和锁情况的常用语句
查看事务等待状况:
? 1 2 3 4 5 6 7 8 9 10 11select
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query
from
information_schema.innodb_lock_waits w
inner
join
information_schema.innodb_trx b
on
b.trx_id = w.blocking_trx_id
inner
join
information_schema.innodb_trx r
on
r.trx_id = w.requesting_trx_id;
查看更具体的事务等待状况:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22select
b.trx_state,
e.state,
e.
time
,
d.state
as
block_state,
d.
time
as
block_time,
a.requesting_trx_id,
a.requested_lock_id,
b.trx_query,
b.trx_mysql_thread_id,
a.blocking_trx_id,
a.blocking_lock_id,
c.trx_query
as
block_trx_query,
c.trx_mysql_thread_id
as
block_trx_mysql_tread_id
from
information_schema.innodb_lock_waits a
left
join
information_schema.innodb_trx b
on
a.requesting_trx_id = b.trx_id
left
join
information_schema.innodb_trx c
on
a.blocking_trx_id = c.trx_id
left
join
information_schema.processlist d
on
c.trx_mysql_thread_id = d.id
left
join
information_schema.processlist e
on
b.trx_mysql_thread_id = e.id
order
by
a.requesting_trx_id;
查看未关闭的事务:
–mysql 5.6
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22select
a.trx_id,
a.trx_state,
a.trx_started,
a.trx_query,
b.id,
b.
user
,
b.db,
b.command,
b.
time
,
b.state,
b.info,
c.processlist_user,
c.processlist_host,
c.processlist_db,
d.sql_text
from
information_schema.innodb_trx a
left
join
information_schema.processlist b
on
a.trx_mysql_thread_id = b.id
and
b.command =
'sleep'
left
join
performance_schema.threads c
on
b.id = c.processlist_id
left
join
performance_schema.events_statements_current d
on
d.thread_id = c.thread_id;
–mysql 5.5
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18select
a.trx_id,
a.trx_state,
a.trx_started,
a.trx_query,
b.id,
b.
user
,
b. host,
b.db,
b.command,
b.
time
,
b.state,
b.info
from
information_schema.innodb_trx a
left
join
information_schema.processlist b
on
a.trx_mysql_thread_id = b.id
where
b.command =
'sleep'
;
查看某段时间以来未关闭事务:
? 1 2 3 4 5 6 7 8 9 10select
trx_id,
trx_started,
trx_mysql_thread_id
from
information_schema.innodb_trx
where
trx_started < date_sub(now(), interval 1
minute
)
and
trx_operation_state
is
null
and
trx_query
is
null
;
补充:mysql锁定状态查看命令
1 show processlist;
show processlist显示哪些线程正在运行。您也可以使用mysqladmin processlist语句得到此信息。如果您有super权限,您可以看到所有线程。否则,您只能看到您自己的线程(也就是,与您正在使用的mysql账户相关的线程)。如果有线程在update或者insert 某个表,此时进程的status为updating 或者 sending data。
如果您得到“too many connections”错误信息,并且想要了解正在发生的情况,本语句是非常有用的。mysql保留一个额外的连接,让拥有super权限的账户使用,以确保管理员能够随时连接和检查系统(假设您没有把此权限给予所有的用户)。
status
含义
checking table
正在检查数据表(这是自动的)。
closing tables
正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。
connect out
复制从服务器正在连接主服务器。
copying to tmp table on disk
由于临时结果集大于tmp_table_size,正在将临时表从内存存储转为磁盘存储以此节省内存。
creating tmp table
正在创建临时表以存放部分查询结果。
deleting from main table
服务器正在执行多表删除中的第一部分,刚删除第一个表。
deleting from reference tables
服务器正在执行多表删除中的第二部分,正在删除其他表的记录。
flushing tables
正在执行flush tables,等待其他线程关闭数据表。
killed
发送了一个kill请求给某线程,那么这个线程将会检查kill标志位,同时会放弃下一个kill请求。mysql会在每次的主循环中检查kill标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么kill请求会在锁释放时马上生效。
locked
被其他查询锁住了。
sending data
正在处理select查询的记录,同时正在把结果发送给客户端。
sorting for group
正在为group by做排序。
sorting for order
正在为order by做排序。
opening tables
这个过程应该会很快,除非受到其他因素的干扰。例如,在执alter table或lock table语句行完以前,数据表无法被其他线程打开。正尝试打开一个表。
removing duplicates
正在执行一个select distinct方式的查询,但是mysql无法在前一个阶段优化掉那些重复的记录。因此,mysql需要再次去掉重复的记录,然后再把结果发送给客户端。
reopen table
获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表。
repair by sorting
修复指令正在排序以创建索引。
repair with keycache
修复指令正在利用索引缓存一个一个地创建新索引。它会比repair by sorting慢些。
searching rows for update
正在讲符合条件的记录找出来以备更新。它必须在update要修改相关的记录之前就完成了。
sleeping
正在等待客户端发送新请求。
system lock
正在等待取得一个外部的系统锁。如果当前没有运行多个mysqld服务器同时请求同一个表,那么可以通过增加--skip-external-locking参数来禁止外部系统锁。
upgrading lock
insert delayed正在尝试取得一个锁表以插入新记录。
updating
正在搜索匹配的记录,并且修改它们。
user lock
正在等待get_lock()。
waiting for tables
该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知:flush tables tbl_name, alter table, rename table, repair table, analyze table,或optimize table。
waiting for handler insert
insert delayed已经处理完了所有待处理的插入操作,正在等待新的请求。
大部分状态对应很快的操作,只要有一个线程保持同一个状态好几秒钟,那么可能是有问题发生了,需要检查一下。还有其他的状态没在上面中列出来,不过它们大部分只是在查看服务器是否有存在错误是才用得着。
2 show full processlist;
show processlist;只列出前100条,如果想全列出请使用show full processlist;
3 show open tables;
这条命令能够查看当前有那些表是打开的。in_use列表示有多少线程正在使用某张表,name_locked表示表名是否被锁,这一般发生在drop或rename命令操作这张表时。所以这条命令不能帮助解答我们常见的问题:当前某张表是否有死锁,谁拥有表上的这个锁等。
? 1show
open
tables
from
database
;
4 show status like ‘%lock%'
查看服务器状态。
5 show engine innodb statusg;
mysql 5.1之前的命令是:show innodbstatusg;,mysql 5.5使用上面命令即可查看innodb引擎的运行时信息。
6 show variables like ‘%timeout%';
查看服务器配置参数。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/zyz511919766/article/details/49335729