如何使用php下载大型mysql数据库

时间:2019-02-05 12:41:13

标签: php mysql database-backups

在我的情况下,数据库几乎超过2GB,因此占用太多时间,因此如何分解它的任何想法,请帮助`backup_tables('localhost','xyz','xyz','xyz');

功能backup_tables($ host,$ user,$ pass,$ name) {     $ link = mysqli_connect($ host,$ user,$ pass,$ name);

if($tables == '*')
{
    $tables = array();
    $result = mysqli_query($link, 'SHOW TABLES');
    while($row = mysqli_fetch_row($result)){
        $tables[] = $row[0];}
    }
else{ $tables = is_array($tables) ? $tables : explode(',',$tables);}

//cycle through
foreach($tables as $table)
{
    $result = mysqli_query($link, 'SELECT * FROM '.$table);
    $num_fields = mysqli_num_fields($result);

    $return.= 'DROP TABLE '.$table.';';
    $row2 = mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";

    for ($i = 0; $i < $num_fields; $i++) 
    {
        while($row = mysqli_fetch_row($result))
        {
            $return.= 'INSERT INTO '.$table.' VALUES(';
            for($j=0; $j < $num_fields; $j++) 
            {
                $row[$j] = addslashes($row[$j]);
                $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                if ($j < ($num_fields-1)) { $return.= ','; }
            }
            $return.= ");\n";
        }
    }

    $return.="\n\n\n";
}

// save file
$path = 'downloads/';
$handle = fopen($path.'db-backup-'.(md5(implode(',',$tables))).'.sql','w+');
$filewrite = fwrite($handle,$return);

$fileList = glob('downloads/*.sql');

echo $filename = basename($fileList['0']); 

fclose($handle);`

1 个答案:

答案 0 :(得分:0)

只需转到phpmyadmin并执行转储,这是最佳解决方案。