如何导出mysql数据库到sql文件
你的用户可能会要求你增加一个导出整个数据库到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();]]>
?>
原文地址:http://webcheatsheet.com/php/how_to_create_a_dump_of_mysql_database_in_one_click.php
关键词:mysql数据库
阅读本文后您有什么感想? 已有 人给出评价!
- 1
- 1
- 1
- 1
- 1
- 1