首页 >后端开发 >php教程 >php批量转换Mysql表引擎

php批量转换Mysql表引擎

巴扎黑
巴扎黑原创
2016-11-24 10:28:201198浏览

<?php
/**
 * 批量转换Mysql表引擎
 */
error_reporting(E_ALL);
// 数据库连接配置
$host     = &#39;localhost&#39;;
$username = &#39;root&#39;;
$passwd   = &#39;&#39;;
$database = &#39;test&#39;;
// 要转换的库名配置,多库转换增加配置元素即可
$configs  = array($database);
// 转换配置
$convert_rule = array(
    &#39;from&#39; => &#39;InnoDB&#39;,
    &#39;to&#39;   => &#39;MyISAM&#39;
);
mysql_engine_convert();
/**
 * 转换函数
 */
function mysql_engine_convert()
{
    global $host,$username,$passwd,$configs,$convert_rule;
    if ( ($conn = mysql_connect($host, $username, $passwd)) !== false)
    {
        foreach ($configs as $db_name)
        {
            mysql_select_db($db_name) or exit(&#39;Not found db: &#39;. $db_name);
            $tables = mysql_query("SHOW FULL TABLES");
            while ($table = mysql_fetch_row($tables))
            {
                if ($table[1] === &#39;VIEW&#39;) continue;
                $sql = "SHOW TABLE STATUS from {$db_name} where Name=&#39;{$table[0]}&#39; ";
                if ($result = mysql_query($sql))
                {
                    $table_status = mysql_fetch_row($result);
                    
                    if (strtolower($table_status[1]) == strtolower($convert_rule[&#39;from&#39;]))
                        mysql_query("ALTER TABLE {$table[0]} ENGINE = {$convert_rule[&#39;to&#39;]}");
                }
            }
            echo $db_name,&#39;:All tables ENGINE is &#39;,$convert_rule[&#39;to&#39;],"\n";
        }
        
    } else {
        echo "db error\n";
    }
}


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn