Jump to content

Recommended Posts

Posted

Nevermind, I finally found something in Google after 2 hours of searching with multiple combinations of keywords. If anyone ever wonders how to do this in the future, here is the why and how.

 

Why?: Maybe your database builds up pretty high overhead pretty quick.

 

How?:

 

1. Create a directory outside your public_html folder called "cron_scripts". I created mine at my account root level.

2. Using your favorite editor, copy and paste the following code, modifying the "host", "dbusername" and "dbpassword" to whatever is appropriate for your account:

 

><?php
$db = mysql_connect("host", "dbusername", "dbpassword"); 
$sql = "SHOW DATABASES"; 
$dbs_result = mysql_query($sql, $db); 
if(mysql_num_rows($dbs_result)) 
{ 
while($dbs_row=mysql_fetch_assoc($dbs_result)) 
{ 
$database = $dbs_row["Database"]; 
echo "\n\nOptimizing database $database : \n"; 
mysql_select_db($database, $db); 
$sql = "SHOW TABLE STATUS"; 
$tbls_result = mysql_query($sql, $db); 
if(mysql_num_rows($tbls_result)) 
{ 
while($tbls_row=mysql_fetch_assoc($tbls_result)) 
{ 
$TableName = "`".$tbls_row["Name"]."`";  
$sql = "OPTIMIZE TABLE ".$TableName; 
echo "\n".$sql; 
mysql_query($sql, $db); 
} 
} 
} 
} 
echo "\n\n"; 
mysql_close($db);
?>

 

3. Name the file "optimize.php" and upload to the "cron_scripts" directory we created earlier.

4. Now just go into cPanel and create a Cron Job with what ever schedule you want and a command line of the following nature, again changing "youraccount" to whatever is appropriate for your account:

 

>/usr/local/bin/php /home/youraccount/cron_scripts/optimize.php

 

And that's all folks! I hope this helps someone else in the future. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...