需要导出查询而不是在没有触发器的情况下为 mysqldump 创建文件

发布于 2024-08-31 00:56:44 字数 844 浏览 0 评论 0原文

我有代码然后

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-09-07 00:56:44

您必须在 PHP 中执行此操作吗?您可以使用一些基本管道在终端中轻松执行此操作(无需创建任何文件)。

两个命令就完成了:

echo 'create database foo2' | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

这会将 foo 复制到新数据库 foo2

请注意,可以pipe in php ..但是..实际上,你应该在shell中执行此操作(IMO)。

编辑:

哎呀,出于某种原因我以为你想要触发器。上面编辑包括 --skip-triggers

编辑 2:

“Piping”是指路由 stdoutstdin

Windows 支持管道。上面的命令在 Windows 上对我有用。

但是,我无法让管道在 Windows 上通过 PHP 工作(至少对于 mysql 来说)。就像来自 proc_open 的流忽略我的输入。

因此,另一种方法是采用我上面提供的命令,将它们放入 .bat 文件中,然后看看是否可以通过 system()

最终编辑

你需要做一些研究,因为我无法解释每一个小细节。您是否引用了我指出的 system() 文档?它可以让您调用系统命令..

我的建议是您制作一个基本脚本,将其命名为 bla.bat

@echo off
echo create database foo2 | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

如果它与您的脚本位于同一文件夹中,那么您的脚本可以执行以下操作:

<?php
system('cmd.exe /C ' . dirname(__FILE__).'\\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:

echo 'create database foo2' | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

This copies foo into new database foo2

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 and stdin.

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 from proc_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 via system()

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:

@echo off
echo create database foo2 | mysql -uroot
mysqldump --skip-triggers -uroot foo | mysql -uroot foo2

If that's in the same folder as your script, then your script can do this:

<?php
system('cmd.exe /C ' . dirname(__FILE__).'\\bla.bat');

Viola. DB copied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文