Wednesday, December 22, 2010

How to import large mysql dump to database? or Fastest mysql dump importer

Hey guys, it is difficult to import large mysql dump file to database. See below for how to import mysql dump using only one file.

Step 1: Download dump file. (e.g.  demodb.sql).
Step 2: Change connection, username, password, database variable values.
Step 3: Change dump file name with proper path.
Step 4: Run this page.

Import mysql dump file code.

+++++++++++++++++++++++++++++++++++++++++++++++++++++
<?php
$host ="Your Host";                   // Change host (e.g localhost)
$dbusername ="Database username";     // Change Database Username
$dbpassword ="Database password";      // Change Database Password
$database ="Your database";        // Change Database
$dumpfile = "Your Dump file";        // Set dump file path (e.g. demodb.sql)   

mysql_connect($host,$dbusername,$dbpassword);
mysql_select_db($database);
$file = fopen($dumpfile, 'r');
print '<pre>';
print mysql_error();
$temp = '';
$count = 0;

while($line = fgets($file)) {
    if ((substr($line, 0, 2) != '--') && (substr($line, 0, 2) != '/*') && strlen($line) > 1) {
    $last = trim(substr($line, -2, 1));
    $temp .= trim(substr($line, 0, -1));
     if ($last == ';') {
         mysql_query($temp);
          $count++;
           $temp = '';
     }
  }
}
echo mysql_error();
echo "Total {$count} queries fire(s).\n";
echo "Enjoing this fastest mysql dump importer..\n";
echo '</pre>';
?>
+++++++++++++++++++++++++++++++++++++++++++++++++++++

Enjoying this fastest mysql dump importer.