Mysql interaction protocol analysis-mysql package basic data, mysql package basic format, mysql Interaction
Mysql interaction protocol is a common protocol used to develop mysql peripheral components, such as JDBC and libmysql.
Here we need to realize that the mysql interaction protocol is actually a half-duplex interaction protocol. As for why, we need to dig a small hole and fill it out later.
Before discussing the format of the mysql interaction protocol, we need to clarify a small problem, that is, the basic data types used by the mysql interaction protocol.
1. Integer type
The integer type can be used to indicate the integer type, datetime type, DOUBLE type, FLOAT type, bit type, and enumeration type in mysql data type.
All mysql integer types are small-end, which is the Convention of network data. That is, if the hexadecimal number is 0x12345678, it will change to 0x78563412 during storage.
The integer data types here are: int <len> (len =,) and variable integer int <lenenc>
(1) int <len> is an integer data with a fixed length of len bytes. For example, if int <3> represents number 3, it is represented by a small end 0x030000.
(2) int <lenenc> is an integer of the variable length type. The length of this character can be 1, 3, 4, 9.
We set the value to be represented to n.
1) WHEN n <251, int <lenenc> is expressed as int <1>.
2) When 251 <= n <2 ^ 16, int <lenenc> is expressed as 0xfc + int <2>.
3) when 2 ^ 16 <= n <2 ^ 24, int <lenenc> is expressed as 0xfd + int <3>.
4) when 2 ^ 24 <= n <2 ^ 64, int <lenenc> is represented by 0xfe + int <8>.
This indicates that if we encounter the int <lenenc> type, We need to read the first value type to judge
1) When type <0xfb, the number of reads is considered to be type.
2) When type = 0xfc, the int <2> and int <2> are the actual values.
3) When type = 0xfd, the int <3> and int <3> are the actual values.
4) When type = 0xfe, the int <8> and int <8> are the actual values.
So what are the values of type 0xff and 0xfb? This is where the pitfalls will be entered later.
Which of the following statements can be spoiler:
0xff indicates an action error in the mysql interaction protocol,
If an insert command fails to be sent, such a reply will appear.
0xfb indicates NULL, which means that the mysql insert value is usually NULL and NULL.
2. string type
The string type can be used to indicate mysql's high-precision decimal type, string type, big bit type, and big Integer type.
Here the string type is mainly
(1) string <len> is a string with a length of len.
(2) string <NUL> is string + '0x00', where '0x00' is used as the end character.
(3) string <lenenc> is int <lenenc> + string, int <lenenc> represents the length of the string.
(4) string <EOF> is the string that appears at the end of a mysql interaction package. In other words, this string is the last string of a mysql interactive package.
(5) string <fix> and string <var> are rarely used.
3. Basic mysql package format
Mysql package length int <3> |
Mysql package No. int <1> |
Mysql Package content string <len> |
The length of the mysql package is the length of the mysql Package content. The packet header is not included in the length of the mysql package, which is generally int <3>, if the length exceeds 2 ^ 24-1, mysql will subcontract, such as 2 ^ 24-1, into the following two packages.
0 xfffffff |
0x00 |
Mysql Package content |
Zero x 000000 |
0x01 |
Mysql Package content |
The mysql Package number is the serial number of the complete process, that is, the first packet sent by the process starts from 0x00, in the future, no matter whether the packet is replied or the packet to be subcontracted, 1 will be added to the package until the package serial number reaches 0xff, and the count starts from 0x001.
For example, if I want to query the statement "select * from a table", I will send the first mysql package containing the query statement "select * from a table" to the mysql database, the number of this package is 0x00. At this time, the process starts and the packages replied to by the mysql database will start from 0x01 until the reply ends. At this time, the process ends. The new statement indicates that the new process starts, and the mysql package serial number count starts from 0x00 again.
Reference Web site http://dev.mysql.com/doc/internals/en/describing-packets.html#type-string.var_len
Http://dev.mysql.com/doc/internals/en/mysql-packet.html