MySQL Interactive protocol is a common protocol for developing MySQL peripheral components, such as jdbc,libmysql and so on.
Here we want to realize that MySQL Interactive protocol is actually half-duplex interactive protocol, as for why, here first dig a small pit, and then fill in.
Before we explore the format of the MySQL interactive protocol, we need to clarify a small problem, which is the basic data type that the MySQL interaction protocol will use.
1. Integer type
The integer types here can be used to represent integer types in MySQL data types, datetime types, DOUBLE, float types, bit types, enumeration types, and so on.
All MySQL integer types are small-ended, which is the Convention for network data. That is, for the 16 binary number 0x12345678, it becomes 0x78563412 when stored
Then the integer data Type here is:int<len> (where len = 1,2,3,4,6,8) and a variable integer type int<lenenc>
(1) int<len> is a fixed length of Len byte integer data, such as int<3> represents the number 3, will be expressed as a small end 0x030000.
(2) int<lenenc> is an integer of variable-length type, the length of which can be 1, 3, 4, 9.
The number we're setting to represent is N.
1) when n < 251,,int<lenenc> will be expressed as int<1>.
2) when 251 <= N < 2^16,int<lenenc> will be expressed as 0XFC + int<2>.
3) when 2^16 <= N < 2^24,int<lenenc> will be expressed as 0xfd + int<3>.
4) when 2^24 <= N < 2^64,int<lenenc> will be expressed as 0xFE + int<8>.
This means that if we encounter a int<lenenc> type, we need to read the first value type to determine
1) when type < 0XFB, the number of reads is considered to be type.
2) when type = = 0XFC, reading the back int<2>,int<2> is the real value.
3) when type = = 0xfd, reading the back int<3>,int<3> is the real value.
4) when type = = 0xFE, reading the back int<8>,int<8> is the real value.
So what is the type 0xFF and 0XFB? Here, dig the pit and fill it later.
What can be a spoiler:
0xFF generally represents an action error in the MySQL interaction protocol,
This response occurs when an insert command fails to be sent.
And 0XFB is null, that is, the MySQL insertion value is often null, which means null
2. String type
The string types here can be used to represent MySQL's high precision decimal type, string type, large bit type, and large integer type, among others.
The string type here is mainly
(1) string<len> is a string of len length.
(2) string<nul> is string+ ' 0x00 ', where ' 0x00 ' is the end character.
(3) string<lenenc> is the length of the string that int<lenenc> +string,int<lenenc> represents.
(4) String<eof> is the string that appears at the end of a MySQL interaction pack. In other words, this string is the last string of a MySQL interaction package.
(5) String<fix> and string<var> are almost impossible to use.
3.mysql Package Basic Format
MySQL Packet length int<3> |
MySQL Package serial number int<1> |
MySQL Package content string<len> |
MySQL package length is the length of the contents of the MySQL package, do not put the packet length and package header packet length in the MySQL package, generally int<3>, if the length of the 2^24-1,mysql will be sub-package, such as 2^24-1, divided into the following two packets.
0xFFFFFFF |
0x00 |
MySQL Package content |
0x000000 |
0x01 |
MySQL Package content |
MySQL Package serial number is a complete process of the package sequence number, that is, the first packet issued by the process starts from 0x00, later regardless of the reply package or package will be added 1, until the package sequence number reaches 0xFF, and then start counting from 0x001.
For example, I want to query the statement "SELECT * from a table", then I will send the first containing the query statement "SELECT * from a table" MySQL package to the MySQL database, this package is the serial number 0x00, then, the process began, Subsequent MySQL database replies will be counted from 0x01 until the end of the reply, when the process is over. I want to query the new statement to start with the new process, and the MySQL package ordinal count starts again from 0x00.
Reference URL Http://dev.mysql.com/doc/internals/en/describing-packets.html#type-string.var_len
Http://dev.mysql.com/doc/internals/en/mysql-packet.html
MySQL Interactive protocol parsing--mysql package base data, MySQL package basic format