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
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
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
You can use MySQL to script it and execute it:
dbname
to MyISAMCONVERT_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}
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.
dbname
to MyISAM and not replicate to other serversCONVERT_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}
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 !!!
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!
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