Search by

owner888 / php-mysql

owner888

Pure-PHP asynchronous MySQL client (caching_sha2_password, prepared statements, transactions, Fiber connection pool), powered by owner888/php-async.

Package info

github.com/owner888/php-mysql

pkg:composer/owner888/php-mysql

Statistics

Installs: 28

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.2.0 2026-11-30 21:50 UTC

This package is auto-updated.

Last update: 2026-11-30 21:51:19 UTC


README

Pure-PHP 8.3+ asynchronous MySQL client powered by owner888/php-async.

  • Non-blocking TCP and STARTTLS over Fibers — no ext-pdo_mysql or ext-mysqli required.
  • Supports MySQL 8.x / 9.x caching_sha2_password (fast auth, TLS, and RSA-OAEP full auth) as well as mysql_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 on ER_NEED_REPREPARE (1615).
  • Row-by-row streaming via stream() (Generator, O(1) memory) with automatic cleanup on early break.
  • Fiber-safe connection pool with backpressure (Deferred waiter 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 via owner888/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