Packet capture and analysis of mysql traffic under Linux C/C++ (protocol analysis)

The MySQL communication protocol is a stateful protocol mainly used for communication between MySQL clients and servers. This protocol is implemented in MySQL client connectors (such as Connector/C, Connector/J, etc.), MySQL Proxy, and master-slave replication.

Features of the protocol include support for SSL, compression and authentication.

The interaction process between the MySQL client and the server is mainly divided into two stages: the handshake authentication stage and the command execution stage.
During the handshake authentication phase, the server will generate some service information, including protocol number, server version, thread id, encrypted random numbers (seeds), server capability flag and other information. The client will generate authentication information, including client capability flag, user name, encryption password, connection database name and other information. Then, the server returns the authentication result. If the verification is correct, it returns an OK packet, otherwise it is an exception packet.

In addition, the MySQL communication protocol also supports a variety of inter-process communication methods, such as pipes, named pipes, shared memory, TCP/IP sockets, and UNIX domain sockets. In practical applications, tools such as Wireshark can be used to capture and analyze the MySQL protocol.

mysql protocol handshake process

The handshake process of the MySQL protocol includes the interaction process between the client and the server. Here’s an overview of the process:

  1. The client sends a connection request to the server.

  2. After receiving the connection request, the server returns a handshake packet to the client.

  3. After receiving the handshake packet, the client parses the information in it, including server version, connection status, etc.

  4. The client determines the protocol version to be used based on the information in the handshake packet, and sends a handshake response packet to the server.

  5. After receiving the handshake response packet, the server verifies the client’s identity and authority, and replies with an authentication result to the client.

  6. After receiving the authentication result, the client performs corresponding processing according to the result, such as closing the connection or entering the next step.

  7. If the authentication is successful, normal communication begins between the client and the server, and subsequent database operations such as querying and inserting can be performed.

  8. The client returns the query results to the server, and the server receives the results and returns them to the client.

  9. Disconnect request

    MySQL starts the interactive process of SSL encryption protocol

  1. Server configuration SSL: First, the MySQL server needs to configure an SSL certificate and key. The server generates its own SSL certificate and configures the certificate and key into the MySQL server’s configuration file.

  2. The client requests an encrypted connection: When the client connects to the MySQL server, it needs to specify the SSL protocol in the connection string. For example, you can add the --ssl parameter to the connection command, or set ssl-mode to “required” in the connection configuration file.

  3. Server handshake negotiation: When the server receives the client’s connection request and requests to use the SSL protocol, the server will reply with a handshake packet containing the server’s SSL certificate and other SSL parameters.

  4. Client verification certificate: After the client receives the server’s handshake packet, it will verify the server’s SSL certificate. The client checks that the certificate is legitimate and signed by a trusted issuer. If the certificate is valid, the connection continues.

  5. Server verifies client certificate: If the server needs to verify the client’s SSL certificate, the server will ask the client to provide the SSL certificate and verify the certificate. If the verification is successful, the connection continues.

  6. SSL connection establishment: After the verification process is completed, the two parties begin to communicate using the SSL protocol, and all data transmission will be encrypted.

Through the above handshake and verification process, the MySQL client and server establish a secure encrypted connection to ensure the confidentiality and integrity of data during transmission.

mysql client/server functionality

#define MYSQL_CAPS_LP 0x0001 /* CLIENT_LONG_PASSWORD */
#define MYSQL_CAPS_FR 0x0002 /* CLIENT_FOUND_ROWS */
#define MYSQL_CAPS_LF 0x0004 /* CLIENT_LONG_FLAG */
#define MYSQL_CAPS_CD 0x0008 /* CLIENT_CONNECT_WITH_DB */
#define MYSQL_CAPS_NS 0x0010 /* CLIENT_NO_SCHEMA */
#define MYSQL_CAPS_CP 0x0020 /* CLIENT_COMPRESS */
#define MYSQL_CAPS_OB 0x0040 /* CLIENT_ODBC */
#define MYSQL_CAPS_LI 0x0080 /* CLIENT_LOCAL_FILES */
#define MYSQL_CAPS_IS 0x0100 /* CLIENT_IGNORE_SPACE */
#define MYSQL_CAPS_CU 0x0200 /* CLIENT_PROTOCOL_41 */
#define MYSQL_CAPS_IA 0x0400 /* CLIENT_INTERACTIVE */
#define MYSQL_CAPS_SL 0x0800 /* CLIENT_SSL */
#define MYSQL_CAPS_II 0x1000 /* CLIENT_IGNORE_SPACE */
#define MYSQL_CAPS_TA 0x2000 /* CLIENT_TRANSACTIONS */
#define MYSQL_CAPS_RS 0x4000 /* CLIENT_RESERVED */
#define MYSQL_CAPS_SC 0x8000 /* CLIENT_SECURE_CONNECTION */

How to achieve communication between mysql client and server? What are the characteristics of the protocol?

Communication between the MySQL client and server is achieved through the TCP/IP protocol. The following are some characteristics of the MySQL protocol:

  1. Based on the request-response model: During the communication process, the client sends a request to the server, and the server returns a response to the client after processing.

  2. Binary protocol: MySQL protocol uses binary format for transmission. Each data message contains a specific field structure and corresponding data type.

  3. Data message format: The data message consists of a fixed-length header and a variable-length payload. The header contains information such as protocol version, connection ID, data length, etc.

  4. Multi-threading support: The MySQL protocol supports multiple threads to execute multiple queries concurrently on the same connection. The server assigns a thread ID to each thread.

  5. Long connection: The MySQL protocol supports long connections, that is, the connection between the client and the server can remain open, reducing the overhead of connecting and disconnecting.

  6. SQL statement support: MySQL protocol can send SQL statements (such as query statements, transaction statements, etc.) to the server for execution, and receive the result set returned by the server.

  7. Error handling: MySQL protocol supports the server to return error information, including error code and error description, so that the client can handle abnormal situations.

  8. Secure connection: MySQL protocol supports communication encryption through SSL/TLS to protect the security of session data.

mysql server status (bit field)

#define MYSQL_STAT_IT 0x0001
#define MYSQL_STAT_AC 0x0002
#define MYSQL_STAT_MR 0x0004
#define MYSQL_STAT_MU 0x0008
#define MYSQL_STAT_BI 0x0010
#define MYSQL_STAT_NI 0x0020
#define MYSQL_STAT_CR 0x0040
#define MYSQL_STAT_LR 0x0080
#define MYSQL_STAT_DR 0x0100
#define MYSQL_STAT_BS 0x0200
#define MYSQL_STAT_SESSION_STATE_CHANGED 0x0400
#define MYSQL_STAT_QUERY_WAS_SLOW 0x0800
#define MYSQL_STAT_PS_OUT_PARAMS 0x1000

MySQL command code

#define MYSQL_SLEEP 0 /* not from client */
#defineMYSQL_QUIT 1
#define MYSQL_INIT_DB 2
#defineMYSQL_QUERY 3
#define MYSQL_FIELD_LIST 4
#define MYSQL_CREATE_DB 5
#define MYSQL_DROP_DB 6
#defineMYSQL_REFRESH 7
#defineMYSQL_SHUTDOWN 8
#defineMYSQL_STATISTICS 9
#define MYSQL_PROCESS_INFO 10
#define MYSQL_CONNECT 11 /* not from client */
#define MYSQL_PROCESS_KILL 12
#defineMYSQL_DEBUG 13
#defineMYSQL_PING 14
#define MYSQL_TIME 15 /* not from client */
#define MYSQL_DELAY_INSERT 16 /* not from client */
#define MYSQL_CHANGE_USER 17
#define MYSQL_BINLOG_DUMP 18 /* replication */
#define MYSQL_TABLE_DUMP 19 /* replication */
#define MYSQL_CONNECT_OUT 20 /* replication */
#define MYSQL_REGISTER_SLAVE 21 /* replication */
#define MYSQL_STMT_PREPARE 22
#define MYSQL_STMT_EXECUTE 23
#define MYSQL_STMT_SEND_LONG_DATA 24
#define MYSQL_STMT_CLOSE 25
#define MYSQL_STMT_RESET 26
#define MYSQL_SET_OPTION 27
#define MYSQL_STMT_FETCH 28

MySQL provides multiple commands for performing database operations. The following lists some commonly used MySQL commands and their functions:

  1. USE : Switch to the specified database.

  2. SHOW DATABASES: Lists all available databases.

  3. SHOW TABLES: Lists all tables in the current database.

  4. SHOW COLUMNS FROM : List all fields of the specified table.

  5. SELECT FROM WHERE : Query data that meets the conditions from the specified table.

  6. INSERT INTO () VALUES (): Insert new rows into the specified table.

  7. UPDATE SET = WHERE : Update qualified rows in the specified table.

  8. DELETE FROM WHERE : Delete qualified rows from the specified table.

  9. ALTER TABLE ADD COLUMN : Add a new column to the specified table.

  10. CREATE TABLE (): Create a new table.

  11. DROP TABLE : Delete the specified table.

  12. GRANT ON TO @: Grants the user permissions on the specified table.

  13. REVOKE ON FROM @: Revokes permissions on the specified table from the user.

  14. DESCRIBE or EXPLAIN : Display detailed structural information of the specified table.

The above are just some common MySQL commands. There are many more commands that can be used to perform complex database operations, such as merging, grouping, sorting, joining, etc. The specific effects of MySQL commands depend on the context and parameters used.

mysql data packet type analysis

static const struct val_str mysql_state_table[] = {<!-- -->
    {<!-- -->UNDEFINED, "undefined"},
    {<!-- -->LOGIN, "login"},
    {<!-- -->REQUEST, "request"},
    {<!-- -->RESPONSE_OK, "response OK"},
    {<!-- -->RESPONSE_MESSAGE, "response message"},
    {<!-- -->RESPONSE_TABULAR, "tabular response"},
    {<!-- -->RESPONSE_SHOW_FIELDS, "response to SHOW FIELDS"},
    {<!-- -->FIELD_PACKET, "field packet"},
    {<!-- -->ROW_PACKET, "row packet"},
    {<!-- -->RESPONSE_PREPARE, "response to PREPARE"},
    {<!-- -->PREPARED_PARAMETERS, "parameters in response to PREPARE"},
    {<!-- -->PREPARED_FIELDS, "fields in response to PREPARE"},
    {<!-- -->AUTH_SWITCH_REQUEST, "authentication switch request"},
    {<!-- -->AUTH_SWITCH_RESPONSE, "authentication switch response"},
    {<!-- -->0, NULL}};

The main types of MySQL protocol data packets include the following:

  • Connection request message (Handshake Packet)

The connection request message is used to establish a connection between the client and the server. The meaning of its fields is as follows:
Protocol Version: Indicates the MySQL protocol version used between the client and the server. For example, 5.7.21 indicates the protocol using MySQL 5.7.21 version.
Server Version: Indicates the MySQL version information of the server.
Connection ID: Represents the connection identifier between the client and the server.
Salt: A random string used to encrypt the connection password.
Session Capabilities: Indicates the session features supported by the client, such as transactions, multi-statement execution, etc.
Charset: Indicates the character encoding used for communication between the client and the server.
Client Capabilities: Indicates the features supported by the client, such as supported SQL syntax, plug-ins, etc.

  • Query request message (Query Packet)

The query request message is used to send SQL query statements to the server. The meaning of its fields is as follows:
Command Type: Indicates the type of query request. The value 0x03 indicates a normal query request.
Status Code: Indicates the result of the server processing the request, 0 indicates success, and other values indicate errors.
Protocol Version: Indicates the MySQL protocol version used between the client and the server.
Message: Indicates detailed information about the request processing results returned by the server, usually including error information or warning information.
Fields Present (field presence flag): Indicates the number of fields actually present in the data packet.
Column Count: Indicates the number of columns in the response result.
Row Data: Contains each row of query results.

  • Close connection packet (Terminate Packet)

The connection close message is used to terminate the connection between the client and the server. The meaning of its fields is as follows:
Status Code: Indicates the result of the server processing the request, 0 indicates success, and other values indicate errors.
Reason: Indicates the reason for closing the connection, such as normal shutdown, timeout, etc.
Protocol Version: Indicates the MySQL protocol version used between the client and the server.

MySQL’s basic data types

MySQL’s basic data types include:

  • 1. Numeric data type:

Integer types: including TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT. These types occupy the number of bytes (storage space) from small to large.
Floating point decimal data types: including FLOAT and DOUBLE.
Fixed-point decimal type: represented by DECIMAL(M,N), where M represents the precision and the total number of digits; N represents the scale and the number of decimal digits.

  • 2. Date and time data types:

YEAR
DATE
TIME
DATETIME
TIMESTAMP

  • 3. String data type:

CHAR: Fixed-length string, the maximum length is 255 characters.
VARCHAR: Variable-length string, with a maximum length of 65535 characters.
TINYTEXT: A very small text string with a maximum length of 255 characters.
TEXT: Small text string with a maximum length of 65535 characters.
BINARY: Binary string, maximum length is 65535 characters.
VARBINARY: Variable-length binary string, with a maximum length of 65535 characters.

  • 4. Binary data type:

BINARY, VARBINARY, BIT, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB.

These basic types provide MySQL with rich data expression methods. When using MySQL, you can choose the appropriate data type to store data according to your needs.

Linux C/C++ packet capture and analysis of mysql traffic (protocol analysis)

...
#define MYSQL_CAPS_LP 0x0001 /* CLIENT_LONG_PASSWORD */
#define MYSQL_CAPS_FR 0x0002 /* CLIENT_FOUND_ROWS */
#define MYSQL_CAPS_LF 0x0004 /* CLIENT_LONG_FLAG */
#define MYSQL_CAPS_CD 0x0008 /* CLIENT_CONNECT_WITH_DB */
#define MYSQL_CAPS_NS 0x0010 /* CLIENT_NO_SCHEMA */
#define MYSQL_CAPS_CP 0x0020 /* CLIENT_COMPRESS */
#define MYSQL_CAPS_OB 0x0040 /* CLIENT_ODBC */
#define MYSQL_CAPS_LI 0x0080 /* CLIENT_LOCAL_FILES */
#define MYSQL_CAPS_IS 0x0100 /* CLIENT_IGNORE_SPACE */
#define MYSQL_CAPS_CU 0x0200 /* CLIENT_PROTOCOL_41 */
#define MYSQL_CAPS_IA 0x0400 /* CLIENT_INTERACTIVE */
#define MYSQL_CAPS_SL 0x0800 /* CLIENT_SSL */
#define MYSQL_CAPS_II 0x1000 /* CLIENT_IGNORE_SPACE */
#define MYSQL_CAPS_TA 0x2000 /* CLIENT_TRANSACTIONS */
#define MYSQL_CAPS_RS 0x4000 /* CLIENT_RESERVED */
#define MYSQL_CAPS_SC 0x8000 /* CLIENT_SECURE_CONNECTION */

/* field flags */
#define MYSQL_FLD_NOT_NULL_FLAG 0x0001
#define MYSQL_FLD_PRI_KEY_FLAG 0x0002
#define MYSQL_FLD_UNIQUE_KEY_FLAG 0x0004
#define MYSQL_FLD_MULTIPLE_KEY_FLAG 0x0008
#define MYSQL_FLD_BLOB_FLAG 0x0010
#define MYSQL_FLD_UNSIGNED_FLAG 0x0020
#define MYSQL_FLD_ZEROFILL_FLAG 0x0040
#define MYSQL_FLD_BINARY_FLAG 0x0080
#define MYSQL_FLD_ENUM_FLAG 0x0100
#define MYSQL_FLD_AUTO_INCREMENT_FLAG 0x0200
#define MYSQL_FLD_TIMESTAMP_FLAG 0x0400
#define MYSQL_FLD_SET_FLAG 0x0800
...
#define MYSQL_CAPS_MS 0x0001 /* CLIENT_MULTI_STATMENTS */
#define MYSQL_CAPS_MR 0x0002 /* CLIENT_MULTI_RESULTS */
#define MYSQL_CAPS_PM 0x0004 /* CLIENT_PS_MULTI_RESULTS */
#define MYSQL_CAPS_PA 0x0008 /* CLIENT_PLUGIN_AUTH */
#define MYSQL_CAPS_CA 0x0010 /* CLIENT_CONNECT_ATTRS */
#define MYSQL_CAPS_AL 0x0020 /* CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA */
#define MYSQL_CAPS_EP 0x0040 /* CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS */
#define MYSQL_CAPS_ST 0x0080 /* CLIENT_SESSION_TRACK */
#define MYSQL_CAPS_DE 0x0100 /* CLIENT_DEPRECATE_EOF */
#define MYSQL_CAPS_UNUSED 0xFE00

/* status bitfield */
#define MYSQL_STAT_IT 0x0001
#define MYSQL_STAT_AC 0x0002
#define MYSQL_STAT_MR 0x0004
#define MYSQL_STAT_MU 0x0008
#define MYSQL_STAT_BI 0x0010
#define MYSQL_STAT_NI 0x0020
#define MYSQL_STAT_CR 0x0040
#define MYSQL_STAT_LR 0x0080
#define MYSQL_STAT_DR 0x0100
#define MYSQL_STAT_BS 0x0200
#define MYSQL_STAT_SESSION_STATE_CHANGED 0x0400
#define MYSQL_STAT_QUERY_WAS_SLOW 0x0800
#define MYSQL_STAT_PS_OUT_PARAMS 0x1000

/* bitfield for MYSQL_REFRESH */
#define MYSQL_RFSH_GRANT 1 /* Refresh grant tables */
#define MYSQL_RFSH_LOG 2 /* Start on new log file */
#define MYSQL_RFSH_TABLES 4 /* close all tables */
#define MYSQL_RFSH_HOSTS 8 /* Flush host cache */
#define MYSQL_RFSH_STATUS 16 /* Flush status variables */
#define MYSQL_RFSH_THREADS 32 /* Flush thread cache */
#define MYSQL_RFSH_SLAVE 64 /* Reset master info and restart slave thread */
#define MYSQL_RFSH_MASTER 128 /* Remove all bin logs in the index and truncate the index */

/* MySQL command codes */
#define MYSQL_SLEEP 0 /* not from client */
#defineMYSQL_QUIT 1
#define MYSQL_INIT_DB 2
#defineMYSQL_QUERY 3
#define MYSQL_FIELD_LIST 4
#define MYSQL_CREATE_DB 5
#define MYSQL_DROP_DB 6
#defineMYSQL_REFRESH 7
#defineMYSQL_SHUTDOWN 8
#defineMYSQL_STATISTICS 9
#define MYSQL_PROCESS_INFO 10
#define MYSQL_CONNECT 11 /* not from client */
#define MYSQL_PROCESS_KILL 12
#defineMYSQL_DEBUG 13
#defineMYSQL_PING 14
#define MYSQL_TIME 15 /* not from client */
#define MYSQL_DELAY_INSERT 16 /* not from client */
#define MYSQL_CHANGE_USER 17
#define MYSQL_BINLOG_DUMP 18 /* replication */
#define MYSQL_TABLE_DUMP 19 /* replication */
#define MYSQL_CONNECT_OUT 20 /* replication */
#define MYSQL_REGISTER_SLAVE 21 /* replication */
#define MYSQL_STMT_PREPARE 22
#define MYSQL_STMT_EXECUTE 23
#define MYSQL_STMT_SEND_LONG_DATA 24
#define MYSQL_STMT_CLOSE 25
#define MYSQL_STMT_RESET 26
#define MYSQL_SET_OPTION 27
#define MYSQL_STMT_FETCH 28
...

static void mysql_dissect_auth_switch_request(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_auth_switch_response(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_error_packet(struct buffer *buf);
static void mysql_set_conn_state(mysql_conn_data_t *conn_data, mysql_state_t state);
static void mysql_dissect_greeting(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_login(struct buffer *buf, mysql_conn_data_t *conn_data);
static int mysql_dissect_attributes(struct buffer *buf);
static void mysql_dissect_request(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_response(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_result_header(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_ok_packet(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_dissect_field_packet(struct buffer *buf, mysql_conn_data_t *conn_data);
static int mysql_dissect_session_tracker_entry(struct buffer *buf);
static void mysql_dissect_row_packet(struct buffer *buf);
static void mysql_dissect_exec_string(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_time(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_datetime(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_tiny(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_short(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_long(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_float(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_double(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_longlong(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static void mysql_dissect_exec_null(struct buffer *buf, uint8_t param_unsigned, int *param_idx);
static char mysql_dissect_exec_param(struct buffer *buf, int *param_idx, uint8_t param_flags);
static void mysql_dissect_response_prepare(struct buffer *buf, mysql_conn_data_t *conn_data);
static void mysql_print_bytes(const char *payload, size_t size);
...

int main(int argc, char **argv)
{<!-- -->
...
    while (opt != -1)
    {<!-- -->
        switch(opt)
        {<!-- -->
        case 'i':
            opts->interface = strdup(optarg);
            assert(opts->interface);
            break;
        case 'p':

            i + = snprintf(opts->expression + i, max_filter_sz - i - 1, "tcp and ( port 0 ");
            assert(i <= max_filter_sz - 1);
            while ((port = strsep( & amp;optarg, ",")) != NULL)
            {<!-- -->
                if (port)
                {<!-- -->
                    if (!atoi(port))
                    {<!-- -->
                        LOG_ERROR("The port number is incorrect %s", port);
                        goto free;
                    }
                    opts->mysql_server_ports[opts->port_sz + + ] = atoi(port);
                    i + = snprintf(opts->expression + i, max_filter_sz - i - 1, "or port %d ", atoi(port));
                    assert(i <= max_filter_sz - 1);
                    if (opts->port_sz >= max_port_sz)
                    {<!-- -->
                        LOG_ERROR("The number of ports exceeds the limit max=%d", max_port_sz);
                        break;
                    }
                }
            }
            i + = snprintf(opts->expression + i, max_filter_sz - i - 1, ")");
            assert(i <= max_filter_sz - 1);
            break;
        case 'v':
            opts->verbose = !!optarg;
            break;
        case '?':
            usage();
            break;
        default:
            break;
        }
        opt = getopt(argc, argv, optString);
        optarg = str_trim(optarg, "=");
    }
    if (!opts->interface)
    {<!-- -->
        opts->interface = strdup("eth0");
        assert(opts->interface);
    }
...

    struct mysql_ss *ss = mysql_ss_create(opts->mysql_server_ports, opts->port_sz);
    assert(ss);

    struct tcpsniff_opt sniffopt = {<!-- -->
        .snaplen = 65535,
        .pkt_cnt_limit = 0,
        .timeout_limit = 10,
        .device = opts->interface,
        .filter_exp = opts->expression,
        .ud = ss};

...

    mysql_ss_release(ss);
...
}

If you need the complete source code, please add the WeChat number (c17865354792)

operation result:




Summary

The MySQL protocol is the protocol used by the MySQL database management system when communicating between clients and servers. It specifies how the client sends requests to the server, and how the server returns responses to the client. The MySQL protocol is a text-based protocol that uses ASCII codes for communication.

The MySQL protocol consists of multiple commands, each of which has specific format and syntax rules. These include commands such as connection, query, insert, update, and delete, which are used to implement various operations on the database.

In the MySQL protocol, each command is transmitted in the form of a “packet”. A package consists of multiple fields, including command type, sequence number, parameters, etc. The client sends a command package, and after receiving it, the server parses out the command type and parameters, then performs the corresponding operation and returns a response.

Welcome to follow WeChat official account【Programmer Coding

Reference: “MySQL Internals Manual: MySQL Client/Server Protocol”