How can I import a large (14 GB) MySQL dump file into a new MySQL database?

前端 未结 9 1589
醉话见心
醉话见心 2020-11-28 17:45

How can I import a large (14 GB) MySQL dump file into a new MySQL database?

9条回答
  •  粉色の甜心
    2020-11-28 18:19

    I have made a PHP script which is designed to import large database dumps which have been generated by phpmyadmin or mysql dump (from cpanel) . It's called PETMI and you can download it here [project page] [gitlab page].

    It works by splitting an. sql file into smaller files called a split and processing each split one at a time. Splits which fail to process can be processed manually by the user in phpmyadmin. This can be easily programmed as in sql dumps, each command is on a new line. Some things in sql dumps work in phpmyadmin imports but not in mysqli_query so those lines have been stripped from the splits.

    It has been tested with a 1GB database. It has to be uploaded to an existing website. PETMI is open source and the sample code can be seen on Gitlab.

    A moderator asked me to provide some sample code. I'm on a phone so excuse the formatting.

    Here is the code that creates the splits.

                     //gets the config page
                      if (isset($_POST['register']) && $_POST['register'])
                    {
                     echo " ";
            $folder = "split/";
            include ("config.php");
            
            $fh = fopen("importme.sql", 'a') or die("can't open file");
            $stringData = "-- --------------------------------------------------------";
            fwrite($fh, $stringData);
            fclose($fh);
            
            
            $file2 = fopen("importme.sql","r");
            
            //echo "
    "; fclose($file2);

    Here is the code that imports the split

    "; // the above line checks to see if the html form has been submitted $dbname = $accesshost; $dbhost = $username; $dbuser = $password; $dbpasswd = $database; $table_prefix = $dbprefix; //the above lines set variables with the user submitted information //none were left blank! We continue... //echo "$importme"; echo "
    "; $importme = "$_GET[file]"; $importme = file_get_contents($importme); //echo "$importme

    "; $sql = $importme; $findme1 = '-- Indexes for dumped tables'; $pos1 = strpos($importme, $findme1); $findme2 = '-- AUTO_INCREMENT for dumped tables'; $pos2 = strpos($importme, $findme2); $dbhost = ''; @set_time_limit(0); if($pos1 !== false){ $splitted = explode("-- Indexes for table", $importme); // print_r($splitted); for($i=0;$i$sql
    "; if($table_prefix !== 'phpbb_') $sql = preg_replace('/phpbb_/', $table_prefix, $sql); $res = mysql_query($sql); } if(!$res) { echo 'error in query ', mysql_error(), '

    Try importing the split .sql file in phpmyadmin under the SQL tab.'; /* $i = $i +1; */ } else { echo ("Thank You! You will be redirected"); } } elseif($pos2 !== false){ $splitted = explode("-- AUTO_INCREMENT for table", $importme); // print_r($splitted); for($i=0;$i$sql
    "; if($table_prefix !== 'phpbb_') $sql = preg_replace('/phpbb_/', $table_prefix, $sql); $res = mysql_query($sql); } if(!$res) { echo 'error in query ', mysql_error(), '

    Try importing the split .sql file in phpmyadmin under the SQL tab.'; /* $i = $i +1; */ } else { echo ("Thank You! You will be redirected"); } } else { if($table_prefix !== 'phpbb_') $sql = preg_replace('/phpbb_/', $table_prefix, $sql); $res = mysql_query($sql); if(!$res) { echo 'error in query ', mysql_error(), '

    Try importing the split .sql file in phpmyadmin under the SQL tab.'; /* $i = $i +1; */ } else { echo ("Thank You! You will be redirected"); } } //echo 'done (', count($sql), ' queries).'; }

提交回复
热议问题