国语自产精品视频在线看:您身边最放心的安全下载站! 最新软件|热门排行|软件分类|软件专题|厂商大全

国语自产精品视频在线看

技术教程
您的位置:首页数据库类MySQL → 如何导出mysql数据库到sql文件

如何导出mysql数据库到sql文件

我要评论 2012/04/24 14:11:42 来源:国语自产精品视频在线看 编辑:downcc.com [ ] 评论:0 点击:696次

你的用户可能【hù kě néng】🕝会要求🔵你增加🚡一个导出整个数据库🏴到【dào】sql文件的一个选🌍项【xiàng】,当然【dāng rán】phpMyAdmin或者Navicat可以做📷到这些【dào zhè xiē】,但是您【dàn shì nín】的用户可能【hù kě néng】🕝想要更简单的方♌法。

创建一😒个名为🚳backup.php的新文【de xīn wén】件🚂,复制粘贴以下【tiē yǐ xià】代码。如果需要的话,编辑数据库连接【lián jiē】🍺设置和🔲mysqldump路径。为你的应用添加一个【jiā yī gè】backup.php的超级【de chāo jí】💂连接【lián jiē】🍺。

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

请注意:为了保【wéi le bǎo】存和压【cún hé yā】🍇缩转储🦀文件【wén jiàn】,该脚本需要一【xū yào yī】个可写权限。如果你🔱的脚本【de jiǎo běn】没有可【méi yǒu kě】写权限请使用⏸第二个【dì èr gè】🤲版本🚝,唯一的缺点是💾无法压缩转储🦀文件【wén jiàn】。

有压缩版本需要可写权限:

<?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();]]>
?>

原文地【yuán wén dì】址🔚:http://webcheatsheet.com/php/how_to_create_a_dump_of_mysql_database_in_one_click.php

关键词:mysql数据库

阅读本文后您有什么感想? 已有 人给出评价!

  • 1 欢迎喜欢
  • 1 白痴
  • 1 拜托
  • 1 哇
  • 1 加油
  • 1 鄙视