定制 oihana/php-mysql 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oihana/php-mysql

Composer 安装命令:

composer require 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.

README 文档

README

Oihana PHP Mysql

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
Total Downloads
License

📚 Documentation

Full project documentation is available at:
👉 https://bcommebois.github.io/oihana-php-mysql

📦 Installation

Requires PHP 8.4+ and ext-pdo

Install via Composer:

composer require oihana/php-mysql

✨ 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).
  • EnumsMysqlParam, MysqlPrivileges for strongly-typed configuration keys and privilege names.

🚀 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

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

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

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

To run all tests:

composer run-script test

To run a specific test file:

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

🤝 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

See CHANGELOG.md for notable changes.

🧾 License

This project is licensed under the Mozilla Public License 2.0 (MPL-2.0).

👤 About the author

🛠️ Generate the Documentation

We use phpDocumentor to generate the documentation into the ./docs folder.

composer doc

🔗 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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MPL-2.0
  • 更新时间: 2026-05-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固