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.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Master MySQL Binary Packet Handling in PHP with workbunny/mysql-protocol

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-protocol

Usage – 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentmysqlPHPprotocolBinary
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.