CRON JOB to Repair and Optimize Database

In this short articles we will learn how to automatically repair and optimize our database using Cron Job. Cron Job is most likely scheduler, mean it will run automatically after we setup it. I believe not many of us know how to using this features, We can use it to run periodically some PHP script we made. Example a sitemap generator script which may consume to many resource if we run it manually, we can set it run in schedule follow settings on Cron Job.

In this short articles I will give sample how we can use Cron Job to repair and optimize database automatically. Sometimes when you open your database using PHPmyadmin you may see some database corrupt. We need to repair it manually and optimize it then our database will run smoothly again, the impact is our website will run faster. The problem are when we have not much time on focus on this problem, or we manage to much database which may make us frustrating to work on it manually.

Alright enough for the bad explanation, here is a sample how to use Cron Job. First we need to coding some PHP script to follow what we need. In this sample I created an PHP script to repair and optimize my database.

mysql_connect(“localhost“, “DBUSER“, “DBPASS“) or die(mysql_error());
mysql_select_db(“DBNAME“) or die(mysql_error());
mysql_query(“REPAIR TABLE `TABLE1`, `TABLE2`, `TABLE3`”);
mysql_query(“OPTIMIZE TABLE `TABLE1`, `TABLE2`, `TABLE3`”);

I assume all of you already understand the code so let’s continue this articles. This sample script will repair and optimize our database when we open it via web browser. Next, we need to add it to cron job to run it follows the settings we made, so we don’t need to open it manually to repair and optimize database.


From your hosting cPanel, Click on Cron Job, Next choose the setting for run this job, you can choose common settings:

Next in command form type your/php/bin -q /your/path/to/file.php you need to know your php bin and your real path to file. It should works if you’re not set it wrong.

Cron job also can be used for another automation works. Example: backup your files periodically, backup your database periodically, generate your sitemap periodically, etc. All you need is coding that PHP code first then setup the cron job.

That share for today, Have a nice day 🙂

Similar Posts:


1 thought on “CRON JOB to Repair and Optimize Database”

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.