Changing the engine for MySQL tables from MyISAM to InnoDB

Changing the engine for MySQL tables from MyIsam to InnoDB happens in two steps:

  1. Build a query based on the existing data
  2. Run the new query.

Doing this in the mysql console is inconvenient, since the results come back as rows with pipes (the | character) at the start and end of each line. For databases with 100+ tables that’s a pain.

Run this query in PhpMyAdmin:

SET @DATABASE_NAME = 'db_name';

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;

Copy the result and paste it into a new query, ideally wrapped in:

begin transaction;
paste the result here
commit transaction;

Categories:

Updated: