简体   繁体   English

无法导入从phpMyAdmin导出的SQL文件

[英]Can't import a sql file exported from phpMyAdmin

Ok, this is the situation. 好的,这就是这种情况。 I have backed up my database by going into phpMyAdmin and then, without selecting any database, clicking export. 我已通过进入phpMyAdmin备份了数据库,然后在未选择任何数据库的情况下单击“导出”。 Now when I try importing the exported sql file into a fresh MySQL installation, I'm getting errors like http://pastebin.com/vDHdG2T6 (used pastebin as it's pretty long) 现在,当我尝试将导出的sql文件导入到全新的MySQL安装中时,出现诸如http://pastebin.com/vDHdG2T6之类的错误(因为它很长,所以使用了pastebin)

I know I should have used mysqldump instead as it would skip the information_schema table. 我知道我应该使用mysqldump代替,因为它会跳过information_schema表。 Do you have any suggestions how to solve this or import that file without having to edit the million lines file? 您对如何解决此问题或导入该文件而无需编辑百万行文件有任何建议吗?

I've also tried importing the file like: mysql -u root -p -f -h localhost < 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. 我也尝试过导入文件,例如:mysql -u root -p -f -h localhost <Documents / localhost.sql这确实导入了数据库,但其中许多数据库已损坏,缺少表,诸如此类...我使用了-f,这样无论出现什么错误,它都会继续运行。

Thanks in advance! 提前致谢!

EDIT: 编辑:

Thank you guys for the suggestions, I'm sure you were on the right track. 谢谢大家的建议,我相信您的工作方向正确。 However, in the meantime I ended up messing up my backup file as well so I had to write a little php code to extract the lines I need. 但是,与此同时,我最终也弄乱了我的备份文件,因此我不得不编写一些php代码来提取所需的行。 Here it is in case someone has the same problem: 在这种情况下,如果有人遇到相同的问题:

$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>';
}

Save this as index.php and put your sql into import.sql in the same directory, it will list the databases found in file and just click on whichever you want and it will show the queries in a textarea. 将其另存为index.php,并将您的sql放入相同目录中的import.sql中,它将列出在文件中找到的数据库,只需单击所需的任何一个,它将在文本区域中显示查询。

Good luck. 祝好运。

As suggested by beanland, I will answer my own question, with a slightly modified code from the above edit: 正如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>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM