欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL 如何連接對(duì)應(yīng)的客戶端進(jìn)程

 更新時(shí)間:2020年11月20日 14:28:03   作者:蔣樂(lè)興  
這篇文章主要介紹了MySQL 如何連接對(duì)應(yīng)的客戶端進(jìn)程,幫助大家更好的理解和學(xué)習(xí)MySQL,感興趣的朋友可以了解下

問(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 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
 }

解決方案

從前面的內(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)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論