It’s a simple one liner:

mysqlcheck -p --auto-repair --check --optimize --all-databases
Obviously if your current user doesn’t have the rights, you’ll need to use a user that does.