好吧,情况就是这样。我通过进入 phpMyAdmin 备份了数据库,然后在不选择任何数据库的情况下单击导出。现在,当我尝试将导出的 sql 文件导入到全新的 MySQL 安装中时,我收到如下错误 http://pastebin.com/vDHdG2T6(使用pastebin,因为它很长)
我知道我应该使用 mysqldump,因为它会跳过 information_schema 表。 您对如何解决此问题或导入该文件而无需编辑百万行文件有什么建议吗?
我也尝试过导入文件,例如: mysql -u root -p -f -h 本地主机 < Documents/localhost.sql This did import the the databases but many of them were corrupt, missing tables and such...I used the -f so it would keep going regardless of errors.
提前致谢!
编辑:
谢谢你们的建议,我确信你们走在正确的道路上。然而,与此同时,我最终也弄乱了我的备份文件,所以我不得不编写一些 php 代码来提取我需要的行。 这是以防有人遇到同样问题的情况:
$data = file_get_contents('import.sql');
if (! isset($_GET['db'])) {
//list databases
echo '<ul>';
preg_match_all("/Database: `(.*?)`/", $data, $dbs);
foreach ($dbs[1] as $database){
echo "<li><a href=\"index.php?db=$database\">$database</a></li>";
}
echo '</ul>';
} else {
$database = $_GET['db'];
//only get the queries for that db
$data = explode(PHP_EOL, $data);
$buffer = array();
$buffer_started = false;
foreach ($data as $key => $row){
if (preg_match("/Database: `.*?`/", $row) && $buffer_started) {
array_pop($buffer);
break;
}
if (preg_match("/Database: `$database`/", $row)) {
$buffer_started = true;
continue;
}
if ($buffer_started) {
array_push($buffer, $row);
}
unset($data[$key]);
}
echo '<textarea cols="128" rows="20">'.implode("\n", $buffer).'</textarea>';
}
将其保存为index.php并将您的sql放入同一目录中的import.sql中,它将列出在文件中找到的数据库,只需单击您想要的任何一个,它将在文本区域中显示查询。
祝你好运。
按照 beanland 的建议,我将回答我自己的问题,并使用上面编辑中稍加修改的代码:
error_reporting(E_ALL);
$data = file_get_contents('import.sql');
if (! isset($_GET['db'])) {
//list databases
echo '<ul>';
preg_match_all("/Database: `(.*?)`/", $data, $dbs);
foreach ($dbs[1] as $database){
echo "<li><a href=\"index.php?db=$database\">$database</a></li>";
}
echo '</ul>';
} else {
$database = $_GET['db'];
//only get the queries for that db
$data = explode(PHP_EOL, $data);
$buffer = array();
$buffer_started = false;
foreach ($data as $key => $row){
if (preg_match("/Database: `.*?`/", $row) && $buffer_started) {
array_pop($buffer);
break;
}
if (preg_match("/Database: `$database`/", $row)) {
$buffer_started = true;
continue;
}
if ($buffer_started) {
array_push($buffer, $row);
}
unset($data[$key]);
}
$data = implode("\n", $buffer);
//convert all InnoDB to MyISAM
$data = str_replace('InnoDB', 'MyISAM', $data);
echo '<a href="index.php">Back</a><h1>'.$database.'</h1><textarea cols="128" rows="20">'.$data.'</textarea>';
}