0 - Packet

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

After socket is established, exchanges are done using the following format :

Standard packet

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

  • [protocol-data-types/#fixed-length-integers|int<3>]] packet length
  • [protocol-data-types/#fixed-length-integers|int<1>]] sequence number
  • [protocol-data-types/#fixed-length-bytes|byte<n>]] 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

Server will have a buffer to store the send body with a maximum size corresponding to max_allowed_packet variable.

So if max_allowed_packet value < 16777215, packet length must not never be superior to max_allowed_paclet value. If not, server will close the connection.

If max_allowed_packet >= 16777215, then multiple packet can be send to send the data. Server will read packets until packet length != 0xffffff, data beeing added in server buffer.

Example max_allowed_packet is set to 32Mbytes Sending a 20 000 000 bytes packet body First packet will be :

ff ff ff 00 ...

second packet will be

01 45 49 01 ...

01 45 49 = 3 222 785 ( 20 000 000 - 16 777 215)

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.

  • [protocol-data-types/#fixed-length-integers|int<3>]] compress packet length
  • [protocol-data-types/#fixed-length-integers|int<1>]] compress sequence number
  • [protocol-data-types/#fixed-length-integers|int<3>]] uncompress packet length
  • [protocol-data-types/#fixed-length-bytes|byte<n>]] compress body
    • compress body contain one or many standard packet but can be compressed :
      • one or many Standard packet :
        • [protocol-data-types/#fixed-length-integers|int<3>]] packet length
        • [protocol-data-types/#fixed-length-integers|int<1>]] sequence number
        • [protocol-data-types/#fixed-length-bytes|byte<n>]] packet body

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 send compress or uncompress data. Client can indicate that packet is not compressed by setting "uncompress packet length" to 0. "compress packet length" with then be set with uncompressed packet length.

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

Example :

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.