Master MySQL Binary Packet Handling in PHP with workbunny/mysql-protocol
This guide introduces the workbunny/mysql-protocol PHP library, outlines its PHP ≥ 8.1 and optional Workerman dependencies, shows how to install it via Composer, and demonstrates using its Binary and Packet utilities for reading, writing, and manipulating MySQL protocol binary streams.
Overview
The workbunny/mysql-protocol library is a PHP implementation of the MySQL protocol, offering functions to process MySQL protocol packets and enabling the development of clients, proxies, or servers that interact with MySQL.
Dependencies
PHP ≥ 8.1
Optional: workerman ≥ 4.0 (for workerman environments)
Installation
composer require workbunny/mysql-protocolUsage – Binary Stream
The Binary class provides conversion between binary streams and byte arrays, basic read/write operations with independent pointers, and payload handling for strings, byte arrays, iterables, or null.
use Workbunny\MysqlProtocol\Utils\Binary;
$binary = new Binary("workbunny");
// Output byte array
$binary->unpack();
// Output string (returns plaintext if input is plaintext, otherwise binary)
$binary->pack();
// Get raw payload
$binary->payload();Read Operations
Read pointer starts at 0 and advances with each operation.
use Workbunny\MysqlProtocol\Utils\Binary;
$binary = new Binary("workbunny");
$binary->setReadCursor();
$binary->getReadCursor();
$binary->readByte();
$binary->readBytes();
$binary->readLenEncInt();
$binary->readLenEncString();
$binary->readUB();
$binary->readNullTerminated();Write Operations
Write pointer also starts at 0 and advances with each write.
use Workbunny\MysqlProtocol\Utils\Binary;
$binary = new Binary();
$binary->setWriteCursor();
$binary->getWriteCursor();
$binary->writeByte();
$binary->writeBytes();
$binary->writeLenEncInt();
$binary->writeLenEncString();
$binary->writeUB();
$binary->writeNullTerminated();Packet Handling
The Packet class parses and assembles MySQL binary packet data.
It implements PacketInterface for custom extensions.
By default it supports 13 common MySQL interaction types.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
