MySQL批量修改存储引擎之shell脚本篇

4852阅读 0评论2012-06-27 xiaozhenggang
分类:Mysql/postgreSQL

再看MySQL手册,看到有关修改存储引擎的部分,隧想到能否用shell脚本实现批量修改,于是便有了下面的脚本,以把MyISAM转换为InnoDB为例。
实验环境如下:
OS: CentOS 5.8 Final
MySQL Version:5.5.19

脚本内容如下:

点击(此处)折叠或打开

  1. #/bin/bash
  2. #FileName:Convert_Storage_Engine.sh
  3. #Desc:Conversion of a MySQL tables to other storage engines
  4. #Create By:fedoracle
  5. #Date:2012/06/27

  6. DB=new
  7. USER=test
  8. PASSWD=test
  9. HOST=192.168.25.121
  10. MYSQL_BIN=/usr/local/mysql/bin
  11. S_ENGINE=MyISAM
  12. D_ENGINE=InnoDB

  13. #echo "Enter MySQL bin path:"
  14. #read MYSQL_BIN
  15. #echo "Enter Host:"
  16. #read HOST
  17. #echo "Enter Uesr:"
  18. #read USER
  19. #echo "Enter Password:"
  20. #read PASSWD
  21. #echo "Enter DB name :"
  22. #read DB
  23. #echo "Enter the original engine:"
  24. #read S_ENGINE
  25. #echo "Enter the new engine:"
  26. #read D_ENGINE

  27. $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
  28. for t_name in `cat tables.txt`
  29. do
  30.     echo "Starting convert table $t_name......"
  31.     sleep 1
  32.     $MYSQL_BIN/mysql -h$HOST -u$USER -p$PASSWD $DB -e "alter table $t_name engine='"$D_ENGINE"'"
  33.     if [ $? -eq 0 ]
  34.     then
  35.         echo "Convert table $t_name ended." >>con_table.log
  36.         sleep 1
  37.     else
  38.         echo "Convert failed!" >> con_table.log
  39.     fi
  40. done

测试过程如下:

点击(此处)折叠或打开

  1. [root@dbmaster ~]# mysql -h192.168.25.121 -utest -ptest
  2. (test@192.168.25.121) [(none)] create database new;
  3. Query OK, 1 row affected (0.01 sec)

  4. (test@192.168.25.121) [(none)] show databases;
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | 361 |
  10. | mysql |
  11. | new |
  12. | performance_schema |
  13. | test |
  14. +--------------------+
  15. 6 rows in set (0.00 sec)

  16. [root@dbmaster ~]# mysql -h192.168.25.121 -utest -ptest new < 361.sql
  17. (test@192.168.25.121) [(none)] use new;
  18. Database changed
  19. (test@192.168.25.121) [new] show tables;
  20. +---------------------------+
  21. | Tables_in_new |
  22. +---------------------------+
  23. | ad_magazine_content |
  24. | ad_news_letter |
  25. | conf_app |
  26. | ip_province |
  27. | ip_records |
  28. | order_action |
  29. | order_delivery |
  30. | order_goods |
  31. ................................

  32. (test@192.168.25.121) [information_schema] select TABLE_NAME,ENGINE from TABLES where TABLE_SCHEMA='new' and ENGINE='MyISAM';
  33. +--------------------------+--------+
  34. | TABLE_NAME | ENGINE |
  35. +--------------------------+--------+
  36. | ad_news_letter | MyISAM |
  37. | conf_app | MyISAM |
  38. | product_lib_attr_group | MyISAM |
  39. | product_lib_brand | MyISAM |
  40. | product_lib_ccard | MyISAM |
  41. | product_lib_color | MyISAM |
  42. | product_lib_fashion | MyISAM |
  43. | product_lib_material | MyISAM |
  44. | product_lib_season | MyISAM |
  45. | product_lib_series | MyISAM |
  46. | product_lib_size | MyISAM |
  47. | product_lib_size_compare | MyISAM |
  48. | product_lib_temperature | MyISAM |
  49. | product_lib_type | MyISAM |
  50. | product_lib_virtual_cat | MyISAM |
  51. | req_conf_app | MyISAM |
  52. | shop_keywords_details | MyISAM |
  53. | system_api_user | MyISAM |
  54. | system_payment | MyISAM |
  55. | system_region | MyISAM |
  56. | system_shop_dist | MyISAM |
  57. | user_show_order | MyISAM |
  58. +--------------------------+--------+
  59. 22 rows in set (0.02 sec)

  60. [root@dbmaster scripts]# bash ChangeStorageEngine.sh
  61. Starting convert table ad_news_letter......
  62. Starting convert table conf_app......
  63. Starting convert table product_lib_attr_group......
  64. Starting convert table product_lib_brand......
  65. Starting convert table product_lib_ccard......
  66. Starting convert table product_lib_color......
  67. Starting convert table product_lib_fashion......
  68. Starting convert table product_lib_material......
  69. Starting convert table product_lib_season......
  70. Starting convert table product_lib_series......
  71. Starting convert table product_lib_size......
  72. Starting convert table product_lib_size_compare......
  73. Starting convert table product_lib_temperature......
  74. Starting convert table product_lib_type......
  75. ...............................

  76. (test@192.168.25.121) [information_schema] select TABLE_NAME,ENGINE from TABLES where TABLE_SCHEMA='new' and ENGINE='MyISAM';
  77. Empty set (0.01 sec)

  78. [root@dbmaster scripts]# cat con_table.log 
  79. Convert table ad_news_letter ended.
  80. Convert table conf_app ended.
  81. Convert table product_lib_attr_group ended.
  82. Convert table product_lib_brand ended.
  83. Convert table product_lib_ccard ended.
  84. Convert table product_lib_color ended.
  85. Convert table product_lib_fashion ended.
  86. Convert table product_lib_material ended.
  87. Convert table product_lib_season ended.
  88. Convert table product_lib_series ended.
  89. Convert table product_lib_size ended.
  90. Convert table product_lib_size_compare ended.
  91. Convert table product_lib_temperature ended.
  92. Convert table product_lib_type ended.
  93. Convert table product_lib_virtual_cat ended.
  94. Convert table req_conf_app ended.
  95. Convert table shop_keywords_details ended.
  96. Convert table system_api_user ended.
  97. Convert table system_payment ended.
  98. Convert table system_region ended.
  99. Convert table system_shop_dist ended.
  100. Convert table user_show_order ended.
#############################################################################################
有些表在转换的时候由于字符集,字段长度,外键约束等原因会出现一些问题,如下

点击(此处)折叠或打开

  1. ERROR 1217 (23000) at line 1: Cannot delete or update a parent row: a foreign key constraint fails
  2. ERROR 1071 (42000) at line 1: Specified key was too long; max key length is 1000 bytes


上一篇:CentOS6.2下chrome(19.0.1084.56)安装adobe flash player
下一篇:MySQL监控应该知道的九件事