This is a php version of linux's mysqldump in terminal "$ mysqldump -u username -p..."

Installs: 944

Dependents: 1

Stars: 262

Watchers: 27

Forks: 74

Open Issues: 3

Language: PHP

v2.0 2015-09-10 19:58 UTC


Requirements | Installing | Getting started | API | Settings | PDO Settings | TODO | License | Credits

Build Status Scrutinizer Quality Score Latest Stable Version

This is a php version of linux's mysqldump in terminal "$ mysqldump -u username -p...", without dependencies, output compression and sane defaults.

Out of the box, MySQLDump-PHP supports backing up table structures, the data itself, views and triggers.

MySQLDump-PHP is the only library that supports:

  • output binary blobs as hex.
  • resolves view dependencies (using Stand-In tables).
  • output compared against original mysqldump. Linked to travis-ci testing system.


From version 2.0, connections to database are made using the standard DSN, documented in PDO connection string


  • PHP 5.3.0 or newer
  • MySQL 4.1.0 or newer
  • PDO


Using Composer:

$ composer require ifsnop/mysqldump-php:2.*

Or via json file:

"require": {

Using Curl:

$ curl --silent --location | tar xvfz -

Getting started

With Autoloader/Composer:


use Ifsnop\Mysqldump as IMysqldump;

try {
    $dump = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
} catch (\Exception $e) {
    echo 'mysqldump-php error: ' . $e->getMessage();


Plain old PHP:


    include_once(dirname(__FILE__) . '/mysqldump-php-2.0.0/src/Ifsnop/Mysqldump/Mysqldump.php');
    $dump = new Ifsnop\Mysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');


Refer to the wiki for some examples and a comparision between mysqldump and mysqldump-php dumps.

Constructor and default parameters

 * Constructor of Mysqldump. Note that in the case of an SQLite database
 * connection, the filename must be in the $db parameter.
 * @param string $dsn        PDO DSN connection string
 * @param string $user       SQL account username
 * @param string $pass       SQL account password
 * @param array  $dumpSettings SQL database settings
 * @param array  $pdoSettings  PDO configured attributes
public function __construct(
    $dsn = '',
    $user = '',
    $pass = '',
    $dumpSettings = array(),
    $pdoSettings = array()

$dumpSettingsDefault = array(
    'include-tables' => array(),
    'exclude-tables' => array(),
    'compress' => 'None',
    'no-data' => false,
    'add-drop-table' => false,
    'single-transaction' => true,
    'lock-tables' => false,
    'add-locks' => true,
    'extended-insert' => true,
    'disable-keys' => true,
    'where' => '',
    'no-create-info' => false,
    'skip-triggers' => false,
    'add-drop-trigger' => true,
    'hex-blob' => true,
    'databases' => false,
    'add-drop-database' => false,
    'skip-tz-utc' => false,
    'no-autocommit' => true,
    'default-character-set' => 'utf8',
    'skip-comments' => false,
    'skip-dump-date' => false,

$pdoSettingsDefaults = array(

// missing settings in constructor will be replaced by default options
$this->_pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings);
$this->_dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);

Dump Settings

The following options are now enabled by default, and there is no way to disable them since they should always be used.

PDO Settings


To dump a database, you need the following privileges :

    • In order to dump table structures and data.
    • If any databases has views, else you will get an error.
    • If any table has one or more triggers.
    • If "lock tables" option was enabled.

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?


Current code for testing is an ugly hack. Probably there are much better ways of doing them using PHPUnit, so PR's are welcomed. 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.




Format all code to PHP-FIG standards.


This project is open-sourced software licensed under the GPL license


Originally based on James Elliott's script from 2009.

Adapted and extended by Michael J. Calkins.

Currently maintained and developed by Diego Torres.