MySQL 如何連接對(duì)應(yīng)的客戶端進(jìn)程
問(wèn)題
對(duì)于一個(gè)給定的 MySQL 連接,我們?nèi)绾尾拍苤浪鼇?lái)自于哪個(gè)客戶端的哪個(gè)進(jìn)程呢?
HandshakeResponse
MySQL-Client 在連接 MySQL-Server 的時(shí)候,不只會(huì)把用戶名密碼發(fā)送到服務(wù)端,還會(huì)把當(dāng)前進(jìn)程id,操作系統(tǒng)名,主機(jī)名等等信息也發(fā)到服務(wù)端。這個(gè)數(shù)據(jù)包就叫 HandshakeResponse 官方有對(duì)其格式進(jìn)行詳細(xì)的說(shuō)明。
我自己改了一個(gè)連接驅(qū)動(dòng),用這個(gè)驅(qū)動(dòng)可以看到連接時(shí)發(fā)送了哪些信息。
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 包的字節(jié)格式如下,要傳輸?shù)臄?shù)據(jù)就在包的最后部分。
4 capability flags, CLIENT_PROTOCOL_41 always set4 max-packet size1 character setstring[23] reserved (all [0])string[NUL] username if capabilities & CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA {lenenc-int length of auth-responsestring[n] auth-response } else if capabilities & CLIENT_SECURE_CONNECTION {1 length of auth-responsestring[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-valueslenenc-str keylenenc-str value if-more data in ’length of all key-values’, more keys and value pairs }
解決方案
從前面的內(nèi)容我們可以知道 MySQL-Client 確實(shí)向 MySQL-Server 發(fā)送了當(dāng)前的進(jìn)程 id ,這為解決問(wèn)題提供了最基本的可能性。當(dāng)服務(wù)端收到這些信息后雙把它們保存到了 performance_schema.session_connect_attrs。
第一步通過(guò) information_schema.processlist 查詢關(guān)心的連接,它來(lái)自于哪個(gè) 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)
第二步通過(guò) performance_schema.session_connect_attrs 查詢連接的進(jìn)程 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 的這個(gè)連接是由 172.16.192.1 的 58471 號(hào)進(jìn)程發(fā)起的。
檢查
我剛才是用的 ipython 連接的數(shù)據(jù)庫(kù),ps 看到的結(jié)果也正是 58471 與查詢出來(lái)的結(jié)果一致。
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 如何連接對(duì)應(yīng)的客戶端進(jìn)程的詳細(xì)內(nèi)容,更多關(guān)于MySQL 連接對(duì)應(yīng)的客戶端進(jìn)程的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Microsoft Office Access設(shè)置小數(shù)位數(shù)的方法2. MySQL基本調(diào)度策略淺析3. mybatis 為什么千萬(wàn)不要使用 where 1=14. 巧用SQL語(yǔ)言在ACCESS數(shù)據(jù)庫(kù)中批量替換內(nèi)容5. 數(shù)據(jù)庫(kù)相關(guān)的幾個(gè)技能:ACCESS轉(zhuǎn)SQL6. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)7. Access數(shù)據(jù)庫(kù)日常維護(hù)方法(優(yōu)化)8. DB2 常用命令小結(jié)9. Mysql入門系列:安排預(yù)防性的維護(hù)MYSQL數(shù)據(jù)庫(kù)服務(wù)器10. 微軟需要改進(jìn)SQL Server的六大功能

網(wǎng)公網(wǎng)安備