您现在的位置是:网站首页> 编程资料编程资料
如何导出mysql数据库到sql文件_资源网
                     2022-05-11
                902人已围观
                
                2022-05-11
                902人已围观
            
简介 如何导出mysql数据库到sql文件_资源网
你的用户可能会要求你增加一个导出整个数据库到sql文件的一个选项,当然phpMyAdmin或者navicat可以做到这些,但是您的用户可能想要更简单的方法。
创建一个名为backup.php的新文件,复制粘贴以下代码。如果需要的话,编辑数据库连接设置和mysqldump路径。为你的应用添加一个backup.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; 
 } 
 ?>
没有压缩,不需要可写权限:
 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
相关内容
- 一个小时成功安装SQL Server 2012图解教程_资源网
- SQL Server 2005 不允许远程连接解决方法_资源网
- Sql数据库日志已满的解决办法_资源网
- Sql server 2005安装出错解决方案合集_资源网
- SQL重复记录查询 查询多个字段、多表查询、删除重复记录_资源网
- sql 多表查询 Select语句查询实例分析_资源网
- 常用的MySQL数据库命令大全_资源网
- 八步解决ACCESS自动编号问题,自己用的SQL SERVER 2000数据库,转换为ACCESS数据库_资源网
- MSSQL Server数据库的四种连接方法和sql连接字符串_资源网
- 经典MSSQL语句大全和常用SQL语句命令的作用_资源网
 
                                
                                                         
                                
                                                         
                                
                                                         
 
    