druidfi/mysqldump-php
Composer 安装命令:
composer require druidfi/mysqldump-php
包简介
PHP version of mysqldump cli that comes with MySQL
README 文档
README
This is a PHP version of mysqldump cli that comes with MySQL. It can be used for interacting with the data before
creating the database dump. E.g. it can modify the contents of tables and is thus good for anonymize data.
Out of the box, mysqldump-php supports backing up table structures, the data itself, views, triggers and events.
mysqldump-php supports:
- output binary blobs as hex
- resolves view dependencies (using Stand-In tables)
- output compared against original mysqldump
- dumps stored routines (functions and procedures)
- dumps events
- does extended-insert and/or complete-insert
- supports virtual columns from MySQL 5.7
- does insert-ignore, like a REPLACE but ignoring errors if a duplicate key exists
- modifying data from database on-the-fly when dumping, using hooks
- can save directly to Google Cloud storage over a compressed stream wrapper (GZIPSTREAM)
Requirements
- PHP 8.1 or newer with PDO - see supported versions
- MySQL 8.0 or newer (and compatible MariaDB)
Installing
Install using Composer:
composer require druidfi/mysqldump-php
Getting started
<?php try { $dump = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); $dump->start('storage/work/dump.sql'); } catch (\Exception $e) { echo 'mysqldump-php error: ' . $e->getMessage(); }
Refer to the ifsnop/mysqldump-php Wiki for some examples and a comparison between mysqldump and mysqldump-php dumps.
Changing values when exporting
You can register a callable that will be used to transform values during the export. An example use-case for this is removing sensitive data from database dumps:
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); $dumper->setTransformTableRowHook(function ($tableName, array $row) { if ($tableName === 'customers') { $row['social_security_number'] = (string) rand(1000000, 9999999); } return $row; }); $dumper->start('storage/work/dump.sql');
Getting information about the dump
You can register a callable that will be used to report on the progress of the dump
$dumper->setInfoHook(function($object, $info) { if ($object === 'table') { echo $info['name'], $info['rowCount']; });
Table specific export conditions
You can register table specific 'where' clauses to limit data on a per table basis. These override the default where
dump setting:
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); $dumper->setTableWheres([ 'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND deleted=0', 'logs' => 'date_logged > NOW() - INTERVAL 1 DAY', 'posts' => 'isLive=1' ]);
Table specific export limits
You can register table specific 'limits' to limit the returned rows on a per table basis:
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); $dumper->setTableLimits([ 'users' => 300, 'logs' => 50, 'posts' => 10 ]);
You can also specify the limits as an array where the first value is the number of rows and the second is the offset
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); $dumper->setTableLimits([ 'users' => [20, 10], //MySql query equivalent "... LIMIT 20 OFFSET 10" ]);
Dump Settings
Dump settings can be changed from default values with 4th argument for Mysqldump constructor:
$dumper = new \Druidfi\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password', $pdoOptions);
All options:
- include-tables
- Only include these tables (array of table names), include all if empty.
- exclude-tables
- Exclude these tables (array of table names), include all if empty, supports regexps.
- include-views
- Only include these views (array of view names), include all if empty. By default, all views named as the include-tables array are included.
- if-not-exists
- Only create a new table when a table of the same name does not already exist. No error message is thrown if the table already exists.
- compress
- Possible values:
Bzip2|Gzip|Gzipstream|Zstd|Lz4|None, default isNone - Could be specified using the consts:
CompressManagerFactory::GZIP,CompressManagerFactory::BZIP2,CompressManagerFactory::ZSTD,CompressManagerFactory::LZ4orCompressManagerFactory::NONE
- Possible values:
- compress-level
- Compression level to use (integer), default is
0(no compression level specified) - For Zstd: 1-22 (default: 3)
- For Lz4: 1-12 (default: 1)
- Compression level to use (integer), default is
- reset-auto-increment
- Removes the AUTO_INCREMENT option from the database definition
- Useful when used with no-data, so when db is recreated, it will start from 1 instead of using an old value
- add-drop-database
- MySQL docs 5.7
- add-drop-table
- MySQL docs 5.7
- add-drop-triggers
- MySQL docs 5.7
- add-locks
- MySQL docs 5.7
- complete-insert
- MySQL docs 5.7
- databases
- MySQL docs 5.7
- default-character-set
- Possible values:
utf8|utf8mb4, default isutf8 utf8is compatible option andutf8mb4is for full utf8 compliance- Could be specified using the consts:
DumpSettings::UTF8orDumpSettings::UTF8MB4 - MySQL docs 5.7
- Possible values:
- disable-keys
- MySQL docs 5.7
- events
- MySQL docs 5.7
- extended-insert
- MySQL docs 5.7
- hex-blob
- MySQL docs 5.7
- insert-ignore
- MySQL docs 5.7
- replace
- Use REPLACE INTO instead of INSERT INTO statements. Cannot be used together with insert-ignore.
- lock-tables
- MySQL docs 5.7
- net_buffer_length
- MySQL docs 5.7
- no-autocommit
- Option to disable autocommit (faster inserts, no problems with index keys)
- MySQL docs 5.7
- no-create-info
- MySQL docs 5.7
- no-data
- Do not dump data for these tables (array of table names), support regexps,
trueto ignore all tables - MySQL docs 5.7
- Do not dump data for these tables (array of table names), support regexps,
- routines
- MySQL docs 5.7
- single-transaction
- MySQL docs 5.7
- skip-comments
- MySQL docs 5.7
- skip-dump-date
- MySQL docs 5.7
- skip-triggers
- MySQL docs 5.7
- skip-tz-utc
- MySQL docs 5.7
- skip-definer
- MySQL docs 5.7
- where
- MySQL docs 5.7
The following options are now enabled by default, and there is no way to disable them since they should always be used.
- disable-foreign-keys-check
- MySQL docs 5.7
Privileges
To dump a database, you need the following privileges:
- SELECT
- In order to dump table structures and data.
- SHOW VIEW
- If any databases has views, else you will get an error.
- TRIGGER
- If any table has one or more triggers.
- LOCK TABLES
- If "lock tables" option was enabled.
- PROCESS
- If you don’t use the --no-tablespaces option.
Use SHOW GRANTS FOR user@host; to know what privileges user has. See the following link for more information:
- Which are the minimum privileges required to get a backup of a MySQL database schema?
- PROCESS privilege from MySQL 5.7.31 and MySQL 8.0.21 in July 2020
Tests
The testing script creates and populates a database using all possible datatypes. Then it exports it using both mysqldump-php and mysqldump, and compares the output. Only if it is identical tests are OK.
Some tests are skipped if mysql server doesn't support them.
A couple of tests are only comparing between original sql code and mysqldump-php generated sql, because some options are not available in mysqldump.
Local setup for tests:
composer install docker compose up --wait --build docker compose exec -w /app/tests/scripts php81 ./test.sh mysql docker compose exec -w /app/tests/scripts php82 ./test.sh mysql docker compose exec -w /app/tests/scripts php83 ./test.sh mysql docker compose exec -w /app/tests/scripts php84 ./test.sh mysql docker compose exec -w /app/tests/scripts php85 ./test.sh mysql docker compose exec -w /app/tests/scripts php81 ./test.sh mariadb docker compose exec -w /app/tests/scripts php82 ./test.sh mariadb docker compose exec -w /app/tests/scripts php83 ./test.sh mariadb docker compose exec -w /app/tests/scripts php84 ./test.sh mariadb docker compose exec -w /app/tests/scripts php85 ./test.sh mariadb
Credits
Forked from Diego Torres's version which have latest updates from 2020. Use it for PHP 8.1 and older. https://github.com/ifsnop/mysqldump-php
Originally based on James Elliott's script from 2009. https://code.google.com/archive/p/db-mysqldump/
Adapted and extended by Michael J. Calkins. https://github.com/clouddueling
License
This project is open-sourced software licensed under the GPL license
druidfi/mysqldump-php 适用场景与选型建议
druidfi/mysqldump-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 590.26k 次下载、GitHub Stars 达 35, 最近一次更新时间为 2022 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「sql」 「mysql」 「pdo」 「mariadb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 druidfi/mysqldump-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 druidfi/mysqldump-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 druidfi/mysqldump-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
A PSR-7 compatible library for making CRUD API endpoints
Database/ORM-agnostic query construction helper
统计信息
- 总下载量: 590.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 35
- 点击次数: 24
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2022-08-18