eugabrielsilva/sql-dumper

纯PHP制作的SQL备份导出工具

v1.1.0 2022-12-26 21:29 UTC

This package is auto-updated.

Last update: 2024-09-20 01:58:25 UTC


README

这是一个PHP包,旨在使用纯PHP从MySQL数据库生成SQL备份,无需依赖mysqldump或其他工具。

Latest Version Total Downloads License PHP Version

要求

  • PHP版本7.4或更高,且已启用mysqlimbstring扩展
  • MySQL服务器以及具有SHOWSELECT权限的用户

安装

通过Composer

composer require eugabrielsilva/sql-dumper

手动安装
复制src/SQLDumper.php并将其包含到您的脚本中。

用法

// Include Composer packages if not yet
require 'vendor/autoload.php';

// Create SQLDumper instance
$db = new \GabrielSilva\SQLDumper\SQLDumper();

// Set your server settings (optional)
$db->host = 'localhost';
$db->user = 'root';
$db->password = '';
$db->db = 'app';

// Generate dump to an SQL file
$db->dumpToFile('backup.sql');

// You can also dump to a string
$dump = $db->dumpToString();

选项

您可以使用一些选项来修改SQLDumper,这些选项是实例的属性,可以被覆盖。

注意:下面显示的值是默认值。

$db->host = 'localhost'; // Server hostname
$db->user = 'root'; // Username to connect to the database
$db->password = ''; // User password
$db->db = 'app'; // Database name
$db->port = 3306; // Server port
$db->charset = 'utf8'; // Database charset
$db->includes = []; // Array of tables to include in dump, empty for all
$db->excludes = []; // Array of tables to exclude from dump
$db->createTables = true; // Include CREATE TABLE statements in dump
$db->createDatabase = false; // Include CREATE DATABASE statement in dump
$db->dropTables = false; // Include DROP TABLE statements in dump
$db->dropDatabase = false; // Include DROP DATABASE statement in dump
$db->insertData = true; // Include data in dump
$db->deleteData = false; // Delete data from table before inserting
$db->insertType = 'INSERT'; // Insert type: INSERT, INSERT IGNORE or REPLACE
$db->safeMode = true; // Use IF EXISTS and IF NOT EXISTS clauses in dump
$db->chunkSize = 3000; // Number of rows to fetch per query (useful in large tables)

重要!如果您更改了任何服务器选项,例如hostuserpassworddbportcharset,您需要使用$db->connect()方法刷新数据库连接。

致谢

Gabriel Silva开发和维护的库。