PHPackages                             oihana/php-mysql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. oihana/php-mysql

ActiveLibrary[Database &amp; ORM](/categories/database)

oihana/php-mysql
================

MySQL utilities for PHP 8.4+: DSN builder, robust PDO connection builder, and high-level admin model for managing databases, users, privileges and tables.

1.0.0(2w ago)01↓100%MPL-2.0PHPPHP &gt;=8.4CI passing

Since May 20Pushed 2w agoCompare

[ Source](https://github.com/BcommeBois/oihana-php-mysql)[ Packagist](https://packagist.org/packages/oihana/php-mysql)[ Docs](https://github.com/BcommeBois/oihana-php-mysql)[ RSS](/packages/oihana-php-mysql/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (9)Versions (2)Used By (0)

Oihana PHP - Mysql
==================

[](#oihana-php---mysql)

[![Oihana PHP Mysql](https://raw.githubusercontent.com/BcommeBois/oihana-php-mysql/main/assets/images/oihana-php-mysql-logo-inline-512x160.png)](https://raw.githubusercontent.com/BcommeBois/oihana-php-mysql/main/assets/images/oihana-php-mysql-logo-inline-512x160.png)

MySQL utilities for PHP 8.4+: DSN builder, robust PDO connection builder, and a high-level admin model for managing databases, users, privileges and tables.

[![Latest Version](https://camo.githubusercontent.com/053fbfa61f557ac3e2367d7d481028732becf61b58b92fce1d71283a50a45d04/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6968616e612f7068702d6d7973716c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oihana/php-mysql)
[![Total Downloads](https://camo.githubusercontent.com/c6802adb0f420129494a4fc592910b61a85f40b479df9228ee53190281eaed2d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f6968616e612f7068702d6d7973716c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oihana/php-mysql)
[![License](https://camo.githubusercontent.com/c08752c9124c28514302db770b079cf1e8285421f2265123ad212bf7630e0809/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f6968616e612f7068702d6d7973716c2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

📚 Documentation
---------------

[](#-documentation)

Full project documentation is available at:
👉

📦 Installation
--------------

[](#-installation)

> Requires [PHP 8.4+](https://php.net/releases/) and `ext-pdo`

Install via Composer:

```
composer require oihana/php-mysql
```

✨ Features
----------

[](#-features)

- **`MysqlDSN`** – build a MySQL DSN string from a structured configuration (host, port, dbname, charset, unix socket).
- **`MysqlPDOBuilder`** – create a configured `PDO` instance with sane defaults, optional validation, and easy override of options.
- **`MysqlModel`** – high-level administrative model on top of PDO to manage databases, users, privileges and tables.
- **Traits** – reusable building blocks (`MysqlDatabaseTrait`, `MysqlUserTrait`, `MysqlPrivilegeTrait`, `MysqlTableTrait`, `MysqlAssertionsTrait`, `MysqlRootTrait`).
- **Enums** – `MysqlParam`, `MysqlPrivileges` for strongly-typed configuration keys and privilege names.

🚀 Quick start
-------------

[](#-quick-start)

```
require __DIR__ . '/vendor/autoload.php';

use oihana\mysql\MysqlPDOBuilder;
use oihana\mysql\MysqlModel;

// Build a PDO connection
$pdo = (new MysqlPDOBuilder([
    'host'     => '127.0.0.1',
    'dbname'   => 'demo',
    'username' => 'root',
    'password' => 'secret',
]))();

// Use the admin model
$model = new MysqlModel();
$model->setPDO( $pdo );

if ( !$model->databaseExists('my_app') )
{
    $model->createDatabase('my_app');
}
```

🧰 Usage
-------

[](#-usage)

### Build a DSN

[](#build-a-dsn)

```
use oihana\mysql\MysqlDSN;

$dsn = new MysqlDSN([
    MysqlDSN::HOST        => '127.0.0.1',
    MysqlDSN::PORT        => 3306,
    MysqlDSN::DBNAME      => 'my_database',
    MysqlDSN::CHARSET     => 'utf8mb4',
    MysqlDSN::UNIX_SOCKET => '/tmp/mysql.sock',
]);

echo (string) $dsn;
// mysql:host=127.0.0.1;port=3306;dbname=my_database;charset=utf8mb4;unix_socket=/tmp/mysql.sock
```

### Build a PDO connection

[](#build-a-pdo-connection)

```
use oihana\mysql\MysqlPDOBuilder;

$pdo = (new MysqlPDOBuilder([
    'host'     => 'localhost',
    'dbname'   => 'test_db',
    'username' => 'user',
    'password' => 'secret',
    // 'validate'   => false, // disable validation if needed
    // 'skipDbName' => true,  // build DSN without dbname
]))();
```

### Administrative operations

[](#administrative-operations)

```
use oihana\mysql\MysqlModel;

$model = new MysqlModel();
$model->setPDO( $pdoAdmin ); // connect as root/admin

$model->createDatabase('my_app');
$model->createUser('myuser', 'localhost', 'securepass');
$model->grantPrivileges('myuser', 'localhost', 'my_app');
$model->flushPrivileges();

// Rename a user
$model->renameUser('myuser', 'localhost', 'user', 'localhost');

// Revoke privileges
$model->revokePrivileges('user', 'localhost', 'my_app');

// Export information
print_r( $model->toArray() );
```

✅ Running Unit Tests
--------------------

[](#-running-unit-tests)

To run all tests:

```
composer run-script test
```

To run a specific test file:

```
composer run test ./tests/oihana/mysql/MysqlDSNTest.php
```

🤝 Contributing
--------------

[](#-contributing)

Contributions are welcome! Please:

- Open an issue for discussion before large changes
- Write tests for new features and bug fixes
- Run the full test suite locally before submitting a PR

🗒️ Changelog
------------

[](#️-changelog)

See `CHANGELOG.md` for notable changes.

🧾 License
---------

[](#-license)

This project is licensed under the [Mozilla Public License 2.0 (MPL-2.0)](https://www.mozilla.org/en-US/MPL/2.0/).

👤 About the author
------------------

[](#-about-the-author)

- Author: Marc ALCARAZ (aka eKameleon)
- Mail:
- Website:

🛠️ Generate the Documentation
-----------------------------

[](#️-generate-the-documentation)

We use [phpDocumentor](https://phpdoc.org/) to generate the documentation into the `./docs` folder.

```
composer doc
```

🔗 Related packages
------------------

[](#-related-packages)

- `oihana/php-system` – standard set of PHP helpers and tools (PDO model, logging, init, ...): `https://github.com/BcommeBois/oihana-php-system`
- `oihana/php-core` – core helpers and utilities used by this library: `https://github.com/BcommeBois/oihana-php-core`
- `oihana/php-enums` – a collection of strongly-typed constant enumerations for PHP: `https://github.com/BcommeBois/oihana-php-enums`
- `oihana/php-reflect` – reflection and hydration utilities: `https://github.com/BcommeBois/oihana-php-reflect`

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance96

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

20d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d234506e51778dd3d1d61b6c2af6f294ff963651b4aa509b595d3d2a77b7d89b?d=identicon)[ooop](/maintainers/ooop)

---

Top Contributors

[![ekameleon](https://avatars.githubusercontent.com/u/749032?v=4)](https://github.com/ekameleon "ekameleon (5 commits)")

---

Tags

phpdatabasemysqlpdodsnoihana

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/oihana-php-mysql/health.svg)

```
[![Health](https://phpackages.com/badges/oihana-php-mysql/health.svg)](https://phpackages.com/packages/oihana-php-mysql)
```

###  Alternatives

[ifsnop/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k5.8M74](/packages/ifsnop-mysqldump-php)[clouddueling/mysqldump-php

PHP version of mysqldump cli that comes with MySQL

1.3k23.1k](/packages/clouddueling-mysqldump-php)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1815.7k12](/packages/popphp-pop-db)[riverside/php-orm

PHP ORM micro-library and query builder

121.3k](/packages/riverside-php-orm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
