How to install a big database in an MySQL server

Normally when installing a small database on the mysql server, one does not meet much problems simply execute the script in any mysql client would do.

However when installing a big database it suddenly becomes a whole new story all together.

Pumping too much data or too long a query string to the database would result in the failure of the MySql client or the MySQL server to go away.

During such scenarios, one will necessarily have to write up one’s own script. In most cases, it would be written in PHP.

A simple way to do it is as written below.

<?php
//This file holds the functions needed for handling all interactions with the MYSQL database

//These are the details for setting up a connection
define(‘DB_HOST’, ‘localhost’); // database host
define(‘DB_USER’, ‘your_user_name’); // username
define(‘DB_PASS’, ‘your_password’); // password
define(‘DB_NAME’, ‘your_database_name’); // database name

$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(‘could not connect to database’.DB_HOST);
mysql_select_db(DB_NAME) or die(‘could not connect to database ‘.DB_NAME);

//selects from the database
function connect_query($query){
$rs = mysql_query($query);
if(!$rs){
die($query.'<br>’.’Invalid query: ‘ . mysql_error());
}
else{
return $rs;
}
}

//generic insert
function connect_insert($table_name , $data_array){

$data_keys = ”;
$data_values = ”;

$array_keys = array_keys($data_array);
for($x =0 ; $x < count($array_keys )-1 ; $x++){
$curr_key = $array_keys[$x];
$curr_value = $data_array[$curr_key ];
$data_keys .= $curr_key.” , “;
$data_values .= “‘”.$curr_value.”‘ , “;
}

$curr_key = $array_keys[count($array_keys )-1];
$curr_value = $data_array[$curr_key];
$data_keys .= $curr_key;
$data_values .= “‘”.$curr_value.”‘”;

$query = “insert into $table_name($data_keys) values($data_values)”;
connect_query($query);
}

//returns a row in the results. Use int types as pointers in array
function connect_fetch_row($result){
return mysql_fetch_row($result);
}

//returns a row in the results. Use name as pointers in array
function connect_fetch_array($result){
return mysql_fetch_array($result);
}
function connect_latest_id(){
return mysql_insert_id();
}

$my_sqls = file_get_contents(“sql_file.sql”);
$my_sqls = explode(“;\r\n”, $my_sqls);

$count = 1;
foreach($my_sqls as $curr_sql_statement){
connect_query($curr_sql_statement);
echo $count.” of “.count($my_sqls).” statements executed \r\n”;
$count++;
sleep(1);
}

?>