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

2022-07-27,,,

问题

对于一个给定的 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-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 。

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)

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

检查

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

 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 连接对应的客户端进程的资料请关注其它相关文章!

《MySQL 如何连接对应的客户端进程.doc》

下载本文的Word格式文档,以方便收藏与打印。