如何導出mysql數(shù)據(jù)庫到sql文件
你的用戶可能會要求你增加一個導出整個數(shù)據(jù)庫到sql文件的一個選項,當然phpMyAdmin或者Navicat可以做到這些,但是您的用戶可能想要更簡單的方法。
創(chuàng)建一個名為backup.php的新文件,復制粘貼以下代碼。如果需要的話,編輯數(shù)據(jù)庫連接設置和mysqldump路徑。為你的應用添加一個backup.php的超級連接。
<a href="back.php">export the whole database</a>
請注意:為了保存和壓縮轉儲文件,該腳本需要一個可寫權限。如果你的腳本沒有可寫權限請使用第二個版本,唯一的缺點是無法壓縮轉儲文件。
有壓縮版本需要可寫權限:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "cars";
// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
--user=$username ";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
$command.= " > " . $dumpfname;
system($command);
// zip the dump file
$zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
$zip = new ZipArchive();
if($zip->open($zipfname,ZIPARCHIVE::CREATE))
{
$zip->addFile($dumpfname,$dumpfname);
$zip->close();
}
// read zip file and send it to standard output
if (file_exists($zipfname)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($zipfname));
flush();
readfile($zipfname);
exit;
}
?>
沒有壓縮,不需要可寫權限:
<?php
ob_start();
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "cars";
// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
--user=$username ";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
system($command);
$dump = ob_get_contents();
ob_end_clean();
// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" .
date("Y-m-d_H-i-s").".sql"));
flush();
echo $dump;
exit();]]>
?>
原文地址:http://webcheatsheet.com/php/how_to_create_a_dump_of_mysql_database_in_one_click.php
關鍵詞:mysql數(shù)據(jù)庫
閱讀本文后您有什么感想? 已有 人給出評價!
- 1
- 1
- 1
- 1
- 1
- 1