MySQL 鎖表與解鎖步驟

2021-10-02 11:25:30 字數 3055 閱讀 3948

-- 1.使用大於0表

show open tables where `database` = 'test' and in_use > 0 ;

-- 2.request_trx_id請求鎖 與 blocking_trx_id產生鎖原因

select * from information_schema.innodb_lock_waits;

select trx_id,trx_mysql_thread_id from information_schema.innodb_trx ;

-- 3.被鎖語句id

select

now(),

(unix_timestamp(now()) - unix_timestamp(a.trx_started)

) diff_sec,

b.id,-- 被鎖語句id

b.user,

b.host,

b.db,

c.lock_type,

c.lock_table,

c.lock_index

from

information_schema.innodb_trx a

inner join information_schema.processlist b

on a.trx_mysql_thread_id = b.id

inner join information_schema.innodb_locks c

on a.trx_requested_lock_id = c.lock_id ;

-- 4.檢視正在鎖的事務,表名,鎖狀態

select * from information_schema.innodb_locks;

-- 5.檢視id對應的sql

show full processlist;

select b.processlist_id,a.thread_id,a.sql_text from

performance_schema.events_statements_current a, performance_schema.threads b

where a.thread_id=b.thread_id

-- 6.殺掉程序

kill ??

-- 一步到位

select * from sys.`innodb_lock_waits`;

-- 以下語句適用5.6(含有performance_schema.events_statements_current)以上版本

select

b.`trx_mysql_thread_id` 被鎖id,

(select

a.sql_text

from

performance_schema.events_statements_current a,

performance_schema.threads b

where a.thread_id = b.thread_id

and b.processlist_id = b.`trx_mysql_thread_id`) 被鎖sql,

d.`lock_table` 被鎖表,

c.`trx_mysql_thread_id` 鎖表id, -- 殺掉 kill ??

(select

a.sql_text

from

performance_schema.events_statements_current a,

performance_schema.threads b

where a.thread_id = b.thread_id

and b.processlist_id = c.`trx_mysql_thread_id`) 鎖表sql,

e.`lock_table` 鎖表

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

on a.`requesting_trx_id` = d.`lock_trx_id`

left join information_schema.innodb_locks e

on a.`blocking_trx_id` = e.`lock_trx_id` ;

--適用於5.7及以上版本

select t2.processlist_id,from_unixtime(unix_timestamp(now())-t4.time) start_time,t1.*,t3.sql_text from

(select

object_schema,object_name,lock_status,owner_thread_id

from

`performance_schema`.metadata_locks

where

owner_thread_id != sys.ps_thread_id (connection_id())

and object_schema='test'

) t1

left join `performance_schema`.threads t2 on t1.owner_thread_id=t2.thread_id

left join performance_schema.events_statements_current t3 on t1.owner_thread_id=t3.thread_id

left join information_schema.`processlist` t4 on t4.id=t2.processlist_id

order by t1.object_name

Mysql 鎖表與解鎖

使用lock tables後,在鎖定期間需要在其他執行緒使用其他別的未鎖定表,需要慎用鎖定,可能讀寫失敗等奇怪現象 參考 寫鎖定 lock tables lrcolumnsdesc write 寫鎖,鎖定之後,只有當前執行緒只可以對lrcolumnsdesc進行讀操作和寫操作,其他執行緒對對prod...

mysql 鎖表與解鎖

連線mysql 直接執行unlock tables,細節如下 查詢是否鎖表 show open tables 查詢程序 show processlist 查詢到相對應的程序,然後殺死程序 kill id 一般到這一步就解鎖了 檢視正在鎖的事務 select from information sche...

mysql解鎖 mysql鎖表如何解鎖

什麼是mysql鎖表?為了給高併發情況下的mysql進行更好的優化,有必要了解一下mysql查詢更新時的鎖表機制。mysql有三種鎖的級別 頁級 表級 行級。myisam和memory儲存引擎採用的是表級鎖 table level locking bdb儲存引擎採用的是頁面鎖 page level ...