欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php批量转换Mysql表引擎

程序员文章站 2023-12-28 22:50:34
...
<?php
/**
 * 批量转换Mysql表引擎
 */
error_reporting(E_ALL);
// 数据库连接配置
$host     = 'localhost';
$username = 'root';
$passwd   = '';
$database = 'test';
// 要转换的库名配置,多库转换增加配置元素即可
$configs  = array($database);
// 转换配置
$convert_rule = array(
    'from' => 'InnoDB',
    'to'   => 'MyISAM'
);
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('Not found db: '. $db_name);
            $tables = mysql_query("SHOW FULL TABLES");
            while ($table = mysql_fetch_row($tables))
            {
                if ($table[1] === 'VIEW') continue;
                $sql = "SHOW TABLE STATUS from {$db_name} where Name='{$table[0]}' ";
                if ($result = mysql_query($sql))
                {
                    $table_status = mysql_fetch_row($result);
                    
                    if (strtolower($table_status[1]) == strtolower($convert_rule['from']))
                        mysql_query("ALTER TABLE {$table[0]} ENGINE = {$convert_rule['to']}");
                }
            }
            echo $db_name,':All tables ENGINE is ',$convert_rule['to'],"\n";
        }
        
    } else {
        echo "db error\n";
    }
}

上一篇:

下一篇: