綠色資源網:您身邊最放心的安全下載站! 最新軟(ruǎn)件|熱門排行|軟件分類|軟件專題|廠商大全(quán)

綠色資源網

技術教程
您的位置:首頁數據庫類(lèi)MySQL → 如何導出mysql數據庫(kù)到sql文件

如何導出mysql數(shù)據庫到sql文件

我要評論 2012/04/24 14:11:42 來源(yuán):綠色資源網 編(biān)輯:ynaad.com [ ] 評論:0 點擊:696次

你的用戶可能(néng)會要求你增加(jiā)一個導(dǎo)出整個數(shù)據庫(kù)到sql文件(jiàn)的一個選項,當然phpMyAdmin或(huò)者(zhě)Navicat可以做到(dào)這(zhè)些,但是您的用戶可能想(xiǎng)要更(gèng)簡單的方法。

創建一個名為backup.php的(de)新文件,複製粘貼以下代碼。如果需要的話,編輯數(shù)據庫連接設置和mysqldump路徑。為你的應(yīng)用添(tiān)加一個backup.php的超級連接(jiē)。

<a href="back.php">export the whole database</a>

請注意:為了保存和壓縮轉儲文件,該腳本需要一個可寫權限。如(rú)果你的腳本沒有可寫權限請使用第二個版本,唯一的缺點(diǎn)是(shì)無法壓縮轉(zhuǎn)儲文件。

有壓(yā)縮版本需要可寫權限:

<?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數據庫

閱讀本文後您有什麽感想? 已有(yǒu) 人給出評價!

  • 1 歡迎喜歡
  • 1 白癡
  • 1 拜托
  • 1 哇
  • 1 加油
  • 1 鄙視
免费人欧美成又黄又爽的视频丨一本色道久久88综合日韩精品丨国产专区日韩精品欧美色丨午夜无遮挡男女啪啪视频丨国产欧美日韩综合精品一区二区丨亚洲精品无码不卡在线播HE丨亚洲精品国产精品国自产观看丨日韩国产高清av不卡