Here is a simple command to auto repair, check and optimize all the tables in all databases running on a MySQL server:
mysqlcheck -u root -p --auto-repair --check --optimize --all-databases