{"id":13519261,"url":"https://github.com/ifsnop/mysqldump-php","last_synced_at":"2025-05-14T03:07:56.386Z","repository":{"id":6220955,"uuid":"7452327","full_name":"ifsnop/mysqldump-php","owner":"ifsnop","description":"PHP version of mysqldump cli that comes with MySQL","archived":false,"fork":false,"pushed_at":"2024-09-20T02:03:31.000Z","size":464,"stargazers_count":1247,"open_issues_count":28,"forks_count":300,"subscribers_count":55,"default_branch":"master","last_synced_at":"2024-10-29T15:18:38.494Z","etag":null,"topics":["database","hhvm","linux","macos","mariadb","mysql","mysql-backup","mysqldump","pdo","php","php5","php7","sql","windows"],"latest_commit_sha":null,"homepage":"https://github.com/ifsnop/mysqldump-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ifsnop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-01-05T06:06:56.000Z","updated_at":"2024-10-28T11:52:04.000Z","dependencies_parsed_at":"2023-01-13T13:53:20.129Z","dependency_job_id":"29e26554-99ad-4c68-8ac6-e8f756626929","html_url":"https://github.com/ifsnop/mysqldump-php","commit_stats":{"total_commits":352,"total_committers":50,"mean_commits":7.04,"dds":0.3920454545454546,"last_synced_commit":"91408c95b54ac2ce10ae6f34f18fa52934cf1410"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifsnop%2Fmysqldump-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifsnop%2Fmysqldump-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifsnop%2Fmysqldump-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ifsnop%2Fmysqldump-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ifsnop","download_url":"https://codeload.github.com/ifsnop/mysqldump-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247941705,"owners_count":21022039,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","hhvm","linux","macos","mariadb","mysql","mysql-backup","mysqldump","pdo","php","php5","php7","sql","windows"],"created_at":"2024-08-01T05:01:56.517Z","updated_at":"2025-04-08T23:18:06.604Z","avatar_url":"https://github.com/ifsnop.png","language":"PHP","readme":"MySQLDump - PHP\n=========\n\n[Requirements](https://github.com/ifsnop/mysqldump-php#requirements) |\n[Installing](https://github.com/ifsnop/mysqldump-php#installing) |\n[Getting started](https://github.com/ifsnop/mysqldump-php#getting-started) |\n[API](https://github.com/ifsnop/mysqldump-php#constructor-and-default-parameters) |\n[Settings](https://github.com/ifsnop/mysqldump-php#dump-settings) |\n[PDO Settings](https://github.com/ifsnop/mysqldump-php#pdo-settings) |\n[TODO](https://github.com/ifsnop/mysqldump-php#todo) |\n[License](https://github.com/ifsnop/mysqldump-php#license) |\n[Credits](https://github.com/ifsnop/mysqldump-php#credits)\n\n[![Build Status](https://travis-ci.org/ifsnop/mysqldump-php.svg?branch=devel)](https://travis-ci.org/ifsnop/mysqldump-php)\n[![Total Downloads](https://poser.pugx.org/ifsnop/mysqldump-php/downloads)](https://packagist.org/packages/ifsnop/mysqldump-php)\n[![Monthly Downloads](https://poser.pugx.org/ifsnop/mysqldump-php/d/monthly)](https://packagist.org/packages/ifsnop/mysqldump-php)\n[![Daily Downloads](https://poser.pugx.org/ifsnop/mysqldump-php/d/daily)](https://packagist.org/packages/ifsnop/mysqldump-php)\n[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/ifsnop/mysqldump-php/badges/quality-score.png?s=d02891e196a3ca1298619032a538ce8ae8cafd2b)](https://scrutinizer-ci.com/g/ifsnop/mysqldump-php/)\n[![Latest Stable Version](https://poser.pugx.org/ifsnop/mysqldump-php/v/stable.png)](https://packagist.org/packages/ifsnop/mysqldump-php)\n\nThis is a php version of mysqldump cli that comes with MySQL, without dependencies, output compression and sane defaults.\n\nOut of the box, MySQLDump-PHP supports backing up table structures, the data itself, views, triggers and events.\n\nMySQLDump-PHP is the only library that supports:\n* output binary blobs as hex.\n* resolves view dependencies (using Stand-In tables).\n* output compared against original mysqldump. Linked to travis-ci testing system (testing from php 5.3 to 7.3 \u0026 hhvm)\n* dumps stored routines (functions and procedures).\n* dumps events.\n* does extended-insert and/or complete-insert.\n* supports virtual columns from MySQL 5.7.\n* does insert-ignore, like a REPLACE but ignoring errors if a duplicate key exists.\n* modifying data from database on-the-fly when dumping, using hooks.\n* can save directly to google cloud storage over a compressed stream wrapper (GZIPSTREAM).\n* can restore a dump from a file, when no mysql executable is available.\n\n## Important\n\nFrom version 2.0, connections to database are made using the standard DSN, documented in [PDO connection string](https://secure.php.net/manual/en/ref.pdo-mysql.connection.php).\n\n## Requirements\n\n- PHP 5.3.0 or newer\n- MySQL 4.1.0 or newer\n- [PDO](https://secure.php.net/pdo)\n\n## Installing\n\nUsing [Composer](https://getcomposer.org/):\n\n```\n$ composer require ifsnop/mysqldump-php\n\n```\n\nUsing [Curl](https://curl.haxx.se/) to always download and decompress the latest release:\n\n```\n$ curl --silent --location https://api.github.com/repos/ifsnop/mysqldump-php/releases | grep -i tarball_url | head -n 1 | cut -d '\"' -f 4 | xargs curl --location --silent | tar xvz\n```\n\n## Getting started\n\nWith [Autoloader](https://www.php-fig.org/psr/psr-4/)/[Composer](https://getcomposer.org):\n\n```php\n\u003c?php\n\nuse Ifsnop\\Mysqldump as IMysqldump;\n\ntry {\n    $dump = new IMysqldump\\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');\n    $dump-\u003estart('storage/work/dump.sql');\n} catch (\\Exception $e) {\n    echo 'mysqldump-php error: ' . $e-\u003egetMessage();\n}\n```\n\nPlain old PHP:\n\n```php\n\u003c?php\n\n    include_once(dirname(__FILE__) . '/mysqldump-php-2.0.0/src/Ifsnop/Mysqldump/Mysqldump.php');\n    $dump = new Ifsnop\\Mysqldump\\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');\n    $dump-\u003estart('storage/work/dump.sql');\n```\n\nRefer to the [wiki](https://github.com/ifsnop/mysqldump-php/wiki/Full-usage-example) for some examples and a comparision between mysqldump and mysqldump-php dumps.\n\n## Changing values when exporting\n\nYou 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:\n\n```php\n$dumper = new IMysqldump\\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');\n\n$dumper-\u003esetTransformTableRowHook(function ($tableName, array $row) {\n    if ($tableName === 'customers') {\n        $row['social_security_number'] = (string) rand(1000000, 9999999);\n    }\n\n    return $row;\n});\n\n$dumper-\u003estart('storage/work/dump.sql');\n```\n\n## Getting information about the dump\n\nYou can register a callable that will be used to report on the progress of the dump\n\n```php\n$dumper-\u003esetInfoHook(function($object, $info) {\n    if ($object === 'table') {\n        echo $info['name'], $info['rowCount'];\n    });\n```\n\n## Table specific export conditions\n\nYou can register table specific 'where' clauses to limit data on a per table basis.  These override the default `where` dump setting:\n\n```php\n$dumper = new IMysqldump\\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');\n\n$dumper-\u003esetTableWheres(array(\n    'users' =\u003e 'date_registered \u003e NOW() - INTERVAL 3 MONTH AND deleted=0',\n    'logs' =\u003e 'date_logged \u003e NOW() - INTERVAL 1 DAY',\n    'posts' =\u003e 'isLive=1'\n));\n```\n\n## Table specific export limits\n\nYou can register table specific 'limits' to limit the returned rows on a per table basis:\n\n```php\n$dumper = new IMysqldump\\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');\n\n$dumper-\u003esetTableLimits(array(\n    'users' =\u003e 300,\n    'logs' =\u003e 50,\n    'posts' =\u003e 10\n));\n```\n\n## Constructor and default parameters\n\n```php\n/**\n * Constructor of Mysqldump. Note that in the case of an SQLite database\n * connection, the filename must be in the $db parameter.\n *\n * @param string $dsn        PDO DSN connection string\n * @param string $user       SQL account username\n * @param string $pass       SQL account password\n * @param array  $dumpSettings SQL database settings\n * @param array  $pdoSettings  PDO configured attributes\n */\npublic function __construct(\n    $dsn = '',\n    $user = '',\n    $pass = '',\n    $dumpSettings = array(),\n    $pdoSettings = array()\n)\n\n$dumpSettingsDefault = array(\n    'include-tables' =\u003e array(),\n    'exclude-tables' =\u003e array(),\n    'compress' =\u003e Mysqldump::NONE,\n    'init_commands' =\u003e array(),\n    'no-data' =\u003e array(),\n    'if-not-exists' =\u003e false,\n    'reset-auto-increment' =\u003e false,\n    'add-drop-database' =\u003e false,\n    'add-drop-table' =\u003e false,\n    'add-drop-trigger' =\u003e true,\n    'add-locks' =\u003e true,\n    'complete-insert' =\u003e false,\n    'databases' =\u003e false,\n    'default-character-set' =\u003e Mysqldump::UTF8,\n    'disable-keys' =\u003e true,\n    'extended-insert' =\u003e true,\n    'events' =\u003e false,\n    'hex-blob' =\u003e true, /* faster than escaped content */\n    'insert-ignore' =\u003e false,\n    'net_buffer_length' =\u003e self::MAXLINESIZE,\n    'no-autocommit' =\u003e true,\n    'no-create-db' =\u003e false,\n    'no-create-info' =\u003e false,\n    'lock-tables' =\u003e true,\n    'routines' =\u003e false,\n    'single-transaction' =\u003e true,\n    'skip-triggers' =\u003e false,\n    'skip-tz-utc' =\u003e false,\n    'skip-comments' =\u003e false,\n    'skip-dump-date' =\u003e false,\n    'skip-definer' =\u003e false,\n    'where' =\u003e '',\n    /* deprecated */\n    'disable-foreign-keys-check' =\u003e true\n);\n\n$pdoSettingsDefaults = array(\n    PDO::ATTR_PERSISTENT =\u003e true,\n    PDO::ATTR_ERRMODE =\u003e PDO::ERRMODE_EXCEPTION,\n    PDO::MYSQL_ATTR_USE_BUFFERED_QUERY =\u003e false\n);\n\n// missing settings in constructor will be replaced by default options\n$this-\u003e_pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings);\n$this-\u003e_dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);\n```\n\n## Dump Settings\n\n- **include-tables**\n  - Only include these tables (array of table names), include all if empty.\n- **exclude-tables**\n  - Exclude these tables (array of table names), include all if empty, supports regexps.\n- **include-views**\n  - Only include these views (array of view names), include all if empty. By default, all views named as the include-tables array are included.\n- **if-not-exists**\n  - 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.\n- **compress**\n  - Gzip, Bzip2, None.\n  - Could be specified using the declared consts: IMysqldump\\Mysqldump::GZIP, IMysqldump\\Mysqldump::BZIP2 or IMysqldump\\Mysqldump::NONE\n- **reset-auto-increment**\n  - Removes the AUTO_INCREMENT option from the database definition\n  - Useful when used with no-data, so when db is recreated, it will start from 1 instead of using an old value\n- **add-drop-database**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-database\n- **add-drop-table**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-table\n- **add-drop-triggers**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-drop-trigger\n- **add-locks**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_add-locks\n- **complete-insert**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_complete-insert\n- **databases**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_databases\n- **default-character-set**\n  - utf8 (default, compatible option), utf8mb4 (for full utf8 compliance)\n  - Could be specified using the declared consts: IMysqldump\\Mysqldump::UTF8 or IMysqldump\\Mysqldump::UTF8MB4BZIP2\n  - https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html\n  - https://mathiasbynens.be/notes/mysql-utf8mb4\n- **disable-keys**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_disable-keys\n- **events**\n  - https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_events\n- **extended-insert**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_extended-insert\n- **hex-blob**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_hex-blob\n- **insert-ignore**\n  - https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_insert-ignore\n- **lock-tables**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_lock-tables\n- **net_buffer_length**\n  - https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_net_buffer_length\n- **no-autocommit**\n  - Option to disable autocommit (faster inserts, no problems with index keys)\n  - https://dev.mysql.com/doc/refman/4.1/en/commit.html\n- **no-create-db**\n  - Option to disable the dump of create database statements.\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-db\n- **no-create-info**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-create-info\n- **no-data**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-data\n  - Do not dump data for these tables (array of table names), support regexps, `true` to ignore all tables\n- **routines**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines\n- **single-transaction**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_single-transaction\n- **skip-comments**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_comments\n- **skip-dump-date**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_dump-date\n- **skip-triggers**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_triggers\n- **skip-tz-utc**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc\n- **skip-definer**\n  - https://dev.mysql.com/doc/refman/5.7/en/mysqlpump.html#option_mysqlpump_skip-definer\n- **where**\n  - https://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_where\n\nThe following options are now enabled by default, and there is no way to disable them since\nthey should always be used.\n\n- **disable-foreign-keys-check**\n  - https://dev.mysql.com/doc/refman/5.5/en/optimizing-innodb-bulk-data-loading.html\n\n## PDO Settings\n\n- **PDO::ATTR_PERSISTENT**\n- **PDO::ATTR_ERRMODE**\n- **PDO::MYSQL_ATTR_INIT_COMMAND**\n- **PDO::MYSQL_ATTR_USE_BUFFERED_QUERY**\n  - https://secure.php.net/manual/en/ref.pdo-mysql.php\n  - https://stackoverflow.com/questions/13728106/unexpectedly-hitting-php-memory-limit-with-a-single-pdo-query/13729745#13729745\n  - https://secure.php.net/manual/en/mysqlinfo.concepts.buffering.php\n\n## Errors\n\nTo dump a database, you need the following privileges :\n\n- **SELECT**\n  - In order to dump table structures and data.\n- **SHOW VIEW**\n  - If any databases has views, else you will get an error.\n- **TRIGGER**\n  - If any table has one or more triggers.\n- **LOCK TABLES**\n  - If \"lock tables\" option was enabled.\n\nUse **SHOW GRANTS FOR user@host;** to know what privileges user has. See the following link for more information:\n\n[Which are the minimum privileges required to get a backup of a MySQL database schema?](https://dba.stackexchange.com/questions/55546/which-are-the-minimum-privileges-required-to-get-a-backup-of-a-mysql-database-sc/55572#55572)\n\n## Tests\n\nCurrent code for testing is an ugly hack. Probably there are much better ways\nof doing them using PHPUnit, so PR's are welcomed. The testing script creates\nand populates a database using all possible datatypes. Then it exports it\nusing both mysqldump-php and mysqldump, and compares the output. Only if\nit is identical tests are OK. After [this](https://github.com/ifsnop/mysqldump-php/commit/8496fbb1b26dde404804bc8865ec32044da5b813)\ncommit, some test are performed using phpunit.\n\nSome tests are skipped if mysql server doesn't support them.\n\nA couple of tests are only comparing between original sql code and\nmysqldump-php generated sql, because some options are not available in\nmysqldump.\n\n## Bugs (from mysqldump, not from mysqldump-php)\n\nAfter [this](https://bugs.mysql.com/bug.php?id=80150) bug report, a new\none has been introduced. _binary is appended also when hex-blob option is\nused, if the value is empty.\n\n## Backporting\n\nmysqldump-php is not backwards compatible with php 5.2 because we it uses\nnamespaces. However, it could be trivially fixed if needed.\n\n## Todo\n\nWrite more tests, test with mariadb also.\n\n## Contributing\n\nFormat all code to PHP-FIG standards.\nhttps://www.php-fig.org/\n\n## License\n\nThis project is open-sourced software licensed under the [GPL license](https://www.gnu.org/copyleft/gpl.html)\n\n## Credits\n\nAfter more than 8 years, there is barely anything left from the original source code, but:\n\nOriginally based on James Elliott's script from 2009.\nhttps://code.google.com/archive/p/db-mysqldump/\n\nAdapted and extended by Michael J. Calkins.\nhttps://github.com/clouddueling\n\nCurrently maintained, developed and improved by Diego Torres.\nhttps://github.com/ifsnop\n","funding_links":[],"categories":["PHP","类库"],"sub_categories":["数据库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fifsnop%2Fmysqldump-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fifsnop%2Fmysqldump-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fifsnop%2Fmysqldump-php/lists"}