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

吾爱主题 阅读:155 2024-04-01 23:51:36 评论:0

问题

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

HandshakeResponse

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

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

?
1 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 包的字节格式如下,要传输的数据就在包的最后部分。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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-response 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 。

?
1 2 3 4 5 6 7 8 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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 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)

可以看到 processlist_id = 7 的这个连接是由 172.16.192.1 的 58471 号进程发起的。

检查

我刚才是用的 ipython 连接的数据库,ps 看到的结果也正是 58471 与查询出来的结果一致。

?
1 2 ps -ef | grep 58471 501 58471 57741  0 3:24下午 ttys001  0:03.67 /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python /Library/Frameworks/Python.framework/Versions/3.8/bin/ipython

以上就是MySQL 如何连接对应的客户端进程的详细内容,更多关于MySQL 连接对应的客户端进程的资料请关注服务器之家其它相关文章!

原文链接:https://www.sqlpy.com/blogs/283699273

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容