--锁
-- 查看正在锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
-- 查看等待锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
-- 查询锁
SELECT *
FROM information_schema.INNODB_LOCKS
WHERE LOCK_TRX_ID IN
(SELECT BLOCKING_TRX_ID FROM information_schema.INNODB_LOCK_WAITS);
-- 确认锁源头
show processlist;
mysql - uroot - ppassword - e 'select id from information_schema.processlist where db="xhbx" order by time';
-- 查询锁的具体信息
SELECT distinct b.trx_id blocking_trx_id,
b.trx_mysql_thread_id thread_id,
SUBSTRING(p. HOST, 1, INSTR(p. HOST, ':') - 1) blocking_host,
SUBSTRING(p. HOST, INSTR(p. HOST, ':') + 1) blocking_port,
IF(p.COMMAND = 'Sleep', p.TIME, 0) idel_in_trx,
b.trx_query blocking_query,
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
TIMESTAMPDIFF(SECOND, r.trx_wait_started, CURRENT_TIMESTAMP) wait_time,
r.trx_query waiting_query,
l.lock_table waiting_table_lock
FROM information_schema.INNODB_LOCKS l
LEFT JOIN information_schema.INNODB_LOCK_WAITS w
ON w.requested_lock_id = l.lock_id
LEFT JOIN information_schema.INNODB_TRX b
ON b.trx_id = w.blocking_trx_id
LEFT JOIN information_schema.INNODB_TRX r
ON r.trx_id = w.requesting_trx_id
LEFT JOIN information_schema. PROCESSLIST p
ON p.ID = b.trx_mysql_thread_id
ORDER BY wait_time DESC;
--杀掉线程
--or
select concat('KILL ',id,';') from information_schema.processlist where user='xhbx';
--执行上面查询结果杀进程
kill 线程id
kill 31642
--or
select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
source /tmp/a.txt;
-- 杀掉当前所有的MySQL连接
mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill
-- 杀掉指定用户运行的连接,这里为Mike
mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p kill
-- 查询数据库当前操作
-- 查看整体情况(数据库中存在的进程)
show processlist;
-- 查看完全的SQL语句
show full processlist;
select id,user,host,db,command,time,state,info from information_schema.processlist;
select id,user,host,db,command,time,state,info from information_schema.processlist where db='xhbx';
-- 查询是否自动提交
show variables like 'autocommit';
-- 查询未提交的事务
select count(*) from performance_schema.events_statements_current;
-- 查询正在执行的事物
SELECT TRX_ID, TRX_REQUESTED_LOCK_ID, TRX_MYSQL_THREAD_ID, TRX_QUERY
FROM information_schema.INNODB_TRX
WHERE TRX_STATE = 'LOCK WAIT';
-- 根据线程id查询具体锁信息
SELECT * FROM information_schema.INNODB_TRX where trx_mysql_thread_id="38164548";
-- 当前运行情况
show global status;
show engine innodb status;
show processlist;