Is there any command to optimize all MySQL tables one by one automatically?
I usually use mysqlcheck with --optimize and --databases combination. mysqlcheck is a command-line interface for administrators to check, optimize and repair tables.
mysqlcheck -uroot -p --optimize --databases myDatabase
Here is the MySQL doc : http://dev.mysql.com/doc/refman/5.5/en/mysqlcheck.html
Max.
You can use information schema to generate the statements you need:
SELECT Concat('OPTIMIZE TABLE ',TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema='yourdb';