如何使用php脚本导出mysql数据库?

时间:2015-05-27 22:59:23

标签: php mysql mysqli mysqldump mysql-backup

所以我需要一个脚本,当我打开script.php页面时,例如,将一个数据库从mysql导出到.sql文件,我不知道如何在php中执行,可以帮助一些我呢?

编辑代码:

    <?php

$dbhost   = "localhost";
$dbuser   = "root";
$dbpwd    = "";
$dbname   = "activmanagement";
$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";

passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");

// report - disable with // if not needed
// must look like "-- Dump completed on ..." 

echo "$dumpfile "; passthru("tail -1 $dumpfile");

?>

这一行:

passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");

1 个答案:

答案 0 :(得分:0)

参考PHP - exec() vs system() vs passthru()。你只需要这样的东西

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $dbname   = 'activmanagement';

    $sql_backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
    $dump = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $sql_backup_file";

    system($dump);
?>