我有一个mysql转储文件,我该如何使用php将这个文件导入到mysql中?
实际上,这取决于转储文件的大小。小文件可以用 SQL 命令直接添加,而大文件则需要使用脚本导入。
快速搜索会显示许多预制脚本,将转储文件分解成较小的块以避免服务器超载。我过去曾使用过这个脚本:http://www.ozerov.de/bigdump.php
mysql
命令行客户端 (特别是如果文件很大的话!) :mysql --user=USERNAME --password=PASSWORD --host=HOST DB_NAME < /path/to/your/file.sql
system()
函数来执行这样的命令。file()
函数,逐行将文件读入数组中。您可以尝试以下代码:mysql_connect(BD_HOST, DB_USER, BD_PASS) or die("Error connecting to MySQL server: ".mysql_error());
mysql_select_db(DB_NAME);
$all_lines = file("mysql_dump.sql", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
foreach($all_lines as $query) {
if(substr($query, 0, 2) == "--") {
continue;
}
mysql_query($query) or die("Error executing query <br/> "$query\"<br/>".mysql_error());
}
multi_query
来导入中等大小的数据库备份文件。$command = file_get_contents($dumpfile);
$conn->multi_query($command);
while (mysqli_next_result($conn)); // Flush out the results.
max_allowed_packet
等。