这是Mysql自带的mysqldump cli的php版本,没有依赖关系,输出压缩和正常默认值。
开箱即用,MySQLDump-PHP支持备份表结构,数据本身,视图,触发器和事件。
MySQLDump-PHP是唯一支持以下功能的库:
- 输出二进制blob为十六进制。
- 解决视图依赖关系(使用备用表)。
- 输出与原始mysqldump进行比较。链接到travis-ci测试系统(测试从php 5.3到7.1&hhvm)
- 转储存储过程。
- 转储事件。
- 扩展插入和/或完整插入。
- 支持来自MySQL 5.7的虚拟列。
-
要求
- PHP 5.3.0或更新版本
- MySQL 4.1.0或更新版本
- PDO
-
安装
使用Composer:
$ composer require ifsnop/mysqldump-php:2.*
或通过json文件:
"require": { "ifsnop/mysqldump-php":"2.*" }
使用Curl始终下载和解压缩最新版本:
$ curl --silent --location https://api.github.com/repos/ifsnop/mysqldump-php/releases | grep -i tarball_url | head -n
-
入门
使用Autoloader / Composer:
<?php use Ifsnop\Mysqldump as IMysqldump; try { $dump = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password'); $dump->start('storage/work/dump.sql'); } catch (\Exception $e) { echo 'mysqldump-php error: ' . $e->getMessage(); }
普通的旧PHP:
<?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'); $dump->start('storage/work/dump.sql');
请参阅wiki以获取一些示例以及mysqldump和mysqldump-php转储之间的比较。
-
构造函数和默认参数
-
/** * 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' => Mysqldump::NONE, 'init_commands' => array(), 'no-data' => array(), 'reset-auto-increment' => false, 'add-drop-database' => false, 'add-drop-table' => false, 'add-drop-trigger' => true, 'add-locks' => true, 'complete-insert' => false, 'databases' => false, 'default-character-set' => Mysqldump::UTF8, 'disable-keys' => true, 'extended-insert' => true, 'events' => false, 'hex-blob' => true, /* faster than escaped content */ 'net_buffer_length' => self::MAXLINESIZE, 'no-autocommit' => true, 'no-create-info' => false, 'lock-tables' => true, 'routines' => false, 'single-transaction' => true, 'skip-triggers' => false, 'skip-tz-utc' => false, 'skip-comments' => false, 'skip-dump-date' => false, 'skip-definer' => false, 'where' => '', /* deprecated */ 'disable-foreign-keys-check' => true ); $pdoSettingsDefaults = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false ); // 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
- 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
- compress
- Gzip, Bzip2, None.
- Could be specified using the declared consts: IMysqldump\Mysqldump::GZIP, IMysqldump\Mysqldump::BZIP2 or IMysqldump\Mysqldump::NONE
- 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
- add-drop-table
- add-drop-triggers
- add-locks
- complete-insert
- databases
- default-character-set
- utf8 (default, compatible option), utf8mb4 (for full utf8 compliance)
- Could be specified using the declared consts: IMysqldump\Mysqldump::UTF8 or IMysqldump\Mysqldump::UTF8MB4BZIP2
- http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
- https://mathiasbynens.be/notes/mysql-utf8mb4
- disable-keys
- events
- extended-insert
- hex-blob
- lock-tables
- net_buffer_length
- no-autocommit
- Option to disable autocommit (faster inserts, no problems with index keys)
- http://dev.mysql.com/doc/refman/4.1/en/commit.html
- no-create-info
- no-data
- http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_no-data
- Do not dump data for these tables (array of table names), support regexps,
true
to ignore all tables
- routines
- single-transaction
- skip-comments
- skip-dump-date
- skip-triggers
- skip-tz-utc
- skip-definer
- where
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
以下选项现在默认启用,并且无法禁用它们,因为它们应始终使用。
PDO设置
- PDO :: ATTR_PERSISTENT
- PDO :: ATTR_ERRMODE
- PDO :: MYSQL_ATTR_INIT_COMMAND
- PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY
错误
要转储数据库,您需要以下权限:
- 选择
- 为了转储表结构和数据。
- SHOW VIEW
- 如果任何数据库都有视图,否则你会得到一个错误。
- 触发
- 如果任何表有一个或多个触发器。
- 锁表
- 如果“锁定表”选项已启用。
为用户主机使用SHOW GRANTS; 了解用户拥有的权限。请参阅以下链接了解更多信息:
测试
目前的测试代码是一个比较简单的。可能有更好的方法来使用PHPUnit来完成它们,所以PR是受欢迎的。测试脚本使用所有可能的数据类型创建并填充数据库。然后使用mysqldump-php和mysqldump将其导出,并比较输出。只有当它是相同的测试是可以的。
来源
官方github :https://github.com/ifsnop/mysqldump-php