在connection的生命裏,會一直有一個user thread(以及user thread對應的THD)陪伴它。html
來自Stackoverflow的一個回答:mysql
A session is just a result of a successful connection. Any MySQL client requires some connection settings to establish a connection, and after the connection has been established, it acquires a connection id (thread id) and some context which is called session.
來自官方團隊的描述:sql
Connections correspond to Sessions in SQL standard terminology. A client connects to the MySQL Server and stays connected until it does a disconnect.
user thread
;要麼新建一個OS thread,要麼重用 thread cache裏的可用thread;handshake packet
,接收查詢、返回結果等等;注意:THD 一直沒查到是什麼的簡寫。從查閱的資料看,THD應該也能夠被認爲是 Session
或者 connection的狀態/上下文
。數據庫
user thread
會進入 command phase
;開始忙碌的一輩子。Client發送COM_QUIT
命令開始斷開鏈接操做。session
User Thread開始作清理工做:數據結構
thread cache
還有空位置: 把本身 放到 thread cache
裏並標記爲 suspended
狀態;thread cache
沒有空位置:結束線程。MySQL的鏈接信息,記錄在information_schema
和performance_schema
數據庫中。sqlserver
desc information_schema.processlist;
+---------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------------+------+-----+---------+-------+ | ID | bigint(21) unsigned | NO | | | | | USER | varchar(32) | NO | | | | | HOST | varchar(64) | NO | | | | | DB | varchar(64) | YES | | | | | COMMAND | varchar(16) | NO | | | | | TIME | int(7) | NO | | | | | STATE | varchar(64) | YES | | | | | INFO | varchar(65535) | YES | | | | +---------+---------------------+------+-----+---------+-------+
desc performance_schema.hosts;
+---------------------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------------+------------+------+-----+---------+-------+ | HOST | char(60) | YES | UNI | NULL | | | CURRENT_CONNECTIONS | bigint(20) | NO | | NULL | | | TOTAL_CONNECTIONS | bigint(20) | NO | | NULL | | +---------------------+------------+------+-----+---------+-------+
方法1:ui
show status where variable_name = 'threads_connected';
方法2:線程
show processlist;
方法3:code
select id, user, host, db, command, time, state, info from information_schema.processlist;
select * FROM performance_schema.hosts;