owner888 / php-mysql
Pure-PHP asynchronous MySQL client (caching_sha2_password, prepared statements, transactions, Fiber connection pool), powered by owner888/php-async.
v1.2.0
2026-11-30 21:50 UTC
Requires
- php: >=8.3
- owner888/php-async: ^1.0
Requires (Dev)
- phpunit/phpunit: ^11.0 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Pure-PHP 8.3+ asynchronous MySQL client powered by owner888/php-async.
- Non-blocking TCP and STARTTLS over Fibers — no
ext-pdo_mysqlorext-mysqlirequired. - Supports MySQL 8.x / 9.x
caching_sha2_password(fast auth, TLS, and RSA-OAEP full auth) as well asmysql_native_password. - Native binary prepared statements (
COM_STMT_PREPARE/COM_STMT_EXECUTE/COM_STMT_CLOSE) with per-connection LRU statement caching (statementCacheSize, default 64) and automatic re-prepare onER_NEED_REPREPARE(1615). - Row-by-row streaming via
stream()(Generator, O(1) memory) with automatic cleanup on earlybreak. - Fiber-safe connection pool with backpressure (
Deferredwaiter queue), idle expiry, and automatic discarding of dirty connections. - Atomic
transaction()helper bound to an exclusive connection with automatic commit/rollback. - Shares the host's
Scheduler(such as Revolt/Workerman) automatically viaowner888/php-async.
Installation
composer require owner888/php-mysql
Quick Start
<?php use Owner888\Async\Async; use Owner888\Mysql\MysqlClient; Async::run(static function (): void { $db = new MysqlClient('mysql://user:pass@127.0.0.1:3306/app?charset=utf8mb4&pool=16'); $res = $db->query('INSERT INTO users (name, score) VALUES (?, ?)', ['alice', 100]); echo 'Inserted ID: ', $res->lastInsertId, "\n"; $row = $db->fetchOne('SELECT id, name, score FROM users WHERE id = ?', [$res->lastInsertId]); var_dump($row); // Concurrent queries across Fibers run in parallel on pooled connections: $a = Async::spawn(static fn () => $db->fetchScalar('SELECT SLEEP(0.2), 1')); $b = Async::spawn(static fn () => $db->fetchScalar('SELECT SLEEP(0.2), 2')); $a->await(); $b->await(); // finishes in ~0.2s, not ~0.4s $db->close(); });
Transactions
$db->transaction(static function ($tx): void { $tx->query('UPDATE accounts SET balance = balance - ? WHERE id = ?', [50, 1]); $tx->query('UPDATE accounts SET balance = balance + ? WHERE id = ?', [50, 2]); });
Streaming Large Result Sets
foreach ($db->stream('SELECT id, body FROM messages WHERE mailbox_id = ?', [1]) as $row) { // O(1) memory — rows are decoded one packet at a time. }
Development
composer install
composer test
To run the live MySQL integration tests:
PHP_MYSQL_TEST_HOST=127.0.0.1 PHP_MYSQL_TEST_PORT=3306 \
PHP_MYSQL_TEST_USER=root PHP_MYSQL_TEST_PASS=your_password \
composer test
License
MIT