The following command will optimize all databases in mysql (excpect those that don't support it due to their storage engine).

mysqlcheck -Aao

You can throw the

--auto-repair

flag in if you are brave.