MySQL 如何连接对应的客户端进程
问题
对于一个给定的MySQL连接,我们如何才能知道它来自于哪个客户端的哪个进程呢?
HandshakeResponse
MySQL-Client在连接MySQL-Server的时候,不只会把用户名密码发送到服务端,还会把当前进程id,操作系统名,主机名等等信息也发到服务端。这个数据包就叫HandshakeResponse官方有对其格式进行详细的说明。
我自己改了一个连接驱动,用这个驱动可以看到连接时发送了哪些信息。
2020-05-1915: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包的字节格式如下,要传输的数据就在包的最后部分。
4capabilityflags,CLIENT_PROTOCOL_41alwaysset 4max-packetsize 1characterset string[23]reserved(all[0]) string[NUL]username ifcapabilities&CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA{ lenenc-intlengthofauth-response string[n]auth-response }elseifcapabilities&CLIENT_SECURE_CONNECTION{ 1lengthofauth-response string[n]auth-response }else{ string[NUL]auth-response } ifcapabilities&CLIENT_CONNECT_WITH_DB{ string[NUL]database } ifcapabilities&CLIENT_PLUGIN_AUTH{ string[NUL]authpluginname } ifcapabilities&CLIENT_CONNECT_ATTRS{ lenenc-intlengthofallkey-values lenenc-strkey lenenc-strvalue if-moredatain'lengthofallkey-values',morekeysandvaluepairs }
解决方案
从前面的内容我们可以知道MySQL-Client确实向MySQL-Server发送了当前的进程id,这为解决问题提供了最基本的可能性。当服务端收到这些信息后双把它们保存到了performance_schema.session_connect_attrs。
第一步通过information_schema.processlist查询关心的连接,它来自于哪个IP,和它的processlist_id。
mysql>select*frominformation_schema.processlist; +----+---------+--------------------+--------------------+---------+------+-----------+----------------------------------------------+ |ID|USER|HOST|DB|COMMAND|TIME|STATE|INFO| +----+---------+--------------------+--------------------+---------+------+-----------+----------------------------------------------+ |8|root|127.0.0.1:57760|performance_schema|Query|0|executing|select*frominformation_schema.processlist| |7|appuser|172.16.192.1:50198|NULL|Sleep|2682||NULL| +----+---------+--------------------+--------------------+---------+------+-----------+----------------------------------------------+ 2rowsinset(0.01sec)
第二步通过performance_schema.session_connect_attrs查询连接的进程ID
mysql>select*fromsession_connect_attrswhereprocesslist_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| +----------------+-----------------+------------------------+------------------+ 7rowsinset(0.00sec)
可以看到processlist_id=7的这个连接是由172.16.192.1的58471号进程发起的。
检查
我刚才是用的ipython连接的数据库,ps看到的结果也正是58471与查询出来的结果一致。
ps-ef|grep58471 501584715774103:24下午ttys0010: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连接对应的客户端进程的资料请关注毛票票其它相关文章!