使用 .php 文件生成 MySQL 转储 [英] Using a .php file to generate a MySQL dump
问题描述
这是我掌握的信息:
我正在使用基于 Linux 的系统使用 MySQL 和 PHP5.我需要能够从 .php 文件中生成 mysqldump
,然后将该转储存储在服务器上我指定位置的文件中.
I am working with a Linux based system using MySQL and PHP5. I need to be able to generate a mysqldump
from within a .php file, and then have that dump be stored in a file on the server in a location I would specify.
由于我是一个 PHP 小白,我希望有人给我一些帮助、指导或代码,这将满足我的要求.这必须从 Internet 远程运行.
As I'm a PHP nooblet, I'd like someone to give me some assistance, guidance, or code, that would do what I require. This would have to be run remotely from the Internet.
推荐答案
您可以使用exec()
执行外部命令的函数.
You can use the exec()
function to execute an external command.
注意:在 shell_exec()
和 exec()
之间,我会选择第二个,它不会将输出返回到 PHP 脚本——不需要让 PHP 脚本将整个 SQL 转储作为字符串获取:您只需要将其写入文件,这可以由命令本身完成.
Note: between shell_exec()
and exec()
, I would choose the second one, which doesn't return the output to the PHP script -- no need for the PHP script to get the whole SQL dump as a string : you only need it written to a file, and this can be done by the command itself.
该外部命令将:
That external command will :
- 使用正确的参数调用
mysqldump
, - 并将输出重定向到文件.
例如:
mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql
这意味着您的 PHP 代码将如下所示:
Which means your PHP code would look like this :
exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql');
当然,取决于您使用正确的连接信息,将 ...
替换为那些.
Of course, up to you to use the right connection information, replacing the ...
with those.
这篇关于使用 .php 文件生成 MySQL 转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!