wire / wire-format / mysql-protocol
MySQL's protocol is a packet-based TCP protocol. Each packet: 3-byte length (little-endian), 1-byte sequence number, then payload. The first packet from the server is the Handshake (protocol version, server version string, connection ID, auth plugin data, capabilities, status, character set). The client responds with a HandshakeResponse. Queries are sent as COM_QUERY packets (command byte 0x03 + SQL string).
aka: MySQL protocol (packet structure)
spec: MySQL Internals Manual
MySQL's protocol is a packet-based TCP protocol. Each packet: 3-byte length (little-endian), 1-byte sequence number, then payload. The first packet from the server is the Handshake (protocol version, server version string, connection ID, auth plugin data, capabilities, status, character set). The client responds with a HandshakeResponse. Queries are sent as COM_QUERY packets (command byte 0x03 + SQL string).
| field | size | meaning |
|---|---|---|
| Packet length | 3 bytes (little-endian) | Length of the payload (max 16MB; larger payloads use multiple packets). |
| Sequence number | 1 byte | Packet sequence number (increments per packet in a command; resets to 0 for new commands). |
| Payload | = length | Command byte + data, or result set, or error packet. |
| Handshake (server -> client) | variable | Protocol version (1) + server version (NUL-terminated) + connection ID (4) + auth plugin data part 1 (8) + filler (1) + capability flags (2/4) + charset (1) + status (2) + auth plugin data length (1) + reserved (10) + auth plugin data part 2 + auth plugin name (NUL-terminated). |
| COM_QUERY | variable | Command byte 0x03 + SQL string (not NUL-terminated; length is the packet length). |
| OK packet | variable | Header 0x00 + affected rows (varint) + last insert ID (varint) + status flags (2) + warnings (2) + info string. |
| ERR packet | variable | Header 0xFF + error code (2) + SQL state marker '#' + SQL state (5) + error message. |
| EOF packet | 5 bytes | Header 0xFE + warnings (2) + status flags (2). (Deprecated in MySQL 5.7+; replaced by OK packet with 0xFE header.) |
example:
13 00 00 00 03 53 45 4c 45 43 54 20 40 40 76 65 72 73 69 6f 6e
MySQL COM_QUERY packet: length 19, seq 0, command 0x03, SQL 'SELECT @@version'
agent: curl -H 'accept: application/json' wire.phall.io/wire-format/mysql-protocol
or /wire-format/mysql-protocol.json