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

Detailed explanation of the problem when combining CSS ellipsis and padding

Text truncation with CSS Consider the following c...

How to Rename a Group of Files at Once on Linux

In Linux, we usually use the mv command to rename...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

MYSQL database GTID realizes master-slave replication (super convenient)

1. Add Maria source vi /etc/yum.repos.d/MariaDB.r...

In-depth explanation of the maximum value of int in MySQL

Introduction I will write about the problem I saw...

Use nginx + secondary domain name + https support

Step 1: Add a secondary domain name to the Alibab...

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

Vue3 uses axios interceptor to print front-end logs

Table of contents 1. Introduction 2. Use axios in...

Summary of web design experience and skills

■ Website theme planning Be careful not to make yo...

Example of removing json backslash in php

1. Remove backslashes through the "stripslas...

npm Taobao mirror modification explanation

1. Top-level usage 1. Install cnpm npm i -g cnpm ...

How to move a red rectangle with the mouse in Linux character terminal

Everything is a file! UNIX has already said it. E...

An article teaches you JS function inheritance

Table of contents 1. Introduction: 2. Prototype c...

Shorten the page rendering time to make the page run faster

How to shorten the page rendering time on the bro...