Optimize the shell script _ Mysql of the MySQL data table

Source: Internet
Author: User
Tags mysql manual
Optimize the shell script bitsCN.com of the Mysql data table because the data volume in the company database is large, optimize the data table in the company's mysql database on a regular basis (the optimize description is as follows ), there are more than 300 data tables in the database. manual operations are obviously not realistic. the efficiency of script execution is still very good. The script is as follows:

The OPTIMIZE description in the mysql manual:

OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name]…

If you have deleted a majority of tables, or if you have made many changes to a table containing variable-length rows (tables containing VARCHAR, BLOB, or TEXT columns), you should use

Optimize table. The deleted records are kept in the link list. Subsequent INSERT operations will reuse the old record location. You can use optimize table again

Use unused space and organize data file fragments.

Usage: sh optimize. sh word

[Root @ shellec shell] #

#! /Bin/sh

Time_log =/opt/optimize_time

Sum = $ #

If ["$ sum"-eq 0]

Then

Echo "Error: no parameter chosed"

Exit 1

Fi

For I in $ *; do

Echo "optimize database $ I starting ..."

Tables = $ (/usr/bin/mysql $ I-udevuser-pdevuser-e "show tables" | grep-v "Tables">/opt/$ I)

Tablelist = $ (cat/opt/$ I)

Echo "optimize database $ I starting ......"> $ time_log

Echo "$ I start at $ (date + [% Y/% m/% d/% H: % M: % S])" >>$ time_log

For list in $ tablelist

Do

Echo $ list

/Usr/bin/mysql $ I-utaobao-padmin-e "optimize table $ list"

Done

Echo "$ I end at $ (date + [% Y/% m/% d/% H: % M: % S])" >>$ time_log

Echo> $ time_log

Done

BitsCN.com

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.