MySQL基礎知識:MySQL Connection和Session
阿新 • • 發佈:2021-03-16
在connection的生命裡,會一直有一個user thread(以及user thread對應的THD)陪伴它。
## Connection和Session概念
來自Stackoverflow的一個回答:
```
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.
```
來自官方團隊的描述:
```
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](https://zhuchengliang.com/img/db/mysql-connect.png)
- Connection Requests: 是一個簡單的TCP-IP連線訊息,傳送到MySQL Server的埠(如:3306);
- Receiver Thread:唯一職責是建立 ```user thread```;要麼新建一個OS thread,要麼重用 thread cache裡的可用thread;
- User Thread: [client-server protocol](https://dev.mysql.com/doc/dev/mysql-server/latest/PAGE_PROTOCOL.html) 處理器,比如返回 ```handshake packet```,接收查詢、返回結果等等;
#### THD
- THD: 表示connection上下文的資料結構;連線建立後被建立,斷開連線後被銷燬;
- 使用者的connection和THD是一一對應的,THD不會被connection共用;
- THD資料結構的大小約為 ~10KB,注意用來跟蹤query執行狀態各個方面;
**注意:THD 一直沒查到是什麼的簡寫。從查閱的資料看,THD應該也可以被認為是 ```Session``` 或者 ```connection的狀態/上下文```。**
#### Command Phase
![mysql Active Connection](https://zhuchengliang.com/img/db/mysql-active-connection.png)
- 當connection phase一切安好後, ```user thread``` 會進入 ```command phase```;開始忙碌的一生。
#### 斷開連線
![mysql disconnect](https://zhuchengliang.com/img/db/MySQL-Disconnect.png)
Client傳送```COM_QUIT```命令開始斷開連線操作。
User Thread開始做清理工作:
- 釋放THD;
- ```thread cache```還有空位置: 把自己 放到 ```thread cache```裡並標記為 ```suspended```狀態;
- ```thread cache```沒有空位置:結束執行緒。
## 檢視MySQL Sessions/Active Connections
MySQL的連線資訊,記錄在```information_schema```和```performance_schema```資料庫中。
```sql
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 | | | |
+---------+---------------------+------+-----+---------+-------+
```
```sql
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:
```sql
show status where variable_name = 'threads_connected';
```
方法2:
```sql
show processlist;
```
方法3:
```sql
select id,
user,
host,
db,
command,
time,
state,
info
from information_schema.processlist;
```
#### 檢視每個host的當前連線數和總連線數
```sql
select * FROM performance_schema.hosts;
```
## 參考資料
1. [MySQL show status - active or total connections?](https://stackoverflow.com/questions/7432241/mysql-show-status-active-or-total-connections)
2. [MySQL concepts: session vs connection](https://stackoverflow.com/questions/8797724/mysql-concepts-session-vs-connection)
3. **推薦:** [MySQL Connection Handling and Scaling](https://mysqlserverteam.com/mysql-connection-handling-and-scaling/)
4. [Connection Phase](https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase.html)
5. [Command Phase](https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_command_phase.html)
6. [MySQL Error: Too many connections](https://www.percona.com/blog/2013/11/28/mysql-error-too-many-connections)
7. [5.1.10 Server Status Variables](https://dev.mysql.com/doc/refman/8.0/en/server-status-variabl