我知道我可以单独发出一个alter表,将表存储从MyISAM更改为InnoDB。

我想知道是否有一种方法可以快速将它们全部更改为InnoDB?


当前回答

<?php

// Convert all MyISAM tables to INNODB tables in all non-special databases.
// Note: With MySQL less than 5.6, tables with a fulltext search index cannot be converted to INNODB and will be skipped.

if($argc < 4)
    exit("Usage: {$argv[0]} <host> <username> <password>\n");
$host = $argv[1];
$username = $argv[2];
$password = $argv[3];

// Connect to the database.
if(!mysql_connect($host, $username, $password))
    exit("Error opening database. " . mysql_error() . "\n");

// Get all databases except special ones that shouldn't be converted.
$databases = mysql_query("SHOW databases WHERE `Database` NOT IN ('mysql', 'information_schema', 'performance_schema')");
if($databases === false)
    exit("Error showing databases. " . mysql_error() . "\n");

while($db = mysql_fetch_array($databases))
{
    // Select the database.
    if(!mysql_select_db($db[0]))
        exit("Error selecting database: {$db[0]}. " . mysql_error() . "\n");
    printf("Database: %s\n", $db[0]);

    // Get all MyISAM tables in the database.
    $tables = mysql_query("SHOW table status WHERE Engine = 'MyISAM'");
    if($tables === false)
        exit("Error showing tables. " . mysql_error() . "\n");

    while($tbl = mysql_fetch_array($tables))
    {
        // Convert the table to INNODB.
        printf("--- Converting %s\n", $tbl[0]);
        if(mysql_query("ALTER TABLE `{$tbl[0]}` ENGINE = INNODB") === false)
            printf("--- --- Error altering table: {$tbl[0]}. " . mysql_error() . "\n");
    }
}

mysql_close();

?>

其他回答

对于转换MySql表存储引擎有许多方法:

使用MySql命令如下,转换为innodb (ALTER TABLE t1 ENGINE = innodb)或(ALTER TABLE t1 ENGINE = MyISAM)为MyISAM(你应该为每个单独的表这样做,t1是表名。) 编写一个在所有表上循环的脚本,并运行alter命令 使用一个已经可用的脚本来处理这个问题:https://github.com/rafihaidari/convert-mysql-tables-storage-engine

这是一个简单的php脚本。

<?php
    @error_reporting(E_ALL | E_STRICT);
    @ini_set('display_errors', '1');


    $con = mysql_connect('server', 'user', 'pass');
    $dbName = 'moodle2014';

    $sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$dbName."';";
    $rs = mysql_query($sql, $con);

    $count = 0;
    $ok = 0;
    while($row = mysql_fetch_array($rs)){
            $count ++;
            $tbl = $row[0];
            $sql = "ALTER TABLE ".$dbName.".".$tbl." ENGINE=INNODB;";
            $resultado = mysql_query($sql);
            if ($resultado){
                    $ok ++;
                    echo $sql."<hr/>";
            }
    }
    if ($count == $ok){
            echo '<div style="color: green"><b>ALL OK</b></div>';
    }else{
            echo '<div style="color: red"><b>ERRORS</b>Total tables: '.$count.', updated tables:'.$ok.'</div>';
    }

运行此SQL语句(在MySQL客户端、phpMyAdmin或任何地方)检索数据库中的所有MyISAM表。

将name_of_your_db变量的值替换为您的数据库名称。

SET @DATABASE_NAME = 'name_of_your_db';

SELECT  CONCAT('ALTER TABLE `', table_name, '` ENGINE=InnoDB;') AS sql_statements
FROM    information_schema.tables AS tb
WHERE   table_schema = @DATABASE_NAME
AND     `ENGINE` = 'MyISAM'
AND     `TABLE_TYPE` = 'BASE TABLE'
ORDER BY table_name DESC;

然后,复制输出并作为一个新的SQL查询运行。

这很简单。只有两步。

复制,粘贴并运行: SET @DATABASE_NAME = ' name_your_db '; SELECT CONCAT('ALTER TABLE ", table_name, ' ENGINE=InnoDB;') AS sql_statements FROM information_schema。TABLE AS tb WHERE ' ENGINE ' = 'MyISAM' AND ' TABLE_TYPE ' = 'BASE TABLE'

(复制粘贴所有结果在SQL选项卡)

将所有结果复制到SQL选项卡并在下面一行中粘贴。 开始事务; 提交;

例如:

START TRANSACTION;
ALTER TABLE `admin_files` ENGINE=InnoDB;
COMMIT;

在mysql中,你可以使用文本编辑器进行搜索/替换:

SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'myisam';

注意:你可能应该忽略information_schema和mysql,因为“mysql和information_schema数据库,实现了一些mysql内部,仍然使用MyISAM。特别是,你不能切换授权表来使用InnoDB。”(http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html)

在任何情况下,请注意要忽略并运行的表:

SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'myisam';

现在只需复制/粘贴列表到你的文本编辑器,搜索/替换“|”与“ALTER TABLE”等。

然后你会有一个这样的列表,你可以简单地粘贴到你的mysql终端:

ALTER TABLE arth_commentmeta           ENGINE=Innodb;
ALTER TABLE arth_comments              ENGINE=Innodb;
ALTER TABLE arth_links                 ENGINE=Innodb;
ALTER TABLE arth_options               ENGINE=Innodb;
ALTER TABLE arth_postmeta              ENGINE=Innodb;
ALTER TABLE arth_posts                 ENGINE=Innodb;
ALTER TABLE arth_term_relationships    ENGINE=Innodb;
ALTER TABLE arth_term_taxonomy         ENGINE=Innodb;
ALTER TABLE arth_terms                 ENGINE=Innodb;
ALTER TABLE arth_usermeta              ENGINE=Innodb;

如果你的文本编辑器不能很容易地做到这一点,这里有另一个解决方案,为一个类似的列表(你可以粘贴到mysql),你的数据库只有一个前缀,从linux终端:

mysql -u [username] -p[password] -B -N -e 'show tables like "arth_%"' [database name] | xargs -I '{}' echo "ALTER TABLE {} ENGINE=INNODB;"