1. 服務器響應報文(服務器 -> 客戶端)
當客戶端發起認證請求或命令請求後,服務器會返回相應的執行結果給客戶端。客戶端在收到響應報文後,需要首先檢查第1個字節的值,來區分響應報文的類型。
響應報文類型 | 第1個字節取值範圍 |
---|---|
OK 響應報文 | 0x00 |
Error 響應報文 | 0xFF |
EOF 報文 | 0xFE |
Result Set 報文 | 0x01 - 0xFA |
Field 報文 | 0x01 - 0xFA |
Row Data 報文 | 0x01 - 0xFA |
1.1 ok響應報文
ok響應報文是對下面命令的響應:
- COM_PING
- COM_QUERY,條件是查詢不需要結果返回設置;譬如:insert,update或者alter table
- COM_REFRESH
- COM_REGISTER_SLAVE
這一類型的包適用於不要求返回結果集的命令。
1.1.1 響應包(除去包頭 packet header4個字節)格式:
字節數量 | 說明 |
---|---|
1 | OK報文,值恆爲0x00 |
int < lenenc> | 長度編碼(length encoded)的整形值,1-9個字節; affected rows 影響行數 |
int < lenenc> | last_insert_id |
int <2> | Status Flags |
int<2> | number of warnings |
1.1.2 舉例譬如,請求命令(客戶端->服務器):
use test ;//切換數據庫,對應命令類型爲 COM_INIT_DB
01:18:48.909981 IP (tos 0x8, ttl 64, id 56656, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->5f60)!)
localhost.52284 > localhost.mysql: Flags [P.], cksum 0xfe31 (incorrect -> 0x3146), seq 350:359, ack 3667, win 12623, options [nop,nop,TS val 1034407982 ecr 1034407982], length 9
0x0000: 4508 003d dd50 4000 4006 0000 7f00 0001 E..=.P@.@.......
0x0010: 7f00 0001 cc3c 0cea 00a0 a735 2265 761f .....<.....5"ev.
0x0020: 8018 314f fe31 0000 0101 080a 3da7 d02e ..1O.1......=...
0x0030: 3da7 d02e 0500 0000 0274 6573 74 =........test
^^----- 02標示COM_INIT_DB命令類型
^^------00標示sequence id是0
^^^^ ^^-----0x05 00 00標示mysql packet payload長度是5
^^^^ ^^^^ ^^---------5個字節的payload
response ip報文
01:18:48.910082 IP (tos 0x0, ttl 64, id 40877, offset 0, flags [DF], proto TCP (6), length 63, bad cksum 0 (->9d09)!)
localhost.mysql > localhost.52284: Flags [P.], cksum 0xfe33 (incorrect -> 0x0aac), seq 3667:3678, ack 359, win 12739, options [nop,nop,TS val 1034407982 ecr 1034407982], length 11
0x0000: 4500 003f 9fad 4000 4006 0000 7f00 0001 E..?..@.@.......
0x0010: 7f00 0001 0cea cc3c 2265 761f 00a0 a73e .......<"ev....>
0x0020: 8018 31c3 fe33 0000 0101 080a 3da7 d02e ..1..3......=...
0x0030: 3da7 d02e 0700 0001 0000 0002 0000 00 =..............
^^ ^^ 2個字節的warning numbers, 此處0 warning
^^ ^^2個字節status flag,0x02 00,標示autocommit = ON
^^last inserted id插入自增序列值,此處標示無
^^-----affected rows = 0x00,0行受影響
^^------ ok報文第一個字節是0x00
^^---- sequence id,響應報文的seq id通常從1開始
^^^^ ^^-----------0x07 00 00標示payload length = 7
ref: https://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
1.2 Error報文
1.3 EOF報文
1.4 Result Set 報文
result set報文比較複雜。上面講的ok/error/eof報文只有一個packet。而 result set報文是由多個packet按照特定順序組合而成。
COM_QUERY_Response: 按照類型可以分爲4種
- a ERR_Packet
- a OK_Packet
- a Protocol::LOCAL_INFILE_Request
- a ProtocolText::Resultset
其中,ProtocolText::Resultset格式定義如下:
- A packet containing a Protocol::LengthEncodedInteger column_count
- column_count * Protocol::ColumnDefinition packets
- If the CLIENT_DEPRECATE_EOF client capability flag is not set, EOF_Packet
- One or more ProtocolText::ResultsetRow packets, each containing column_count values
- ERR_Packet in case of error. Otherwise: If the CLIENT_DEPRECATE_EOF client capability flag is set, OK_Packet; else EOF_Packet.
If the SERVER_MORE_RESULTS_EXISTS flag is set in the last EOF_Packet or (if the CLIENT_DEPRECATE_EOF capability flag is set) OK_Packet, another ProtocolText::Resultset will follow (see Multi-resultset).
詳情請見:
https://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition
舉例:https://dev.mysql.com/doc/internals/en/protocoltext-resultset.html
2. 參考
1,http://hutaow.com/blog/2013/11/06/mysql-protocol-analysis/
2,https://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition