需要导出查询而不是在没有触发器的情况下为 mysqldump 创建文件
我有代码然后
$db_name = "db";
$outputfile = "/somewhere";
$new_db_name = 'newdb';
$cmd = 'mysqldump --skip-triggers %s > %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret);
if ($ret !=0 ) {
//log error message in $output
}
导入:
$cmd = 'mysql --database=%s < %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($new_db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret);
//etc.
unlink($outputfile);
但是在这里我应该做什么来获取导出查询,而不是每次都创建一个文件?
编辑:
回应马特的回复
- 我使用的是 Windows,您给出的代码是否可以工作,因为我不确定我已有的代码是否可以在 Windows 中工作,因为它似乎是 linux 命令?
- 我需要在 PHP 中编写脚本,这是在 joomla 中安装组件时将触发的过程
- PHP 中的 PIPE 是什么?
I have code as
$db_name = "db";
$outputfile = "/somewhere";
$new_db_name = 'newdb';
$cmd = 'mysqldump --skip-triggers %s > %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret);
if ($ret !=0 ) {
//log error message in $output
}
Then to import:
$cmd = 'mysql --database=%s < %s 2>&1';
$cmd = sprintf($cmd, escapeshellarg($new_db_name), escapeshellcmd($output_file));
exec($cmd, $output, $ret);
//etc.
unlink($outputfile);
But here what should i do to get the export query, rather than creating a file everytime?
EDITED:
IN THE RESPONSE OF REPLY OF MATT
- I am using Windows, will the code u have given work as I am not sure the code I already have will work in Windows as it seems to be linux commands?
- Its my need to script in PHP its the process which is going to be triggered when installing a component in joomla
- What is PIPE in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在 PHP 中执行此操作吗?您可以使用一些基本管道在终端中轻松执行此操作(无需创建任何文件)。
两个命令就完成了:
这会将
foo
复制到新数据库foo2
请注意,可以pipe in php ..但是..实际上,你应该在shell中执行此操作(IMO)。
编辑:
哎呀,出于某种原因我以为你想要触发器。上面编辑包括
--skip-triggers
编辑 2:
“Piping”是指路由
stdout
和stdin
。Windows 支持管道。上面的命令在 Windows 上对我有用。
但是,我无法让管道在 Windows 上通过 PHP 工作(至少对于
mysql
来说)。就像来自proc_open
的流忽略我的输入。因此,另一种方法是采用我上面提供的命令,将它们放入
.bat
文件中,然后看看是否可以通过system()
最终编辑:
你需要做一些研究,因为我无法解释每一个小细节。您是否引用了我指出的
system()
文档?它可以让您调用系统命令..我的建议是您制作一个基本脚本,将其命名为
bla.bat
:如果它与您的脚本位于同一文件夹中,那么您的脚本可以执行以下操作:
Viola。数据库已复制。
Do you have to do this in PHP? You can easily do this in a terminal with some basic piping (no need to create any files).
Two commands and you're done:
This copies
foo
into new databasefoo2
Note that it is possible to pipe in php .. but .. really, you should just do this in shell (IMO).
Edit:
Oops, for some reason I thought you wanted triggers. Edited above to include
--skip-triggers
Edit 2:
'Piping' refers to routing
stdout
andstdin
.Windows supports pipes. The above commands are working for me on Windows.
However, I am not able to get piping to work through PHP with Windows (at least not for
mysql
). It's like the streams fromproc_open
ignore my input.So the alternative would be to take the commands I provided above, put them in a
.bat
file, and see if you can call that script viasystem()
Final edit:
You're going to need to do some research as I can't explain every small detail. Did you reference the
system()
documentation I pointed to? It lets you call system commands ..My suggestion is you make a basic script, call it
bla.bat
:If that's in the same folder as your script, then your script can do this:
Viola. DB copied.