For a single MySQL table and a batch table conversion engine script convert_engine.sh

Source: Internet
Author: User

The company's recent MySQL always has a large number of lock tables. After analysis, it basically uses the MYISAM Table engine. A large number of reads and writes to a table by MYISAM will cause the entire MySQL table to be locked, as a result, dynamic content cannot read data in a timely manner, which has a huge impact on the user experience. The principle of INNODB is to lock a single row record (row lock) of the table, without affecting other row records in the same table. And write the following SHELL script to convert a single table and the whole database engine...

#! /Bin/sh

# Arg1:-d dbname
# Arg2:-t [tables]
# Arg3:-e engine type (myisam | innodb)

User = "root"
Pwd = "666666"
MYSQLbin = "/usr/local/mysql/bin/mysql-u $ User-p $ Pwd-e"
TmpFile = '/tmp/table. tmp'
Usage ()
{
Echo "Usage (): $0-d dbname [-t tbname]-e engine (myisam | innodb )"
}
If [$ #-eq 0]; then
Usage
Exit 1
Fi
While getopts d: t: e: h OPTION
Do
Case $ OPTION in
D)
{
DBName = $ OPTARG
DBExists = '$ MYSQLbin "show databases;" | grep "$ DBName "'
If ["$ DBExists" = ""]; then
Echo "$ DBName database not exists! "
Exit 1
Fi
};;
T)
{
TBName = $ OPTARG
TBExists = '$ MYSQLbin "use $ DBName; show tables" | grep $ tbname'
If ["$ TBExists" = ""]; then
Echo "$ TBName table not exists! "
Exit 1
Fi
};;
E)
{
EngineName = 'echo $ OPTARG | tr A-Z a-Z'
If ["$ EngineName "! = "Myisam"] & ["$ EngineName "! = "Innodb"]; then
Usage
Echo "Engine $ EngineName is no exists! "
Exit 1
Fi
};;
? | H)
Usage
Exit 0
;;
Esac
Done

If ["$ EngineName" = ""]; then
Usage
Echo "Lose '-e (innodb | myisam )'! "
Exit 1
Fi

If ["$ TBName "! = ""]; Then
CurrentEngine = '$ MYSQLbin "use $ DBName; show table status like' $ tbname' \ G" | grep Engine | awk '{print $2}' | tr A-Z a-Z'
If ["$ CurrentEngine" = "$ EngineName"]; then
Echo-e "\ 033 [31 m Current Table $ TBName is already of type $ EngineName; Ignored! \ 033 [0 m"
Exit 0
Fi
$ MYSQLbin "use $ DBName; alter table $ TBName engine = $ EngineName"
Else
$ MYSQLbin "use $ DBName; show tables" | sed 1d> $ TmpFile
While read Table
Do
CurrentEngine = '$ MYSQLbin "use $ DBName; show table status like' $ table' \ G" | grep Engine | awk '{print $2}' | tr A-Z a-Z'
If ["$ CurrentEngine" = "$ EngineName"]; then
Echo-e "\ 033 [31 m Current Table $ Table is already of type $ EngineName; Ignored! \ 033 [0 m"
Else
$ MYSQLbin "use $ DBName; alter table $ Table engine = $ EngineName ;"
Echo-e "\ 033 [32 m $ DBName Table $ Table Convert $ EngineName is Sucessfull! \ 033 [0 m"
# Continue
Fi
Done <$ TmpFile
Fi

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.