当前位置: 首页 > 软件库 > 数据库相关 > >

mysqldump-php

PHP version of mysqldump cli that comes with MySQL
授权协议 GPL-3.0 License
开发语言 PHP
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 陆浩博
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

MySQLDump - PHP

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

This is a php version of mysqldump cli that comes with MySQL, without dependencies, output compression and sane defaults.

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

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 (testing from php 5.3 to 7.3 & hhvm)
  • 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).

Important

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

Requirements

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

Installing

Using Composer:

$ composer require ifsnop/mysqldump-php

Using Curl to always download and decompress the latest release:

$ 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

Getting started

With 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();
}

Plain old 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');

Refer to the wiki for some examples and a comparision 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 IMysqldump\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 IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');

$dumper->setTableWheres(array(
    '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 IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');

$dumper->setTableLimits(array(
    'users' => 300,
    'logs' => 50,
    'posts' => 10
));

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' => Mysqldump::NONE,
    'init_commands' => array(),
    'no-data' => array(),
    'if-not-exists' => false,
    '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 */
    'insert-ignore' => false,
    '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 sincethey should always be used.

PDO Settings

Errors

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.

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?

Tests

Current code for testing is an ugly hack. Probably there are much better waysof doing them using PHPUnit, so PR's are welcomed. The testing script createsand populates a database using all possible datatypes. Then it exports itusing both mysqldump-php and mysqldump, and compares the output. Only ifit is identical tests are OK. After thiscommit, some test are performed using phpunit.

Some tests are skipped if mysql server doesn't support them.

A couple of tests are only comparing between original sql code andmysqldump-php generated sql, because some options are not available inmysqldump.

Bugs (from mysqldump, not from mysqldump-php)

After this bug report, a newone has been introduced. _binary is appended also when hex-blob option isused, if the value is empty.

Backporting

mysqldump-php is not backwards compatible with php 5.2 because we it usesnamespaces. However, it could be trivially fixed if needed.

Todo

Write more tests, test with mariadb also.

Contributing

Format all code to PHP-FIG standards.https://www.php-fig.org/

License

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

Credits

After more than 8 years, there is barely anything left from the original source code, but:

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

Currently maintained, developed and improved by Diego Torres.https://github.com/ifsnop

  • mysqldump有一个参数--lock-tables,以前对这个参数也没有详细了解过,直到上次有个网友问“参数lock-tables 是一次性锁定当前库的所有表,还是锁定当前导出表?“ ,之前一直以为只是锁定当前导出表,后面看了参数说明后, -l, --lock-tables   Lock all tables for read.  (Defaults to on; use --skip-loc

  • 最近在用mysqldump备份时,想要把数据表和数据分开备份,因此做了2次备份。 执行备份数据库表结构时,指定了 skip-opt 选项,相当于: --add-drop-table, --add-locks,--create-options, --quick, --extended-insert,--lock-tables, --set-charset, and --di 最近在用mysqldum

  • MySQLdump是MySQL自带的导出数据工具,通常我们用它来导出MySQL中,但是有时候我们需要导出MySQL数据库中某个表的部分数据,这时该怎么办呢? mysqldump命令中带有一个 --where/-w 参数,它用来设定数据导出的条件,使用方式和SQL查询命令中中的where基本上相同,有了它,我们就可以从数据库中导出你需要的那部分数据了。 命令格式如下: mysqldump -u用户名

  • 本教程介绍如何使用 mysqldump 实用程序从命令行备份和还原 MySQL 或 MariaDB 数据库。 mysqldump 实用程序创建的备份文件基本上是一组可用于重新创建原始数据库的 SQL 语句。mysqldump 命令还可以生成 CSV 和 XML 格式的文件。 您还可以使用mysql转储实用程序将MySQL数据库传输到另一个MySQL服务器。 如果不备份数据库,软件错误或硬盘驱动器故

  • mysqldump bitsCN.com [MySQL] 用mysqldump制作文本备份 在使用mysqldump备份MySQL时,备份文件是SQL格式的文本文件,它由一系列create table和insert语句组成。恢复时,只要把该SQL文件作为mysql程序的输入即可,如下所示: [plain] mysqldump mydb mytbl > mytbl.sql mysql mydb <

  • 导出数据实例如下: $mdb_host = $g_c["db"][0]["managertool"]["host"];  //数据库ip地址 $mdb_user = $g_c["db"][0]["managertool"]["user"];  //数据库用户名,(默认:root) $mdb_pwd = $g_c["db"][0]["managertool"]["pwd"];  //数据库密码 $m

  • 用mysqldump远程备份mysql数据库 mysqldump是备份mysql数据库的一种好工具。它相对于用phpmyadmin等备份工具更加快速,又避免受php.ini等的限制,在windows系统下还可以结合计划任务实现定时远程备份数据库。 操作用mysqldump备份数据库的用户需要的权限比较小,只需要select和lock table的权限就足够了。所以可以创建一个备份数据库的用户,专门

  • 手头上有一台mysql数据库服务器,因为历史原因,mysql数据库的字符编码为latin1,但PHP程序使用的是utf8字符编码,所以磨途歌每次从mysql数据库中读取数据,都得向mysql数据库发送设置字符集的命令:mysql_query("SET NAMES latin1"); 虽然PHP通过设置字符编码能解决乱码问题,但是用phpmyadmin浏览mysql数据库,中文依然显示为乱码,很不方

  • 作者:王向 爱可生 DBA 团队成员,负责公司 DMP 产品的运维和客户 MySQL 问题的处理。擅长数据库故障处理。对数据库技术和 python 有着浓厚的兴趣。 本文来源:原创投稿 *爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。 背景 笔者在一次处理客户 MySQL 问题时遇到客户的 MySQL 的 sys 库不能用了并抛出一下错误: mysql> SELECT

  • MySQL备份时排除指定数据库的方法 使用mysqldump命令备份时候,--all-databases 可以备份所有的数据库。 使用ignore-table 还可以排除制定的表。但是,mysqldump没有参数可以排除数据库的。 要备份的数据库少的时候,可以通过mysqldump -uroot -p123456 --databases db1 db2 db3 > mysqldump.sql 这样

  • mysqldump bitsCN.com 根据mysql 5.5第6.4章节理解和自己翻译水平有限如有纰漏请指教,原文如下.http://dev.mysql.com/doc/refman/5.5/en/using-mysqldump.html 6.4 使用mysqldump备份(Using mysqldump for Backups)首先多余的不用说了备份用来干什么大家都清楚。mysqldump备

  • 阿里云服务器 mysqldump导出sql文件,在导入的过程中报权限错误 写了一个php脚本做定时复制数据库的工作 (1. 不要问我为什么不用linux 2.不要问我为什么不用主从同步。都是业务需要 我也没办法。) error_reporting(0); $DB = [ 'remote'=>[ "dsn"=>"mysql:host=xxx.xxx.xxx.xxx;dbname=dbname", "

  • 第一步,导出旧库 mysqldump --default-character-set latin1 -uroot -pXXX --database db > /tmp/old.sql 第二步,转换编码 iconv -t utf-8 -f gb2312 -c /tmp/old.sql > /tmp/new.sql 第三步,导入新库 修改new.sql,增加一条sql语句: "SET NAMES ut

 相关资料
  • 问题内容: 我正在寻找用于在mysql数据库中转储所有数据的语法。我不需要任何表格信息。 问题答案: 如果您使用触发器,则还需要包括 如果您使用的是选项,则还需要包括

  • 我们有非常大的数据库(Mariadb10.3)和mysqldump,需要2-3个小时来完成整个数据库转储,而且在转储过程中还会导致应用程序性能下降。 经过一番研究,我们发现可以在my.cnf文件中添加[mysqldump]节。还可以为其添加“quick”、“max_allowed_packet=512m”等选项。 如果我们可以添加‘单事务’也与上述设置,那么DB将不会在转储期间被锁定,这应该会加快

  • 问题内容: 我有正在清理的庞大,混乱的数据库。它可以容纳500多个表,这是将Magento Enterprise和Joomla合并到一个数据库中的结果。 更糟糕的是,有一组70多个Joomla表完全没有使用。这些都以开头。 仅删除这些表将很容易,但是我想先将其“ bak”起来(看看我在那里做了什么?)。在我的脑海中,我可以想象这样的命令: 但这是行不通的。最好的方法是什么?谢谢! 编辑:是的,我可

  • 我现在正在尝试复制一个名为的数据库。在MySQL命令行客户机中,我正在执行我得到错误消息: 说吧,告诉我我很蠢。我只是需要一些帮助,拜托。

  • MySQLdump和上传过程花费了太长的时间(~8小时)来完成整个过程。 我正在将活动数据库转储到mysqldump.tar文件和几乎3GB。当我加载到新的数据库,它需要6-8小时来完成这个过程(上传到新的数据库)。 我要完成这个过程,推荐的解决方案是什么?