[mysql] packet

Address reprint: https://dev.mysql.com/doc/dev/mysql-server/8.0.0/page_protocol_basic_packets.html

A, packet meaning

packet is not the mysql procotol (agreement), but how accurate data protocol data transmission protocol and even how long split more underlying support.


Two, packet structure

| length(3-bytes) | sequence-id(1-byte) | payload(length-bytes) |

length: length in bytes of the payload

sequence-id: packet sequence number of each new mysql command resets to 0, the long protocol data, will be split into a plurality of Packet, are sequentially specified by the order of the sequence-id

payload: Actual procotol (protocol) data, is less than 16M, then the entire protocol payload is command data, or the data will be split into a plurality of packet procotol


Three, packet limit

1. A packet transmission maximum energy 16MB, i.e., the data protocol is less than 16MB, you may transmit a complete packet


2. If the packet is greater than 16MB, will split, the split will be as follows, i.e., a plurality of split except the last, will be before ff ff ff, until it encounters less than ff ff ff, and sequence-id, will The final assembly procotol protocol data as the basis for:

ff ff ff 00 .......

00 00 00 01 .....


Four, mysql server source code handler inlet

1. interested in looking at the code words, a complete packet of data reading and to a plurality of packet assembling function

[SQL / net_serv.cc]:  ulong my_net_read (the NET * NET), as shown below:





Examples of protocol V. Analysis of mysql


After finished reading all the Packet, and assembled into the final protocol protocol data, it can be read payload area of ​​the data relating to the internal protocol

protocol data structure:

| cmd(1-byte) | payload(length - 1 bytes) |

cmd: protocol commands the actual agreement, including:


payload: the data structure will in particular cmd


As to COM_INIT_DB (such as the use use DBName instructions):


Its cmd is 02, payload (dbname) as String [ EOF ]:

Possible complete Packet :

0500 00 00 02 74 65 73 74

length : 5

sequence-id: 0

cmd : 02(COM_INIT_DB)

db_name : 0x74 0x65 0x73 0x74

Reprinted related links:

https://dev.mysql.com/doc/internals/en/text-protocol.html

https://dev.mysql.com/doc/internals/en/com-init-db.html


Published 140 original articles · won praise 28 · views 180 000 +

Guess you like

Origin blog.csdn.net/qq_16097611/article/details/79849312