How does MySQL connect to the corresponding client process?

How does MySQL connect to the corresponding client process?

question

For a given MySQL connection, how can we know which client process it came from?

HandshakeResponse

When MySQL-Client connects to MySQL-Server, it not only sends the username and password to the server, but also the current process id, operating system name, host name and other information to the server. This data packet is called HandshakeResponse and the official website has a detailed description of its format.

I modified a connection driver myself, using which I can see what information is sent when connecting.

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'}

The byte format of the HandshakeResponse packet is as follows. The data to be transmitted is at the end of the packet.

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
 }

Solution

From the previous content, we can know that MySQL-Client does send the current process id to MySQL-Server, which provides the most basic possibility for solving the problem. When the server receives this information, it saves it to performance_schema.session_connect_attrs.

The first step is to query information_schema.processlist for the connection of interest, the IP it came from, and its 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)

The second step is to query the process ID of the connection through performance_schema.session_connect_attrs

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)

We can see that the connection with processlist_id = 7 is initiated by process 58471 of 172.16.192.1.

examine

I just used ipython to connect to the database, and the result I saw from ps was exactly 58471, which is consistent with the query result.

 ps -ef | grep 58471
 501 58471 57741 0 3:24 PMttys001 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

The above is the details of how MySQL connects to the corresponding client process. For more information about the corresponding client process of MySQL connection, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Introduction to using the MySQL mysqladmin client
  • Solve the problem of MySql client exiting in seconds (my.ini not found)
  • PHP Swoole asynchronous MySQL client implementation example
  • Solve the problem that the Node.js mysql client does not support the authentication protocol
  • Solution to the problem that the mysql8.0.11 client cannot log in
  • Detailed explanation of MySQL/Java server support for emoji and problem solving
  • Getting started with NodeJS server development (Express+MySQL)
  • MySQL connection pool for App server (supports high concurrency)
  • Interpreting MySQL client and server protocols

<<:  How to clear the validation prompt in element form validation

>>:  A brief discussion on the issue of element dragging and sorting in table

Recommend

How to monitor array changes in JavaScript

Preface When introducing defineProperty before, I...

How to install jupyter in docker on centos and open ports

Table of contents Install jupyter Docker port map...

Analysis of three parameters of MySQL replication problem

Table of contents 01 sql_slave_skip_counter param...

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

Modify file permissions (ownership) under Linux

Linux and Unix are multi-user operating systems, ...

Instructions for using the meta viewport tag (mobile browsing zoom control)

When OP opens a web page with the current firmwar...

Diving into JS inheritance

Table of contents Preface Prepare Summarize n way...

Detailed explanation of using scp command to copy files remotely in Linux

Preface scp is the abbreviation of secure copy. s...

Introduction to using MySQL commands to create, delete, and query indexes

MySQL database tables can create, view, rebuild a...

A quick review of CSS3 pseudo-class selectors

Preface If CSS is the basic skill of front-end de...

jQuery implements the drop-down box for selecting the place of residence

The specific code for using jQuery to implement t...