MySQL 如何連接對應(yīng)的客戶端進(jìn)程
問題
對于一個給定的 MySQL 連接,我們?nèi)绾尾拍苤浪鼇碜杂谀膫€客戶端的哪個進(jìn)程呢?
HandshakeResponse
MySQL-Client 在連接 MySQL-Server 的時(shí)候,不只會把用戶名密碼發(fā)送到服務(wù)端,還會把當(dāng)前進(jìn)程id,操作系統(tǒng)名,主機(jī)名等等信息也發(fā)到服務(wù)端。這個數(shù)據(jù)包就叫 HandshakeResponse 官方有對其格式進(jìn)行詳細(xì)的說明。
我自己改了一個連接驅(qū)動,用這個驅(qū)動可以看到連接時(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 ,這為解決問題提供了最基本的可能性。當(dāng)服務(wù)端收到這些信息后雙把它們保存到了 performance_schema.session_connect_attrs。
第一步通過 information_schema.processlist 查詢關(guān)心的連接,它來自于哪個 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 查詢連接的進(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 的這個連接是由 172.16.192.1 的 58471 號進(jìn)程發(fā)起的。
檢查
我剛才是用的 ipython 連接的數(shù)據(jù)庫,ps 看到的結(jié)果也正是 58471 與查詢出來的結(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 如何連接對應(yīng)的客戶端進(jìn)程的詳細(xì)內(nèi)容,更多關(guān)于MySQL 連接對應(yīng)的客戶端進(jìn)程的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Oracle數(shù)據(jù)庫中臨時(shí)表的進(jìn)一步深入研究2. 目前學(xué)習(xí)到的常用命令之Mariadb3. 深入探索數(shù)據(jù)庫MySQL性能優(yōu)化與復(fù)雜查詢相關(guān)操作4. SQLServer的內(nèi)存管理架構(gòu)詳解5. centos編譯安裝mariadb的詳細(xì)過程6. Linux安裝MariaDB數(shù)據(jù)庫的實(shí)例詳解7. MySQL百萬數(shù)據(jù)深度分頁優(yōu)化思路解析8. 盤點(diǎn)SqlServer 分頁方式和拉姆達(dá)表達(dá)式分頁9. Mysql優(yōu)化方法詳細(xì)介紹10. 初識SQLITE3數(shù)據(jù)庫
