To optimize a mysql database table via command line you will need to ssh into your server as the root user and enter into the mySQL command line interface by typing "mysql". This will give you the following prompt.
mysql>
Once you are at a mySQL command prompt you will need to type the following. The first selects the database you wish to use and the second optimizes the table(s).
mysql> USE your_database;
Database changed
mysql> OPTIMIZE TABLE `table1` , `table2` , `table3` , `table4`;