Home   >>   MySQL   >>   Optimize MySQL Database With/Via Cron
Optimize MySQL Database With/Via Cron PDF Print E-mail
( 2 Votes )
How To - MySQL
Written by Christian Foronda   
Friday, 29 October 2010 11:30

Create a new user with access to SELECT and INSERT that will run the cron job:

	mysql> GRANT SELECT, INSERT ON *.* TO 'optimizer'@'localhost' identified by 'HardPassword';
mysql> flush privileges;
mysql> exit;

Add to cron job of whoever user:

	# crontab -e
# MySQL weekly optimisation (run every sunday,12am)
0 0 * * 0 /usr/bin/mysqlcheck -Aaos -u optimizer -pHardPassword > /dev/null 2>&1

Option meaning:

-A - Check all the databases.
-a - Analyze the given tables.
-o - Optimize tables.
-s - Print only error messages.

For busy database, it's recommended to optimize the tables once a day.

	# crontab -e
# MySQL daily optimisation (run everyday at 3am)
0 03 * * * /usr/bin/mysqlcheck -Aaos -u optimizer -pHardPassword > /dev/null 2>&1

Get an email if there is an error:

	# crontab -e
# MySQL daily optimisation (run everyday at 3am)
MAILTO=YourEmailAddress
0 03 * * * /usr/bin/mysqlcheck -Aaos -u optimizer -pHardPassword > /dev/null 2>&1



blog comments powered by Disqus
Last Updated on Friday, 29 October 2010 11:51