Check and Optimize MySQL Database Automatically with Crontab/Cron

MySQL is a very popular free yet powerful database system. But even in great databases, the tables may get fragmented with overhead due to continuous update, or delete and insert operation on data stored in database. Beside, it’s also possibility that the databases may get corrupted. Thus, performing health check on database and optimize MySQL server regularly is an important task.

It’s a bit troublesome if the database administrators have to login to the server or launching the phpMyAdmin to optimize the databases one by one or table by table manually. Sometimes DB admin may simply forgets to do the job or set the frequency of optimization to lesser times. It’s recommended that every tables in MySQL databases are checked at least once a day on a busy server.

It’s possible to automate the optimization of MySQL process by using crontab function in Linux/Unix/CentOS/FreeBSD. The cron job to check and optimize MySQL databases can be created by using mysqlcheck client utility comes MySQL installation. mysqlcheck client can checks, repairs, optimizes, and analyzes tables in MySQL database.

To create a new cron job, login to the server as root or any other user, and then edit the crontab file (in most operating system, crontab -e will open crontab file in default text editor) to add in the following line of text. For users using cPanel, click on “Cron job” where you can set up crontab at daily, hourly and other interval. Experience webmasters can also set up a crontab file in rc.hourly or rc.daily or other cron directory. Note that if you login as a MySQL or normal user with no access privileges to all database, it’s not possible to optimize all databases, unless user ID and password for root is specified as in example below.

0 1 * * * mysqlcheck -Aao –auto-repair -u root -p[password] > /dev/null

The above statement has the syntax similar to “mysqlcheck [options] –all-databases”, where –all-databases parameter is the default action is no databases is specified thus can be omitted. The command will run mysqlcheck client to automatically analyze and optimize all databases at 1 am everyday. Note that there is not space between -p and your password for root. You can change the running time to your preference, and also change the options for mysqlcheck command. If you just want to check and optimize certain databases or certain tables without the database, use the following syntax:

mysqlcheck [options] db_name [tables]
mysqlcheck [options] –databases DB1 [DB2 DB3...]

You may want to remove –auto-repair switch from the above command, as a table repair operation might cause data loss under some circumstances the operation due to causes include but are not limited to filesystem errors. For those who has changed the character set and collation of MySQL databases may also need to use –default-character-set option. More information on all available switches and options available can be found here.

Share and contribute or get technical support and help at My Digital Life Forums.



4 Responses to “Check and Optimize MySQL Database Automatically with Crontab/Cron”

  1. Zubin
    January 16th, 2008 06:40
    1

    Note that -–auto-repair should have two dashes

    Also, you might want to leave “> /dev/null” out for the first few runs, to see the results of the command (if you put MAILTO=me@mydomain.com at the top of the crontab, the results will get sent there).

  2. Mike -
    May 6th, 2008 19:15
    2

    I did not know that it was nesessary to perform a regular check for mysql.

  3. php-trivandrum
    September 20th, 2008 18:34
    3

    Sure mysql does need frequent tunning, and also I have seen a lot of the servers running in the default configuration. which happens to be a very modest one. If you have access to the mysql configuration file, there are some values that can make your server breathe more often. And make your visitors more happy.

  4. tbartels
    November 16th, 2008 12:48
    4

    along the lines of what Zubin is suggesting you can use the –silent(-s) switch as well, so you only get output if there is an error, something like

    MAILTO=email@domain.com
    0 1 * * * mysqlcheck -Aaos -uroot -ppassword

    this way you will only get an email if there is an error

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

mysql check database - check mysql database - mysql cron - mysql database check - mysql optimize database - optimize mysql database - mysqlcheck crontab - optimize mysql - mysql optimize - optimize mysql db - mysql check - how to update mysql automatically - optimize table cron - check table cron - database optimizer mysql - mysql optimize table - mysql optimize cron job - check mysql database - php mysql autofix Overhead - how to repair crontab - hacer mysql check database - mysql database optimization + windows - mysql table health - cron mysql optimize - "mysqlcheck --optimize" - crontab mysqlcheck send email - crontab examples for database insertion jobs - cron job to optimize sql - crontab optimize - how to check mysql database - how to update a mysql table automatically using cron jobs - how to check database log at mysql - mysql cron-job check tables - mysql table optimize frequency - mysql optimize cronjob - mysql cron w nocy OPTIMIZE TABLE - mysql optimize all databases - mysql check datavase - mysql OPTIMIZE DB - mysqlcheck optimize - mysql +db +check - mysql cron example - optimizing mysql tables - optimize mysql repairs - optimize database mysql with tables - optimize mysql crontab - otimizar mysql com cron job - check database mysql - check a mysql database - cron job+mysql -