欢迎您来到懒之才-站长的分享平台!   学会偷懒,并懒出境界是提高工作效率最有效的方法!
首页 > 经验分享 > 数据库 > 如何导出mysql数据库到sql文件

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

2018-07-24 478 收藏 0 赞一个 0 真差劲 0 去评论

你的用户可能会要求你增加一个导出整个数据库到sql文件的一个选项,当然phpMyAdmin或者Navicat可以做到这些,但是您的用户可能想要更简单的方法。

创建一个名为backup.php的新文件,复制粘贴以下代码。如果需要的话,编辑数据库连接设置和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();]]>
?>

一、推荐使用迅雷或快车等多线程下载软件下载本站资源。

二、未登录会员无法下载,登录后可获得更多便利功能,若未注册,请先注册。

三、如果服务器暂不能下载请稍后重试!总是不能下载,请点我报错 ,谢谢合作!

四、本站大部分资源是网上搜集或私下交流学习之用,任何涉及商业盈利目的均不得使用,否则产生的一切后果将由您自己承担!本站将不对任何资源负法律责任.如果您发现本站有部分资源侵害了您的权益,请速与我们联系,我们将尽快处理.

五、如有其他问题,请加网站设计交流群(点击这里查看交流群 )进行交流。

六、如需转载本站资源,请注明转载来自并附带链接

七、本站部分资源为加密压缩文件,统一解压密码为:www.aizhanzhe.com

大家评论