简体   繁体   English

如何通过MYSQL / PHP将.sql.gz文件导入我的数据库? (不是命令行)

[英]How to import .sql.gz file into my database via MYSQL/PHP? (not command line)

I have a backup file (.sql.gz) and I want to import it to my empty database on localhost programmatically like how phpmyadmin does, inside a browser with php/mysql/javascript and not via command line. 我有一个备份文件(.sql.gz),我想以编程方式将其导入我在localhost上的空数据库,就像phpmyadmin一样,在浏览器中使用php / mysql / javascript,而不是通过命令行。 Since it's my localhost, security is not really a concern, I just want to automate a task I have to perform regularly when developing. 由于它是我的本地主机,安全性并不是真正的问题,我只想自动执行我必须在开发时定期执行的任务。

There are a bunch of questions here but they all deal with command line solution. 这里有很多问题,但它们都涉及命令行解决方案。

edit: I already have a xampp installation. 编辑:我已经安装了xampp。 But the whole procedure is tedious, I have to first delete the database, then recreate it and then import my data. 但整个过程很繁琐,我必须首先删除数据库,然后重新创建它,然后导入我的数据。 I have to move b/w previous database backus fairly often. 我必须经常移动b / w以前的数据库backus。 So I want to automate the process. 所以我想自动化这个过程。 I just give in the backup .sql.gz file via html form input and it does all of the above automatically. 我只是通过html表单输入提供备份.sql.gz文件,它会自动执行以上所有操作。

I'd open it with gzfile , separate it on the query-delimiter and put it into mysqli::query 我用gzfile打开它,在query-delimiter gzfile它分开并将其放入mysqli :: query中

$file = implode('', gzfile($sqlFile)); // there doesn't exist a gz* function which reads it completely into a string?
$query = $substring_delimiter = "";
$last_was_backslash = false;
$outside_begin_end = true;
$delimiter = ';';
for ($i = 0; $i < strlen($file); $i++) {
    if ($i > 3 && !strcasecmp(substr($file, $i - 4, 5), "BEGIN") && !$substring_delimiter)
        $outside_begin_end = false;
    if (!$outside_begin_end && !strcasecmp(substr($file, $i - 2, 3), "END") && !$substring_delimiter)
        $outside_begin_end = true;
    if ($i > 7 && !strcasecmp(substr($file, $i - 8, 9), "DELIMITER") && trim($query) == '') {
        $delimiter = '';
        do {
            $delimiter .= $file[$i];
        } while (++$i < strlen($file) && $file[$i] != PHP_EOL)
        $delimiter = trim($delimiter);
    }
    if ($file[$i] == '\'' || $file[$i] == '"')
        if ($substring_delimiter) {
            if ($substring_delimiter == $file[$i] && !$last_was_backslash) 
                $substring_delimiter = "";
        } else {
            $substring_delimiter = $file[$i];
        }
    if ($outside_begin_end && !$substring_delimiter && !strcasecmp($delimiter, substr($file, $i))) {
        $sql->query($query); // where $sql is a mysqli instance
        $query = "";
    } else {
        $query .= $file[$i];
    }
    if ($file[$i] == '\\')
        $last_was_backslash = !$last_was_backslash;
    else
        $last_was_backslash = false;
}
if (trim($query) != "")
    $sql->query($query);

Comment from @MarcB is correct. 来自@MarcB的评论是正确的。 Use PHP to call out to a shell process to load the SQL script. 使用PHP调用shell进程来加载SQL脚本。

Writing a PHP script to execute a backup script is a waste of time. 编写PHP脚本来执行备份脚本是浪费时间。 You basically have to implement the mysql client in PHP. 你基本上必须在PHP中实现mysql客户端。

The better solution is something like this: 更好的解决方案是这样的:

shell_exec("gunzip -c $file_sql_gz | mysql --defaults-file=$my_cnf $database_name");

Where $my_cnf is the name of a my.cnf-like file that contains host, user, password to connect. 其中$my_cnf是类似my.cnf的文件的名称,包含要连接的主机,用户和密码。

See also some of my past answers: 另见我过去的一些答案:


Re your comment: 你的评论:

Refer to http://www.php.net/manual/en/features.file-upload.post-method.php 请参阅http://www.php.net/manual/en/features.file-upload.post-method.php
You can access the temp name of a file upload with $_FILES['userfile']['tmp_name'] . 您可以使用$_FILES['userfile']['tmp_name']访问文件上载的临时名称。

我相信Sypex Dumper可以做到这一点。

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

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