PHPackages                             zjkal/mysql-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. zjkal/mysql-helper

ActiveLibrary[Database &amp; ORM](/categories/database)

zjkal/mysql-helper
==================

一个便捷的MySQL导入导出的助手类库。 a mysql import and export helper library.

v1.0.9(1y ago)676416[2 issues](https://github.com/zjkal/mysql-helper/issues)MITPHPPHP &gt;=7.1CI passing

Since Sep 1Pushed 1y ago2 watchersCompare

[ Source](https://github.com/zjkal/mysql-helper)[ Packagist](https://packagist.org/packages/zjkal/mysql-helper)[ RSS](/packages/zjkal-mysql-helper/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (10)Dependencies (1)Versions (11)Used By (0)

 [![MysqlHelper](https://camo.githubusercontent.com/75105f0eca68bdb5ee762dfae7f30ee4432aa696bd4b82857ee10adc68227a85/68747470733a2f2f63646e2e3078312e736974652f6c6f676f2d6d7973716c2d68656c7065722e737667)](https://camo.githubusercontent.com/75105f0eca68bdb5ee762dfae7f30ee4432aa696bd4b82857ee10adc68227a85/68747470733a2f2f63646e2e3078312e736974652f6c6f676f2d6d7973716c2d68656c7065722e737667)

 中文 | [English](https://github.com/zjkal/mysql-helper/blob/main/README_EN.md)

 [ ![License](https://camo.githubusercontent.com/7e1d4ffcbb21419a5018b7c3fb9d1a54821ec2d3cdfef0039618fc81d3e4483f/68747470733a2f2f706f7365722e707567782e6f72672f7a6a6b616c2f6d7973716c2d68656c7065722f6c6963656e7365) ](https://github.com/zjkal/mysql-helper/blob/main/LICENSE) [ ![PHP Version Require](https://camo.githubusercontent.com/0aff3d9bb4ed9ca6f767d14e82fcb750ba7b1c5b01448ce7dd74363ce04478a0/68747470733a2f2f706f7365722e707567782e6f72672f7a6a6b616c2f6d7973716c2d68656c7065722f726571756972652f706870) ](https://github.com/zjkal/mysql-helper) [ ![Latest Stable Version](https://camo.githubusercontent.com/da53f5cf4260aa7a8ec00f47b879768e472207e9d9842e3a8c67761dda5e2e61/68747470733a2f2f706f7365722e707567782e6f72672f7a6a6b616c2f6d7973716c2d68656c7065722f76) ](https://github.com/zjkal/mysql-helper) [ ![Total Downloads](https://camo.githubusercontent.com/afea16510d3f908eab734f4decf18bcb6347006ebe7134596371ef8fa9aaac6c/68747470733a2f2f706f7365722e707567782e6f72672f7a6a6b616c2f6d7973716c2d68656c7065722f646f776e6c6f616473) ](https://packagist.org/packages/zjkal/mysql-helper) [ ![GitHub Workflow Status](https://camo.githubusercontent.com/8cfa2b68480a5c7f033bdde512f9d5b7f5966ed6da487cafd6bf32ce9bbca30d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a6a6b616c2f6d7973716c2d68656c7065722f2e6769746875622f776f726b666c6f77732f7068702e796d6c3f6272616e63683d6d61696e) ](https://github.com/zjkal/mysql-helper)

`MysqlHelper` 是一个便捷的`通过PHP导入和导出Mysql数据库表结构和数据`的工具,可以快速实现mysql的数据库的导入和导出. 此类库旨在提供轻量化便捷的mysql导入导出, 开发初衷是web应用安装程序和插件类应用的数据结构导入. 因此没有做数据分批, 大量数据的导入导出并不适合.

🧩特性
---

[](#特性)

- 简单易用: 仅依赖`mysqlli`扩展,`开箱即用`
- 灵活操作: 兼容主流框架,使用更方便
- 长期维护: 作者为自由职业者,保证项目的`长期稳定`和`持续更新`

🚀安装
---

[](#安装)

通过Composer导入类库

```
composer require zjkal/mysql-helper
```

🌈使用文档
-----

[](#使用文档)

### 1. 实例化

[](#1-实例化)

*方式一: 常规方法*

```
use zjkal\MysqlHelper;

$mysql = new MysqlHelper('root', 'passwd', 'dbname', '127.0.0.1', '3306', 'utf8mb4', 'wp_');
```

*方式二: 实例化后,通过setConfig方法设置数据库配置*

```
$mysql = new MysqlHelper();
$mysql->setConfig(['username' => 'root', 'password' => 'passwd', 'database' => 'dbname']);
```

MysqlHelper针对常用的框架做了兼容,可以直接使用框架的数据库配置, 比如`ThinkPHP`框架或`Laravel`框架

```
$mysql = new MysqlHelper();
$config = config('database.connections.mysql');
$mysql->setConfig($config);
```

### 2. 导出数据

[](#2-导出数据)

- 如果实例化时, 已经设置了表前缀,导出的表名可以不用带前缀

```
//导出数据库(包含表结构和数据)
$mysql->exportSqlFile('test.sql');

//仅导出数据库表结构
$mysql->exportSqlFile('test.sql', false);

//导出指定表的结构和数据
$mysql->exportSqlFile('test.sql', true, ['table1', 'table2']);

//导出数据库所有表,并且添加禁用外键检查的SQL语句
$mysql->exportSqlFile('test.sql', true, [], true);
//php8以上可以更简洁的写法:
$mysql->exportSqlFile('test.sql', disableForeignKeyChecks: true);
```

### 3. 导入数据

[](#3-导入数据)

- 如果需要在导入过程中自定义表前缀, 则sql文件中的表前缀需要使用`__PREFIX__`占位符代替
- 如果实例化时, 已经设置了表前缀, 则可以不用传入第二个参数
- 增加了数据导入时的参数, 用于设置是否删除已存在的表, 默认不删除, 一般用于数据恢复.

```
//导入数据库
$mysql->importSqlFile('test.sql');

//导入数据库,并自动替换表前缀
$mysql->importSqlFile('test.sql', 'wp_');

//导入数据库,不替换前缀, 并且如果表已经存在,则先删除
$mysql->importSqlFile('test.sql', '', true);
//php8以上可以更简洁的写法:
$mysql->importSqlFile('test.sql', dropTableIfExists: true);
```

📃更新日志
-----

[](#更新日志)

> v1.0.9 2024年12月5日
>
> - 增加了数据导入时的参数, 用于设置是否删除已存在的表

> v1.0.8 2024年11月23日
>
> - 修复了导入表时重复替换忽略语句的BUG

> v1.0.7 2024年10月4日
>
> - 增加导入的稳定性:过滤空行

> v1.0.6 2024年8月27日
>
> - 导入时增加了判断是否存在同名表的逻辑

> v1.0.5 2024年6月14日
>
> - 增加了在导出时可以设置禁用外键检查的参数

点击查看更多> v1.0.4 2024年4月19日
>
> - 优化了.sql文件中注释的过滤规则

> v1.0.3 2023年12月9日
>
> - 实例化时如果已经设置了表前缀,导出的表名可以不包含前缀

> v1.0.2 2023年9月23日
>
> - 增加了数据导出的稳定性

> v1.0.1 2023年9月10日
>
> - 修复了在Thinkphp框架下端口识别错误的BUG
> - 增加了导入的稳定性

> v1.0.0 2023年9月2日
>
> - 首次发布

😎开发者们
-----

[](#开发者们)

    [ ![zjkal](https://avatars.githubusercontent.com/u/15082976?v=4)
 **zjkal** ](https://github.com/zjkal)   [ ![fedsin](https://avatars.githubusercontent.com/u/179591768?v=4)
 **fedsin** ](https://github.com/fedsin)   🐧QQ频道
-----

[](#qq频道)

[ ![QQ频道-世界上最好的编程语言](https://camo.githubusercontent.com/22cd029ebd45e77c7417ee3fbc3f0e68870caf9dfc55016d3a94023dbbd24fff/68747470733a2f2f63646e2e3078312e736974652f7172636f64652d717170642e706e67)](https://pd.qq.com/s/7h2hvcuxs)📖开源协议
-----

[](#开源协议)

MysqlHelper遵循[MIT开源协议](https://github.com/zjkal/mysql-helper/blob/main/LICENSE), 意味着您无需任何授权, 即可免费将MysqlHelper应用到您的项目中

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~51 days

Recently: every ~43 days

Total

10

Last Release

568d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15082976?v=4)[zjkal](/maintainers/zjkal)[@zjkal](https://github.com/zjkal)

---

Top Contributors

[![zjkal](https://avatars.githubusercontent.com/u/15082976?v=4)](https://github.com/zjkal "zjkal (54 commits)")[![fedsin](https://avatars.githubusercontent.com/u/179591768?v=4)](https://github.com/fedsin "fedsin (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

composerexporthelpersimportlibrariesmysqlphpphphelperexportmysqlimportzjkal

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zjkal-mysql-helper/health.svg)

```
[![Health](https://phpackages.com/badges/zjkal-mysql-helper/health.svg)](https://phpackages.com/packages/zjkal-mysql-helper)
```

###  Alternatives

[rah/danpu

Zero-dependency MySQL dump library for easily exporting and importing databases

62410.3k11](/packages/rah-danpu)[eftec/pdoone

Minimaist procedural PDO wrapper library

1116.1k9](/packages/eftec-pdoone)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1815.7k12](/packages/popphp-pop-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
