对MySQL单个表和批量表转换引擎脚本convert_engine.sh

php中文网
发布: 2016-06-07 16:57:52
原创
1181人浏览过

公司最近的MySQL总是大量的锁表,分析了一下,基本上都是用的MYISAM表引擎,MYISAM在一张表里大量的读写会造成MySQL整张表都锁死

公司最近的MySQL总是大量的锁表,分析了一下,基本上都是用的MYISAM表引擎,MYISAM在一张表里大量的读写会造成MySQL整张表都锁死,而造成动态内容不能及时读数据,,给用户体验带来巨大的影响。INNODB的工作原理只是锁表的单行记录(行锁),不会影响同一张表内的其他行记录。与是写下了以下SHELL脚本,可单个表和整数据库的引擎转换...

#!/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[31m Current Table $TBName is already of type $EngineName;Ignored! \033[0m"
     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[31m Current Table $Table is already of type $EngineName;Ignored! \033[0m"
    else
        $MYSQLbin "use $DBName;alter table $Table engine=$EngineName;"
        echo -e "\033[32m $DBName Table $Table Convert $EngineName is  Sucessfull! \033[0m"
#  continue
           fi
 done  fi

linux

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号