在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;