如何一键导出MySQL数据库
来源:互联网 发布:淘宝极有假货有假货吗 编辑:程序博客网 时间:2024/05/21 11:21
有时候,你的用户要求添加一个选项,导出整个数据库到一个SQL文件。虽然phpMyAdmin,以及Navicat有这个功能,但你的用户想要更简单点怎么办?
以下是如何一键导出MySQL数据库的php代码。
新建一个名为backup.php的文件,复制粘贴以下代码,然后编辑数据库连接设置和mysqldump的路径。有必要的话,你还可以添加一个backup.php超链接到你的程序里:
<A href="backup.php">导出整个数据库</A>
请注意,第一个php代码执行的时候,会导出zip压缩后的sql文件,所以此代码所在文件夹需要可写的权限。
如果你没有写的权限,请使用第二个php代码,缺点是导出的sql文件不会被zip压缩。
此代码需要可写权限:
<?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();]]> ?>
- 如何一键导出MySQL数据库
- 如何导出mysql数据库
- 如何导入导出MySQL数据库
- 如何导入导出MySQL数据库
- 如何导入导出MySQL数据库
- 如何导入导出MySQL数据库
- 如何导入导出MySQL数据库
- 如何导入导出MySQL数据库
- MYSQL中如何导出数据库
- 如何导入导出MySQL数据库
- 如何使用mysql workbench导出远程数据库
- linux下如何导入导出MySQL数据库
- linux下如何导入导出MySQL数据库
- 如何从mySQL 中导出数据库?
- MySQL如何导入导出数据库、数据库中的表?
- navicat链接MySQL数据库如何实现数据库的导入导出
- php动态生成Mysql数据库字典,并一键导出excel代码
- 如何导入导出MySQL数据库----*.sql文件操作
- 用ADO连接SQL Server数据库
- 并查集 Hdu 1856 More is better
- 黑马程序员——内省(IntroSpector)
- Div与Table,我们应正确对待
- 常见异常
- 如何一键导出MySQL数据库
- Queue小例子
- C/C++求对数
- JSP账号密码验证提示
- How uncaught exceptions are handled
- hdu1175
- JAVA的面向对象编程--------课堂笔记
- Win7优化加速[1][珍藏版]
- 黑马程序员——泛型