Results 1 to 10 of 10

Thread: Cron to run every day to optimize mysql?

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    140
    cPanel/WHM Access Level

    Root Administrator

    Default Cron to run every day to optimize mysql?

    Is there a cron script i can run every day to flush the fragmented tables and improve mysql performance?

  2. #2
    Member
    Join Date
    Feb 2012
    Location
    Howzit! Free Advertising Classifieds
    Posts
    22
    cPanel/WHM Access Level

    Website Owner

    Default Re: Cron to run every day to optimize mysql?

    Yes, create a db_clean.php and set it up as a cronjob....

    Code:
    <?
    include '../incl/config.php'; //// This should contain your DB connections
    	// Set font
    	echo "<font size=\"-1\" face=\"arial\">";
    	// Statement to select the databases
    	$db_select = 'SHOW DATABASES';
    	// Query mySQL for the results
    	$db_result = mysql_query($db_select);
    		// Loop through all the databases
    		while ($db_row = mysql_fetch_array($db_result)) {
    			// Select currently looped database and continue only if sucessful (have permission to)
    			If (mysql_select_db($db_row[0])) {
    				// Echo database name
    				echo "<br><b>";
    				echo $db_row[0];
    				echo "</b><br>";
    				// Statement to select the tables in the currently looped database
    				$tbl_status = 'SHOW TABLE STATUS FROM ' . $db_row[0];
    				// Query mySQL for the results
    				$tbl_result = mysql_query($tbl_status);
    					// Check to see if any tables exist within database
    					If(mysql_num_rows($tbl_result)) {
    						// Loop through all the tables
    						while ($tbl_row = mysql_fetch_array($tbl_result)) {
    							// Statement to optimize table
    							$opt_table = 'OPTIMIZE TABLE ' . $tbl_row[0];
    							// Query mySQL to optimize currently looped table
    						    $opt_result = mysql_query($opt_table);
    							// Echo table name
    							echo "&nbsp;&nbsp;<i>";
    							echo $tbl_row[0];
    							echo "</i><br>";
    						} // End table while loop 
    					} else {
    						// Alert that there are no tables within database
    							echo "&nbsp;&nbsp;<i>No Tables</i><br>";
    					} // End table existance if statement
    			} // End database if statement
    		} // End database while loop
    	// Alert them that operation was successful
    	echo "<br><br><b>Above tables successfully optimized!</b>";
    	// End font
    	echo "</font>";
    ?>

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    140
    cPanel/WHM Access Level

    Root Administrator

    Default Re: Cron to run every day to optimize mysql?

    does this do all the databases on the shared server and will remove fragmented cache?

  4. #4
    cPanel Staff cPanelTristan's Avatar
    Join Date
    Oct 2010
    Location
    somewhere over the rainbow
    Posts
    7,611
    cPanel/WHM Access Level

    Root Administrator

    Default Re: Cron to run every day to optimize mysql?

    The script is very carefully commented for each line provided. Did you read the script provided to see what it is doing? If you read each comment above each command, you can see what it does specifically.
    cPResources: Support Options | More Support Options | Forums Search | cPanel.net Site Search | Mailing Lists(Alt) | Docs
    -- Tristan, Technical Analyst III, Forums Specialist, cPanel Tech Support

    Submit a ticket | Check an existing ticket

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    41
    cPanel/WHM Access Level

    Root Administrator

    Default Re: Cron to run every day to optimize mysql?

    Quote Originally Posted by cPanelTristan View Post
    The script is very carefully commented for each line provided. Did you read the script provided to see what it is doing? If you read each comment above each command, you can see what it does specifically.
    // Select currently looped database and continue only if sucessful (have permission to)
    thats what made me also wonder if it did all.

    would a cron such as
    0 1 * * * mysqlcheck -u root -p[YOURPASSWORD] --auto-repair --optimize --all-databases] > /dev/null
    also do this?
    seems the script is much more involved.
    Last edited by dmacleo; 08-18-2012 at 07:32 PM.

  6. #6
    cPanel Staff cPanelTristan's Avatar
    Join Date
    Oct 2010
    Location
    somewhere over the rainbow
    Posts
    7,611
    cPanel/WHM Access Level

    Root Administrator

    Default Re: Cron to run every day to optimize mysql?

    The script will only check tables that can be optimized and exist. It's far more detailed and would likely take less processing power than running through them all without checking.

    It's up to you what you want to do, but it is definitely advised to always read the script comments to understand a script, since that's the purpose of having detailed comments.
    cPResources: Support Options | More Support Options | Forums Search | cPanel.net Site Search | Mailing Lists(Alt) | Docs
    -- Tristan, Technical Analyst III, Forums Specialist, cPanel Tech Support

    Submit a ticket | Check an existing ticket

  7. #7
    Member
    Join Date
    Jan 2012
    Posts
    41
    cPanel/WHM Access Level

    Root Administrator

    Default Re: Cron to run every day to optimize mysql?

    for the record I wasn't denigrating the script, it looks powerful.
    just wanted to clarify if it did all also.

  8. #8
    Member
    Join Date
    Jun 2010
    Posts
    13

    Default Re: Cron to run every day to optimize mysql?

    Looking over the script all it seems to do is optimize only

    // Statement to optimize table
    $opt_table = 'OPTIMIZE TABLE ' . $tbl_row[0];

    That seems to be the only command on SQL it actually does other then search for and loop through dbases

  9. #9
    Member
    Join Date
    Feb 2012
    Location
    Howzit! Free Advertising Classifieds
    Posts
    22
    cPanel/WHM Access Level

    Website Owner

    Default Re: Cron to run every day to optimize mysql?

    dmacleo, in short...
    Setting up this script as a cronjob to run at a specific time each day will optimize all db's on your server.

  10. #10
    Member
    Join Date
    Jun 2010
    Posts
    13

    Default Re: Cron to run every day to optimize mysql?

    The script will optimize, the command line will also repair and optimize.
    One is PHP the other is Shell
    One has customized reporting the other is default

    When I run the command line it skips all optimized tables and only does ones needing it, goes very fast but gives me a list of all the skipped ones + ones that are done.

Similar Threads

  1. weekly/monthly backups to run a particular day
    By alexkb in forum Data Protection
    Replies: 1
    Last Post: 02-08-2010, 08:52 PM
  2. Cron <root@xxx> run-parts /etc/cron.hourly
    By mmarch in forum New User Questions
    Replies: 0
    Last Post: 03-31-2009, 11:45 PM
  3. Replies: 3
    Last Post: 03-29-2007, 04:31 AM
  4. Cron run-parts /etc/cron.daily
    By Nitro in forum cPanel & WHM Discussions
    Replies: 0
    Last Post: 09-26-2006, 10:31 AM
  5. Cron <@ls05> run-parts /etc/cron.daily
    By GeekPatrolMille in forum cPanel & WHM Discussions
    Replies: 2
    Last Post: 03-17-2004, 02:30 PM