容器技术交流

 找回密码
 立即注册
查看: 4046|回复: 3

如何一键导出MySQL数据库

[复制链接]
发表于 2012-4-25 00:11:05 | 显示全部楼层 |阅读模式
有时候,你的用户要求添加一个选项,导出整个数据库到一个SQL文件。虽然phpMyAdmin,以及Navicat有这个功能,但你的用户想要更简单点怎么办?
以下是如何一键导出MySQL数据库的php代码。
新建一个名为backup.php的文件,复制粘贴以下代码,然后编辑数据库连接设置和mysqldump的路径。有必要的话,你还可以添加一个backup.php超链接到你的程序里:
  1. <A href="backup.php">导出整个数据库</A>
复制代码
请注意,第一个php代码执行的时候,会导出zip压缩后的sql文件,所以此代码所在文件夹需要可写的权限。
如果你没有写的权限,请使用第二个php代码,缺点是导出的sql文件不会被zip压缩。

此代码需要可写权限:
  1. <?php
  2.   
  3. $username = "root";  
  4. $password = "";  
  5. $hostname = "localhost";  
  6. $dbname   = "cars";
  7.   
  8. // if mysqldump is on the system path you do not need to specify the full path
  9. // simply use "mysqldump --add-drop-table ..." in this case
  10. $dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
  11. $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
  12.     --user=$username ";
  13. if ($password)  
  14.         $command.= "--password=". $password ." ";  
  15. $command.= $dbname;
  16. $command.= " > " . $dumpfname;
  17. system($command);
  18.   
  19. // zip the dump file
  20. $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
  21. $zip = new ZipArchive();
  22. if($zip->open($zipfname,ZIPARCHIVE::CREATE))  
  23. {
  24.    $zip->addFile($dumpfname,$dumpfname);
  25.    $zip->close();
  26. }
  27.   
  28. // read zip file and send it to standard output
  29. if (file_exists($zipfname)) {
  30.     header('Content-Description: File Transfer');
  31.     header('Content-Type: application/octet-stream');
  32.     header('Content-Disposition: attachment; filename='.basename($zipfname));
  33.     flush();
  34.     readfile($zipfname);
  35.     exit;
  36. }
  37. ?>
复制代码


下面的代码不需要可写权限:
  1. <?php
  2. ob_start();
  3.   
  4. $username = "root";  
  5. $password = "";  
  6. $hostname = "localhost";  
  7. $dbname   = "cars";
  8.   
  9. // if mysqldump is on the system path you do not need to specify the full path
  10. // simply use "mysqldump --add-drop-table ..." in this case
  11. $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
  12.     --user=$username ";
  13. if ($password)  
  14.         $command.= "--password=". $password ." ";  
  15. $command.= $dbname;
  16. system($command);
  17.   
  18. $dump = ob_get_contents();  
  19. ob_end_clean();
  20.   
  21. // send dump file to the output
  22. header('Content-Description: File Transfer');
  23. header('Content-Type: application/octet-stream');
  24. header('Content-Disposition: attachment; filename='.basename($dbname . "_" .  
  25.     date("Y-m-d_H-i-s").".sql"));
  26. flush();
  27. echo $dump;
  28. exit();]]>
  29. ?>
复制代码


发表于 2012-4-25 00:13:16 | 显示全部楼层
马克一下  
发表于 2012-4-25 02:15:42 | 显示全部楼层
如果我的库有5G多,到时候这个.sql好不好导入?
发表于 2012-4-25 16:31:30 | 显示全部楼层
不错啊。好东西啊。太给力了!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Archiver|URLOS ( 粤ICP备18087780号 )

GMT+8, 2025-5-15 11:35 , Processed in 0.028535 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表