0 - Packet

You are viewing an old version of this article. View the current version here.

Client - server exchanges are done using the following format :

Standard packet

Standard MySQL/MariaDB Packet have a 4 bytes header + packet body.

Packet length is the length of the packet body. Packet length size cannot be more than 3 bytes length value ( = 16777215). Sequence number indicate exchange number when an exchange demand different exchanges. first seqence is 0.

Example : Sending a COM_PING packet COM_PING body has only one byte (0x10):

01 00 00 00 10

Server will then return an OK_Packet response with a sequence number of 1.

Packet splitting

Packet length is 3 bytes making a maximum size of 16Mbytes. Data may be greater than that. For those cases, Client can send many packet for the same data, incrementing sequence number for each packet.

The principe is to split data by chunk of 16MBytes. When server receive a packet with 0xffffff length, it will continue to read next packet. (in case of length exactly 16MBytes, an empty packet must terminate the sequence).


Example max_allowed_packet is set to a value > to 40 Mbytes Sending a 40M bytes packet body : standard_packet
First packet will be :

ff ff ff 00 ...

second packet will be

ff ff ff 01 ...

third packet will be

02 00 80 02 ...


Client must be aware of max_allowed_packet variable value : Server will have a buffer to store the body with a maximum size corresponding to this max_allowed_packet value. If client send more data than max_allowed_packet size, socket will be close.



Compressed packet

For slow connection, packet can be compressed. this is activate after the handshake-response-packet when Client indicate [[https://mariadb.com/kb/en/mariadb/1-connecting-connecting/#capabilities|COMPRESS] capability with server having this functionnality too.

When activated, packets will be composed of 7 bytes a compress header + data, data being the compressed or uncompressed value of one or many standard packets.

  • int<3> compress packet length
  • int<1> compress sequence number
  • int<3> uncompress packet length
  • byte<n> compress body
    • compress body contain one or many standard packet but can be compressed :

Since compress body can contain many "standard packet", compress sequence number is incremented separatly from sequence number.

For small packet, using compression won't be efficient, so client can choose to send uncompress data.
That is done by setting compress packet length to real length and uncompress packet length to 0. (Data must then be uncompressed).

Example : Sending a COM_PING packet COM_PING body when COMPRESS is enable. This is a 1 byte data, that has then no reason to be compressed. so

01 00 00 00 00 00 00 01 00 00 00 10

Server will then return an OK_Packet response with a compress sequence number of 1, and a sequence number of 1.


Compression packet splitting

server will uncompress data and then must have the same packet than if there was no compression. If data size need splitting, better to separate compress packet.

compress_packet


Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.