• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL 如何连接对应的客户端进程

mysql 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

问题

对于一个给定的 MySQL 连接,我们如何才能知道它来自于哪个客户端的哪个进程呢?

HandshakeResponse

MySQL-Client 在连接 MySQL-Server 的时候,不只会把用户名密码发送到服务端,还会把当前进程id,操作系统名,主机名等等信息也发到服务端。这个数据包就叫 HandshakeResponse 官方有对其格式进行详细的说明。

我自己改了一个连接驱动,用这个驱动可以看到连接时发送了哪些信息。

2020-05-19 15:31:04,976 - mysql-connector-python.mysql.connector.protocol.MySQLProtocol.make_auth - MainThread - INFO - conn-attrs {'_pid': '58471', '_platform': 'x86_64', '_source_host': 'NEEKYJIANG-MB1', '_client_name': 'mysql-connector-python', '_client_license': 'GPL-2.0', '_client_version': '8.0.20', '_os': 'macOS-10.15.3'}

HandshakeResponse 包的字节格式如下,要传输的数据就在包的最后部分。

4       capability flags, CLIENT_PROTOCOL_41 always set
4       max-packet size
1       character set
string[23]   reserved (all [0])
string[NUL]  username
 if capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA {
lenenc-int   length of auth-respons<span>本文来源gaodai#ma#com搞*!代#%^码$网*</span>e
string[n]   auth-response
 } else if capabilities & CLIENT_SECURE_CONNECTION {
1       length of auth-response
string[n]   auth-response
 } else {
string[NUL]  auth-response
 }
 if capabilities & CLIENT_CONNECT_WITH_DB {
string[NUL]  database
 }
 if capabilities & CLIENT_PLUGIN_AUTH {
string[NUL]  auth plugin name
 }
 if capabilities & CLIENT_CONNECT_ATTRS {
lenenc-int   length of all key-values
lenenc-str   key
lenenc-str   value
  if-more data in 'length of all key-values', more keys and value pairs
 }

解决方案

从前面的内容我们可以知道 MySQL-Client 确实向 MySQL-Server 发送了当前的进程 id ,这为解决问题提供了最基本的可能性。当服务端收到这些信息后双把它们保存到了 performance_schema.session_connect_attrs。

第一步通过 information_schema.processlist 查询关心的连接,它来自于哪个 IP,和它的 processlist_id 。

mysql> select * from information_schema.processlist;
+----+---------+--------------------+--------------------+---------+------+-----------+----------------------------------------------+
| ID | USER  | HOST        | DB         | COMMAND | TIME | STATE   | INFO                     |
+----+---------+--------------------+--------------------+---------+------+-----------+----------------------------------------------+
| 8 | root  | 127.0.0.1:57760  | performance_schema | Query  |  0 | executing | select * from information_schema.processlist |
| 7 | appuser | 172.16.192.1:50198 | NULL        | Sleep  | 2682 |      | NULL                     |
+----+---------+--------------------+--------------------+---------+------+-----------+----------------------------------------------+
2 rows in set (0.01 sec)

第二步通过 performance_schema.session_connect_attrs 查询连接的进程 ID

mysql> select * from session_connect_attrs where processlist_id = 7;               
+----------------+-----------------+------------------------+------------------+
| PROCESSLIST_ID | ATTR_NAME    | ATTR_VALUE       | ORDINAL_POSITION |
+----------------+-----------------+------------------------+------------------+
|       7 | _pid      | 58471         |        0 |
|       7 | _platform    | x86_64         |        1 |
|       7 | _source_host  | NEEKYJIANG-MB1     |        2 |
|       7 | _client_name  | mysql-connector-python |        3 |
|       7 | _client_license | GPL-2.0        |        4 |
|       7 | _client_version | 8.0.20         |        5 |
|       7 | _os       | macOS-10.15.3     |        6 |
+----------------+-----------------+------------------------+------------------+
7 rows in set (0.00 sec)

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL 如何连接对应的客户端进程

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址