phpMyAdmin 导出功能如何工作?

2022-01-05 00:00:00 mysql phpmyadmin

如果我想创建一个与 phpMyAdmin 中的导出"选项卡执行相同操作的 PHP 函数,我该怎么做?我不知道是否有执行此操作的 MySQL 函数,或者 phpMyAdmin 是否只是手动构建导出文件(在 SQL 中).没有外壳访问.只是使用 PHP.

If I were to want to create a PHP function that does the same thing as the Export tab in phpMyAdmin, how could I do it? I don't know if there is a MySQL function that does this or if phpMyAdmin just builds the export file (in SQL that is) manually. Without shell access. Just using PHP.

我尝试了 mysqldump 的文档,但是似乎需要使用外壳.我不太确定那是什么——也许我的问题是:你如何使用 shell?

I tried the documentation for mysqldump, but that seemed to require using the shell. I'm not quite sure what that even is -- maybe my question is: how do you use shell?

我的愚蠢想法是允许非技术用户使用 MySQL 在一台服务器(比如本地主机)上构建一个站点,然后将站点、数据库和所有内容导出到另一台服务器(例如远程服务器).

My silly idea is to allow non-technical users to build a site on one server (say a localhost) using MySQL then export the site, database and all, to another server (eg. a remote server).

我想我对导入过程非常清楚.

I think I'm pretty clear on the Import process.

推荐答案

您可以查看 phpMyAdmin 源代码(开源软件的一个优势).查看libraries/export/sql.php 脚本文件中的export.php 脚本和支持函数.

You can check the phpMyAdmin source code (an advantage of open-source software). Check the export.php script and the supporting functions in the libraries/export/sql.php script file.

总而言之,phpMyAdmin 的作用是:

In summary, what phpMyAdmin does is:

  • 获取给定数据库中的表列表(SHOW TABLES FROM...),
  • 获取每个表的创建查询(SHOW CREATE TABLE...),
  • 解析它并从中提取列定义,
  • 获取所有数据(SELECT * FROM...)
  • 根据列数据构建查询.

我为我自己的应用程序编写了类似的代码(出于备份目的,当 phpMyAdmin 的 GPL 许可证不允许我使用它时),但是我使用 DESCRIBE 来获取列定义.我认为他们更愿意解析 SHOW CREATE TABLE 输出,因为包含比 DESCRIBE 输出更多的信息.

I've written similar code for my own apps (for backup purposes, when the GPL license of phpMyAdmin doesn't allow me to use it), however I use DESCRIBE to get column definitions. I think they rather parse the SHOW CREATE TABLE output because contains more information than DESCRIBE output.

这种生成 SQL 语句的方法需要小心处理转义,但它具有一定的灵活性,因为您可以转换类型、过滤或清理数据等.它也比使用像 mysqldump 这样的工具慢很多并且您应该注意不要消耗所有可用内存(尽快写入,经常写入,不要将所有内容都保留在内存中).

This way to generate SQL sentences requires a bit of care to handle the escaping but it allows for some flexibility, as you can convert types, filter or sanitize data, etc. It is also a lot slower than using a tool like mysqldump and you should take care of not consuming all available memory (write soon, write often, don't keep everything in memory).

如果你要实现一个迁移过程(从服务器到服务器),也许使用一些 shell 脚本和直接调用 mysqldump 会更容易,除非你用 PHP 做所有事情.

If you will implement a migration process (from server to server) maybe it would be easier to do it with some shell scripting and calling mysqldump directly, unless you will do everything with PHP.

相关文章