TECH / Servers & Websites 2 min read

How to Convert MyISAM Tables to Aria in MariaDB

Antonio NEMCD Author
Published Sep 2, 2019 · 2 min

Years ago we migrated our sites from MySQL to MariaDB and quickly decided to move legacy MyISAM tables over to the crash-safe Aria storage engine. Unlike MyISAM, Aria uses transactional logging and page checksums, making it far more resilient after unexpected power loss or server reboots.

MariaDB logo

Below are two clean ways to batch-convert all MyISAM tables to Aria: pure SQL right from the console (the safest route) and a modernized PHP 8 mysqli script.

Back up before converting: run mariadb-dump -u root -p dbname > dbname_backup.sql (or mysqldump) first. Also keep in mind that for high-concurrency transactional workloads in MariaDB, InnoDB is usually the best default, while Aria shines as a drop-in crash-safe replacement for MyISAM.

Method 1. Generate and Run SQL Statements (Recommended)

The cleanest approach is to query information_schema for every MyISAM table in your database and generate ready-to-run ALTER TABLE statements:

SET @DATABASE_NAME = 'mastertest';

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

Review the generated list and execute it in your SQL client or phpMyAdmin. If you have SSH access, you can pipe the output straight into MariaDB in a single line:

mariadb -u root -p -NBe "SELECT CONCAT('ALTER TABLE \`', table_name, '\` ENGINE=Aria;') FROM information_schema.tables WHERE table_schema='mastertest' AND ENGINE='MyISAM' AND TABLE_TYPE='BASE TABLE';" | mariadb -u root -p mastertest

Method 2. Batch Conversion via PHP (mysqli)

Originally we used a quick PHP helper for converting several databases. Since legacy mysql_connect functions were removed long ago, here is an updated PHP 8 mysqli version that converts only MyISAM tables while leaving InnoDB tables untouched:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$host = 'localhost';
$user = 'username';
$pass = 'password';
$db   = 'database';

$mysqli = new mysqli($host, $user, $pass, $db);

$sql = "SELECT table_name FROM information_schema.tables
        WHERE table_schema = ? AND ENGINE = 'MyISAM' AND TABLE_TYPE = 'BASE TABLE'";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $db);
$stmt->execute();
$res = $stmt->get_result();

while ($row = $res->fetch_assoc()) {
    $table = str_replace('`', '``', $row['table_name']);
    $mysqli->query("ALTER TABLE `{$table}` ENGINE=Aria");
    echo "Converted: {$row['table_name']}\n";
}
echo "Done.\n";
?>

Run the script from the CLI via php convert_aria.php so large tables don’t hit web server timeouts, and delete the file containing database credentials as soon as the conversion finishes.

Related posts:

Leave a Comment