MySQLDump - PHP

这是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

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

 

以下选项现在默认启用,并且无法禁用它们,因为它们应始终使用。

PDO设置

错误

要转储数据库,您需要以下权限:

  • 选择
    • 为了转储表结构和数据。
  • SHOW VIEW
    • 如果任何数据库都有视图,否则你会得到一个错误。
  • 触发
    • 如果任何表有一个或多个触发器。
  • 锁表
    • 如果“锁定表”选项已启用。

为用户主机使用SHOW GRANTS; 了解用户拥有的权限。请参阅以下链接了解更多信息:

获得MySQL数据库模式备份所需的最低特权是哪些?

测试

目前的测试代码是一个比较简单的。可能有更好的方法来使用PHPUnit来完成它们,所以PR是受欢迎的。测试脚本使用所有可能的数据类型创建并填充数据库。然后使用mysqldump-php和mysqldump将其导出,并比较输出。只有当它是相同的测试是可以的。

来源

官方github  :https://github.com/ifsnop/mysqldump-php

转载于:https://my.oschina.net/u/3338945/blog/1786273

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值