13

Previously, I use this:

USE dbname;
ALTER TABLE tablename ENGINE=MYISAM;

I'm looking for simpler way to convert all tables in a database, rather than writing every table name one by one

  • 1
    Simpler how? That looks pretty simple to me. – Zoredache Mar 10 '11 at 15:58
  • 5
    Learn to use scripting to accomplish your goals. – Tom O'Connor Mar 10 '11 at 16:15
11

I'm not aware of any way to do this in mysql itself, but a simple shell script will do the job:

TABLES=$(mysql -pXXXXXXX -uXXXXXXX --skip-column-names -B -D $DB -e 'show tables')
for T in $TABLES
do
    mysql -pXXXXX -uXXXXX -D $DB -e "ALTER TABLE $T ENGINE=MYISAM"
done
  • 1
    how to disable foreign key check ? – realtebo Jun 14 at 12:34
7

You can use MySQL to script it and execute it:

This will convert every InnoDB table in database dbname to MyISAM

CONVERT_SCRIPT=Convert_dbname_InnoDB_to_MyISAM.sql
mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' ENGINE=MyISAM;') FROM information_schema.tables WHERE table_schema='dbname' AND engine='InnoDB';" > ${CONVERT_SCRIPT}
mysql -u... -p... -A < ${CONVERT_SCRIPT}

This will convert every InnoDB table to MyISAM

CONVERT_SCRIPT=Convert_InnoDB_to_MyISAM.sql
mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' ENGINE=MyISAM;') FROM information_schema.tables WHERE engine ='InnoDB';" > ${CONVERT_SCRIPT}
mysql -u... -p... -A < ${CONVERT_SCRIPT}

If you do not want the conversion of the tables to be replicated to Slaves, just put SET SQL_LOG_BIN=0; as the first line. That way, you can test the conversion in a Master/Slave setup by converting only the Slave first and then the Master later.

This will convert every InnoDB table in database dbname to MyISAM and not replicate to other servers

CONVERT_SCRIPT=Convert_dbname_InnoDB_to_MyISAM.sql
echo "SET SQL_LOG_BIN=0;" > ${CONVERT_SCRIPT}
mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' ENGINE=MyISAM;') FROM information_schema.tables WHERE table_schema='dbname' AND engine='InnoDB';" >> ${CONVERT_SCRIPT}
mysql -u... -p... -A < ${CONVERT_SCRIPT}

This will convert every InnoDB table to MyISAM and not replicate to other servers

CONVERT_SCRIPT=Convert_InnoDB_to_MyISAM.sql
echo "SET SQL_LOG_BIN=0;" > ${CONVERT_SCRIPT}
mysql -u... -p... -AN -e"SELECT CONCAT('ALTER TABLE ',table_schema,'.',table_name,' ENGINE=MyISAM;') FROM information_schema.tables WHERE engine ='InnoDB';" >> ${CONVERT_SCRIPT}
mysql -u... -p... -A < ${CONVERT_SCRIPT}

Give it a Try !!!

  • Going from MyIsam2InnoDB in real world you'll probably want SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; somewhere to bypass the no_zero_date since mysql 5.7 - Source stackoverflow.com/questions/9192027/… – Antony Gibbs Aug 2 '18 at 21:07
  • Going from MyIsam2InnoDB in real world you'll probably want SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; somewhere to bypass the no_zero_date since mysql 5.7 - Source stackoverflow.com/questions/9192027/… – Antony Gibbs Aug 2 '18 at 21:07
0

For those who still having this issue, you can follow this way to do it, I found this answer in a website. It helps me a lot:

shell> mysql -u username -p -e "SELECT concat('ALTER TABLE ', TABLE_NAME,' ENGINE=MYISAM;') FROM Information_schema.TABLES WHERE TABLE_SCHEMA = 'db_name' AND ENGINE = 'InnoDB' AND TABLE_TYPE = 'BASE TABLE'" | tail -n+2 >> alter.sql

Update username and db_name values with your own values.

Afet executing the script, it will save a file under name: alter.sql Open the file and execute the content on your phpmyadmin or mysql command line.

Cheers!

-1

I prefer one-liners for this type of stuff. This is a one-liner version of the most accepted answer.

This works if you have your MySQL username and password set in ~/.my.cnf.

D=your_db; for t in $(mysql $D -B -e 'SHOW TABLES'); do mysql $D -e "ALTER TABLE $t ENGINE=MyISAM"; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.