首页 > 解决方案 > 如何使用php下载大型mysql数据库

问题描述

在我的情况下,数据库几乎超过 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);`

标签: phpmysqldatabase-backups

解决方案


只需转到 phpmyadmin 并进行转储,最好的解决方案。


推荐阅读