Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: php script for converting MyIsam tables to InnoDB  (Read 2300 times)

0 Members and 1 Guest are viewing this topic.

jominj

  • Guest
php script for converting MyIsam tables to InnoDB
« on: December 30, 2013, 07:31:54 pm »
We can convert the MyIsam tables to InnoDB using the mysql command mysql> ALTER TABLE tablename ENGINE=InnoDB

But this may become a tedious task when the number of tables is very large, We can convert MyIsam tables to InnoDB using a php script file

Create a php file and add the following code to the file
Code: [Select]
<?php
//Your database connection items here

$host 'hostname'//Specify host
$dbuser 'username'// Specify user with alter permissions 
$dbpass 'password'//user password
$mydb 'mydb'//specify schema or db to be modified


//connect to database using variables above
$link mysql_connect($host,$dbuser,$dbpass);
$db mysql_select_db($mydb);
   
if (!
$link) {
die(
'Could not connect: ' mysql_error());

}
echo 
"Connected Successfully to: $host." "\n\n";
echo 
"Using database: $mydb." "\n\n"
echo 
"Running script as $dbuser." "\n\n";


//show tables in database
$sql 'SHOW TABLES';
$rs mysql_query($sql);

echo 
$sql;
echo 
"\n";

if (!
$rs) {
die(
'SQL Recordset Error: ' mysql_error());

}
else {
//loop through tables and convert to InnoDB
while($row mysql_fetch_array($rs))
{
$tbl $row[0];
$sql "ALTER TABLE $tbl engine=InnoDB;";
mysql_query($sql);

echo 
$sql;
echo 
"\n";
}

echo 
'Operation Completed.' "\n\n";
echo 
'Confirm Storage Engine conversion using phpmyadmin ' "\n" 'or from mysql: show create table tblname.' "\n";

}
//close connection
mysql_close($link);

?>


Give execution permission to the file using the command # chmod +x file.php

Run the file using the command # php file.php