Blog
Browsing all articles in PHP / MYSQL
2

Faster Way To Import Mysql Dump

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 |  copy code |? 
01
<?php
02
$host ="Your Host";                   // Change host (e.g localhost)
03
$dbusername ="Database username";     // Change Database Username
04
$dbpassword ="Database password";      // Change Database Password
05
$database ="Your database";        // Change Database
06
$dumpfile = "Your Dump file";        // Set dump file path (e.g. demodb.sql)   
07
 
08
mysql_connect($host,$dbusername,$dbpassword);
09
mysql_select_db($database);
10
$file = fopen($dumpfile, 'r');
11
print '<pre>';
12
print mysql_error();
13
$temp = '';
14
$count = 0;
15
 
16
while($line = fgets($file)) {
17
    if ((substr($line, 0, 2) != '&minus;&minus;') && (substr($line, 0, 2) != '/*') && strlen($line) > 1) {
18
    $last = trim(substr($line, &minus;2, 1));
19
    $temp .= trim(substr($line, 0, &minus;1));
20
     if ($last == ';') {
21
         mysql_query($temp);
22
          $count++;
23
           $temp = '';
24
     }
25
  }
26
}
27
echo mysql_error();
28
echo "Total {$count} queries fire(s).\n";
29
echo "Enjoing this fastest mysql dump importer..\n";
30
echo '</pre>';
31
?>

Enjoying this fastest mysql dump importer.