再看MySQL手册,看到有关修改存储引擎的部分,隧想到能否用shell脚本实现批量修改,于是便有了下面的脚本,以把MyISAM转换为InnoDB为例。
实验环境如下:
OS: CentOS 5.8 Final
MySQL Version:5.5.19
脚本内容如下:
- #/bin/bash
- #FileName:Convert_Storage_Engine.sh
- #Desc:Conversion of a MySQL tables to other storage engines
- #Create By:fedoracle
- #Date:2012/06/27
- DB=new
- USER=test
- PASSWD=test
- HOST=192.168.25.121
- MYSQL_BIN=/usr/local/mysql/bin
- S_ENGINE=MyISAM
- D_ENGINE=InnoDB
- #echo "Enter MySQL bin path:"
- #read MYSQL_BIN
- #echo "Enter Host:"
- #read HOST
- #echo "Enter Uesr:"
- #read USER
- #echo "Enter Password:"
- #read PASSWD
- #echo "Enter DB name :"
- #read DB
- #echo "Enter the original engine:"
- #read S_ENGINE
- #echo "Enter the new engine:"
- #read D_ENGINE
- $MYSQL_BIN/mysql -h$HOST -u$USER -p$PASSWD $DB -e "select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='"$DB"' and ENGINE='"$S_ENGINE"';" | grep -v "TABLE_NAME" >tables.txt
- for t_name in `cat tables.txt`
- do
- echo "Starting convert table $t_name......"
- sleep 1
- $MYSQL_BIN/mysql -h$HOST -u$USER -p$PASSWD $DB -e "alter table $t_name engine='"$D_ENGINE"'"
- if [ $? -eq 0 ]
- then
- echo "Convert table $t_name ended." >>con_table.log
- sleep 1
- else
- echo "Convert failed!" >> con_table.log
- fi
- done
测试过程如下:
#############################################################################################
有些表在转换的时候由于字符集,字段长度,外键约束等原因会出现一些问题,如下
- ERROR 1217 (23000) at line 1: Cannot delete or update a parent row: a foreign key constraint fails
- ERROR 1071 (42000) at line 1: Specified key was too long; max key length is 1000 bytes
阅读(659) | 评论(0) | 转发(0) |