如何查看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: test@10.160.2.55
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;