MySQL總共支持四種連接方式訪問資料庫,如下表格所示,其中Linux平臺環境下,主要有兩種連接方式,一種是TCP/IP連接方式,另外一種是socket方式。Name pipe和Share memory是Windows平臺下獨有的連接方式。 那麼,MySQL資料庫中,我們能否查看會話具體使用的連接方 ...
MySQL總共支持四種連接方式訪問資料庫,如下表格所示,其中Linux平臺環境下,主要有兩種連接方式,一種是TCP/IP連接方式,另外一種是socket方式。Name pipe和Share memory是Windows平臺下獨有的連接方式。
那麼,MySQL資料庫中,我們能否查看會話具體使用的連接方式呢(例如,使用TCP/IP連接方式)?如果有的話,總共有哪一些方法呢? 下麵簡單總結一下。
方法1:status命令查看
如下所示,socket方式連接資料庫的話,會有UNIX socket這樣的信息(紅色部分信息)
mysql> status;
--------------
mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id: 9
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.33 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
UNIX socket: /tmp/mysql.sock
Binary data as: Hexadecimal
Uptime: 4 hours 8 min 7 sec
Threads: 3 Questions: 45 Slow queries: 9 Opens: 153 Flush tables: 3 Open tables: 72 Queries per second avg: 0.003
--------------
mysql>
如下所示,TCP/IP方式連接資料庫的話,會有xxx.xxx.xxx.xxx via TCP/IP以及TCP port這樣的信息(紅色部分的信息)
mysql> status
--------------
mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
Connection id: 10
Current database:
Current user: [email protected]
SSL: Cipher in use is TLS_AES_256_GCM_SHA384
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.33 MySQL Community Server - GPL
Protocol version: 10
Connection: 10.160.2.53 via TCP/IP
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
TCP port: 3306
Binary data as: Hexadecimal
Uptime: 4 hours 8 min 47 sec
Threads: 3 Questions: 48 Slow queries: 9 Opens: 153 Flush tables: 3 Open tables: 72 Queries per second avg: 0.003
--------------
mysql>
但是status命令有一個很大的弊端,只能查看當前會話的連接方式,不能查看其它會話的連接信息,而我們一般主要是查看其它會話的連接方式。所以,這個命令非常有局限性。
方法2:通過通用查詢日誌(general log)查看
如下所示,你在通用查詢日誌(general log)中,你可以看到會話的連接的方式(紅色部分信息),但是一般情況下,MySQL資料庫並不會開啟通用查詢日誌,所以對於這種 方法,我們僅需知道有這麼一種方法,但是其實很少使用,就像你知道走路能走到北京去一樣,你根本不可能選擇這種方法。
2023-11-09T16:16:13.614201+08:00 11 Query show variables like '%general_log%'
2023-11-09T16:16:26.614163+08:00 11 Query select user,host from mysql.user
2023-11-09T16:18:21.214091+08:00 11 Query show variables like '%port%'
2023-11-09T16:18:42.378325+08:00 12 Connect test@10.160.3.104 on using SSL/TLS
2023-11-09T16:18:42.378492+08:00 12 Connect Access denied for user 'test'@'10.160.3.119' (using password: YES)
2023-11-09T16:18:47.802135+08:00 13 Connect test@10.160.3.104 on using SSL/TLS
2023-11-09T16:18:47.802659+08:00 13 Query select @@version_comment limit 1
2023-11-09T16:18:57.006583+08:00 11 Quit
2023-11-09T16:19:28.448989+08:00 14 Connect root@localhost on using Socket
2023-11-09T16:19:28.449468+08:00 14 Query select @@version_comment limit 1
2023-11-09T16:19:30.701042+08:00 14 Quit
方法3:MySQL的Enterprise Audit log查看
這種方法僅僅適用於MySQL企業版,MySQL社區版無法使用這種方法,所以也有一定的局限性。如下所示:
<AUDIT_RECORD>
<TIMESTAMP>2023-11-09T10:28:58 UTC</TIMESTAMP>
<RECORD_ID>16_2023-11-09T10:28:00</RECORD_ID>
<NAME>Connect</NAME>
<CONNECTION_ID>8</CONNECTION_ID>
<STATUS>0</STATUS>
<STATUS_CODE>0</STATUS_CODE>
<USER>root</USER>
<OS_LOGIN/>
<HOST>localhost</HOST>
<IP/>
<COMMAND_CLASS>connect</COMMAND_CLASS>
<CONNECTION_TYPE>Shared Memory</CONNECTION_TYPE>
<PRIV_USER>root</PRIV_USER>
<PROXY_USER/>
<DB/>
</AUDIT_RECORD>
方法4:通過performance_schema.threads查看
performance_schema.threads中有個欄位connection_type,它記錄了會話的連接方式,所以,我們可以通過這個表查看會話的連接方式。而且這種方法最方便,實用。
CONNECTION_TYPE的取值如下:
The protocol used to establish the connection, or NULL for background threads. Permitted values are:
TCP/IP (TCP/IP connection established without encryption),
SSL/TLS (TCP/IP connection established with encryption),
Socket (Unix socket file connection),
Named Pipe (Windows named pipe connection),
Shared Memory (Windows shared memory connection).
查看所有非後臺線程的連接方式:
select processlist_id
, processlist_user
, processlist_host
, thread_id
, connection_type
from performance_schema.threads
where processlist_user is not null
order by thread_id;
--查看某個會話的連接方式:
select processlist_id
, processlist_user
, processlist_host
, thread_id
, connection_type
from performance_schema.threads
where processlist_user is not null
and processlist_id=xxx --xxx用具體會話ID替換
order by thread_id;