【问题标题】:how to download large mysql database using php如何使用php下载大型mysql数据库
【发布时间】:2019-02-05 12:41:12
【问题描述】:

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

function 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);`

【问题讨论】:

  • 也许使用 shell 脚本,然后执行 mysqldump?
  • mysqldump ... | xz -9 &gt; my-dump.sql.xz 以获得更小的转储文件。如果xz 不可用,请改用`bzip2or gzip`。
  • 有没有办法在没有phpmyadmin的情况下使用php?
  • 你可以试试this

标签: php mysql database-backups


【解决方案1】:

只要去 phpmyadmin 做一个转储,最好的解决方案。

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    相关资源
    最近更新 更多