简体   繁体   English

如何在不使用 exec() 的情况下在 PHP 中执行 mysqldump?

[英]How to perform mysqldump in PHP without using exec()?

I am trying to run this code:我正在尝试运行此代码:

$cmd = '/usr/bin/mysqldump --user=myUsername --password="myPassword" --host=localhost myDatabase > myOutputSqlFilePath;
exec($cmd, $output, $return_val);

But it fails, as it seems exec() is not available for PHP on this particular server.但它失败了,因为似乎exec()不适用于此特定服务器上的 PHP。 (I have confirmed this by trying to run exec("mkdir test-dir", $output, $return_val); which fails to create the test directory.) (我已经通过尝试运行exec("mkdir test-dir", $output, $return_val);来确认这一点,但它无法创建测试目录。)

So, I would like to know what other options I have to perform the mysqldump in PHP?所以,我想知道在 PHP 中执行 mysqldump 还需要哪些其他选项?

Now, I have some related PHP code that creates a database, like this:现在,我有一些相关的 PHP 代码来创建数据库,如下所示:

function createDb($cPanelUser, $cPanelPass, $dbName) {

    $buildRequest = "/frontend/paper_lantern/sql/addb.html?db=$dbName";

    $openSocket = fsockopen('localhost',2082);
    if(!$openSocket) {
        return "Socket error";
        exit();
    }

    $authString = $cPanelUser . ":" . $cPanelPass;
    $authPass = base64_encode($authString);
    $buildHeaders  = "GET " . $buildRequest ."\r\n";
    $buildHeaders .= "HTTP/1.0\r\n";
    $buildHeaders .= "Host:localhost\r\n";
    $buildHeaders .= "Authorization: Basic " . $authPass . "\r\n";
    $buildHeaders .= "\r\n";

    fputs($openSocket, $buildHeaders);
    while(!feof($openSocket)) {
        fgets($openSocket,128);
    }
    fclose($openSocket);
}

So maybe there is something like this that can be used to perform the mysqldump?所以也许有这样的东西可以用来执行mysqldump?

Or maybe there's an entirely different solution??或者也许有一个完全不同的解决方案?

exec() wasn't available, so in the end I solved this by using mysqli with SHOW CREATE tables and then selecting all the rows, and creating/populating the new database with that data - all with mysqli . exec()不可用,所以最后我通过使用mysqli和 SHOW CREATE 表然后选择所有行,并使用该数据创建/填充新数据库 - 全部使用mysqli 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM