Establish routine maintenance specifications for MySQL Databases (1)

Source: Internet
Author: User

Related Articles:

How to perform MySQL database table Fault Detection

How to fix MySQL database tables

How to lock MySQL database tables

How to maintain MySQL Database Log Files

Create a database table maintenance Specification

It is a good idea to perform a database table check on a regular basis rather than when a problem occurs. Taking into account the establishment of a schedule for preventive maintenance to assist with automatic problems, you can take measures to correct them:

Perform regular database backup and Allow Log updates.

Schedule regular table checks. Using the checklist will reduce the chances of using backup. In Windows, scheduled tasks are used. Unix cron jobs are generally called from the crontab file of the account used by the running server.

For example, if you run the server as a mysql user, you can create a regular check from the mysql crontab file. If you do not know how to use cron, use the following command to view the relevant Unix manual page:

$man cron$man crontab

The database table is checked during system boot before the server is started. And it may be restarted due to an early crash. If this is the case, the database table may have been destroyed and should be thoroughly checked.

Create a script for regular maintenance

To run an automatic table check, you can write a script to change the directory to the server data directory and perform myisamchk and isamchk on all database tables. If you only have a MyISAM table or an ISAM table, you only need one program. You can comment out the unrelated program from the script.

This tutorial can be called by cron or during system startup.

For maintenance purposes, you can use myisamchk-s to check the table. The-s, -- silent option enables myisamchk and isamchk to run in silent mode. Only messages are printed when an error occurs. In addition, myisamchk supports the -- fast option, which allows the program to skip people and tables that have not been modified since the last check.

1. A simple script

For example, a simple script that is easy to understand checks all tables in the server directory and changes DATADIR to a proper value for your system ):

#!/bin/sh
cd DATADIR
myisamchk  --silent  --fast  */*.MYI
isamchk  --silent  */*.ISM

2. A complicated script

A potential problem with this script: if there are many tables, the wildcard mode '*/*. myi' and '*/*. ISM 'may be unavailable because "too has arguments (too many parameters)" or the command line exceeds the length allowed by shell. The script can be further modified to the same, and DATADIR can be changed to a value suitable for your system ):

#!/bin/sh
datadir=DATADIR
find $dtatdir –name “*. MYI” -print | xargs myisamchk  --silent  --fast
find $dtatdir –name “*. ISM” -print | xargs isamchk  --silent

You can also specify multiple database directories in the script.

3. How to execute the script

If you save the script as check_tables, make sure it is executable. Of course, we recommend that you switch to the dedicated mysql instance first:

$ Su mysql
$ Vi check_tables (edit the script, you can also use your favorite editor)
$ Chmod + x check_tables

Run the command manually to check whether your script has an error:

$check_tables

Ideally, no results are output. If the system does not support External Locking, the visitor's server will change it during your checklist. In this case, the script may report the tables that are not actually correct. If the system supports External Locking, this problem will not occur.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.