MySQL基礎知識:MySQL Connection和Session

在connection的生命裏,會一直有一個user thread(以及user thread對應的THD)陪伴它。html

Connection和Session概念

來自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.

MySQL Client和MySQL Server創建鏈接的過程

Connection Phase

mysql connection

  • Connection Requests: 是一個簡單的TCP-IP鏈接消息,發送到MySQL Server的端口(如:3306);
  • Receiver Thread:惟一職責是建立 user thread;要麼新建一個OS thread,要麼重用 thread cache裏的可用thread;
  • User Thread: client-server protocol 處理器,好比返回 handshake packet,接收查詢、返回結果等等;

THD

  • THD: 表示connection上下文的數據結構;鏈接創建後被建立,斷開鏈接後被銷燬;
  • 用戶的connection和THD是一一對應的,THD不會被connection共用;
  • THD數據結構的大小約爲 ~10KB,注意用來跟蹤query執行狀態各個方面;

注意:THD 一直沒查到是什麼的簡寫。從查閱的資料看,THD應該也能夠被認爲是 Session 或者 connection的狀態/上下文數據庫

Command Phase

mysql Active Connection

  • 當connection phase一切安好後, user thread 會進入 command phase;開始忙碌的一輩子。

斷開鏈接

mysql disconnect

Client發送COM_QUIT命令開始斷開鏈接操做。session

User Thread開始作清理工做:數據結構

  • 釋放THD;
  • thread cache還有空位置: 把本身 放到 thread cache裏並標記爲 suspended狀態;
  • thread cache沒有空位置:結束線程。

查看MySQL Sessions/Active Connections

MySQL的鏈接信息,記錄在information_schemaperformance_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;

查看每一個host的當前鏈接數和總鏈接數

select * FROM performance_schema.hosts;

參考資料

  1. MySQL show status - active or total connections?
  2. MySQL concepts: session vs connection
  3. 推薦: MySQL Connection Handling and Scaling
  4. Connection Phase
  5. Command Phase
  6. MySQL Error: Too many connections
  7. 5.1.10 Server Status Variables
相關文章
相關標籤/搜索