Narendra Dhami

My Site

Typical automated MySQL maintenance jobs

Posted by Narendra Dhami on August 26, 2008

The following maintenance jobs are typically run against a MySQL database:

  • Backup
  • Clean-up binary logs.
  • Optimize tables
  • Purge query cache
  • Rotate binary logs
  • Backup
    A backup is not a typical maintenance job. But it behaves more or less like one. The backup should be done regularly depending on the restore/PITR (Point in Time Recovery) requirements.

    Make sure, that in the backup all the necessary files (data files, transaction log files, configuration files and binary log files) are included. To prove that the backup process is working properly a regular restore should be performed. This can ideally be combined with the set-up of new database instances for developers or testing.
    Clean-up the binary logs

    The binary logs can be cleaned-up in two ways:

    a) Passive by MySQL itself:
    # my.cnf
    expire_logs_days = 7

    b) Active by the customers environment:
    mysql> PURGE MASTER LOGS TO ‘binarylog.000999’;
    mysql> PURGE MASTER LOGS BEFORE ‘2008-07-29 22:46:26’;

    Make sure NO binary logs are purged which are still needed by a slave. In this situation the slave is lost and has to be set-up from scratch.
    Make also sure binary logs are not removed by a file system operation (rm bin-log.*). Otherwise the database gets confused.

    Optimize table
    After large UPDATE or INSERT/DELETE operations or long time tables are blown up and contain a lot of unused space. This unused space can be partially reclaimed by optimizing the table again.
    mysql> OPTIMIZE TABLE ;

    This operation internally copies the whole table and therefore can take a long time!

    Purge query cache
    When there are SELECT queries with different sizes of result sets the query cache gets de-fragmented. This is shown by a lot of free space in the query cache but also a lot of not cached queries. Here it makes sense to purge the query cache from time to time.

    Binlog rotate
    Binary logs can only be rotated by size. Sometimes you want to have them rotated by time. You can do this as follows (for example with a cron job):
    mysql> FLUSH LOGS;


    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )


    Connecting to %s

    %d bloggers like this: