文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

MySQL怎么批量修改存储引擎

2024-04-02 19:55

关注

本篇内容主要讲解“MySQL怎么批量修改存储引擎”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL怎么批量修改存储引擎”吧!

再看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.  

  7. DB=new

  8. USER=test

  9. PASSWD=test

  10. HOST=192.168.25.121

  11. MYSQL_BIN=/usr/local/mysql/bin

  12. S_ENGINE=MyISAM

  13. D_ENGINE=InnoDB

  14.  

  15. #echo "Enter MySQL bin path:"

  16. #read MYSQL_BIN

  17. #echo "Enter Host:"

  18. #read HOST

  19. #echo "Enter Uesr:"

  20. #read USER

  21. #echo "Enter Password:"

  22. #read PASSWD

  23. #echo "Enter DB name :"

  24. #read DB

  25. #echo "Enter the original engine:"

  26. #read S_ENGINE

  27. #echo "Enter the new engine:"

  28. #read D_ENGINE

  29.  

  30. $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

  31. for t_name in `cat tables.txt`

  32. do

  33.     echo "Starting convert table $t_name......"

  34.     sleep 1

  35.     $MYSQL_BIN/mysql -h$HOST -u$USER -p$PASSWD $DB -e "alter table $t_name engine='"$D_ENGINE"'"

  36.     if [ $? -eq 0 ]

  37.     then

  38.         echo "Convert table $t_name ended." >>con_table.log

  39.         sleep 1

  40.     else

  41.         echo "Convert failed!" >> con_table.log

  42.     fi

  43. done

测试过程如下:

点击(此处)折叠或打开

  1. [root@dbmaster ~]# mysql -h292.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.  

  5. (test@192.168.25.121) [(none)] show databases;

  6. +--------------------+

  7. | Database |

  8. +--------------------+

  9. | information_schema |

  10. | 361 |

  11. | mysql |

  12. | new |

  13. | performance_schema |

  14. | test |

  15. +--------------------+

  16. 6 rows in set (0.00 sec)

  17.  

  18. [root@dbmaster ~]# mysql -h292.168.25.121 -utest -ptest new < 361.sql

  19. (test@192.168.25.121) [(none)] use new;

  20. Database changed

  21. (test@192.168.25.121) [new] show tables;

  22. +---------------------------+

  23. | Tables_in_new |

  24. +---------------------------+

  25. | ad_magazine_content |

  26. | ad_news_letter |

  27. | conf_app |

  28. | ip_province |

  29. | ip_records |

  30. | order_action |

  31. | order_delivery |

  32. | order_goods |

  33. ................................

  34.  

  35. (test@192.168.25.121) [information_schema] select TABLE_NAME,ENGINE from TABLES where TABLE_SCHEMA='new' and ENGINE='MyISAM';

  36. +--------------------------+--------+

  37. | TABLE_NAME | ENGINE |

  38. +--------------------------+--------+

  39. | ad_news_letter | MyISAM |

  40. | conf_app | MyISAM |

  41. | product_lib_attr_group | MyISAM |

  42. | product_lib_brand | MyISAM |

  43. | product_lib_ccard | MyISAM |

  44. | product_lib_color | MyISAM |

  45. | product_lib_fashion | MyISAM |

  46. | product_lib_material | MyISAM |

  47. | product_lib_season | MyISAM |

  48. | product_lib_series | MyISAM |

  49. | product_lib_size | MyISAM |

  50. | product_lib_size_compare | MyISAM |

  51. | product_lib_temperature | MyISAM |

  52. | product_lib_type | MyISAM |

  53. | product_lib_virtual_cat | MyISAM |

  54. | req_conf_app | MyISAM |

  55. | shop_keywords_details | MyISAM |

  56. | system_api_user | MyISAM |

  57. | system_payment | MyISAM |

  58. | system_region | MyISAM |

  59. | system_shop_dist | MyISAM |

  60. | user_show_order | MyISAM |

  61. +--------------------------+--------+

  62. 22 rows in set (0.02 sec)

  63.  

  64. [root@dbmaster scripts]# bash ChangeStorageEngine.sh

  65. Starting convert table ad_news_letter......

  66. Starting convert table conf_app......

  67. Starting convert table product_lib_attr_group......

  68. Starting convert table product_lib_brand......

  69. Starting convert table product_lib_ccard......

  70. Starting convert table product_lib_color......

  71. Starting convert table product_lib_fashion......

  72. Starting convert table product_lib_material......

  73. Starting convert table product_lib_season......

  74. Starting convert table product_lib_series......

  75. Starting convert table product_lib_size......

  76. Starting convert table product_lib_size_compare......

  77. Starting convert table product_lib_temperature......

  78. Starting convert table product_lib_type......

  79. ...............................

  80.  

  81. (test@192.168.25.121) [information_schema] select TABLE_NAME,ENGINE from TABLES where TABLE_SCHEMA='new' and ENGINE='MyISAM';

  82. Empty set (0.01 sec)

  83.  

  84. [root@dbmaster scripts]# cat con_table.log 

  85. Convert table ad_news_letter ended.

  86. Convert table conf_app ended.

  87. Convert table product_lib_attr_group ended.

  88. Convert table product_lib_brand ended.

  89. Convert table product_lib_ccard ended.

  90. Convert table product_lib_color ended.

  91. Convert table product_lib_fashion ended.

  92. Convert table product_lib_material ended.

  93. Convert table product_lib_season ended.

  94. Convert table product_lib_series ended.

  95. Convert table product_lib_size ended.

  96. Convert table product_lib_size_compare ended.

  97. Convert table product_lib_temperature ended.

  98. Convert table product_lib_type ended.

  99. Convert table product_lib_virtual_cat ended.

  100. Convert table req_conf_app ended.

  101. Convert table shop_keywords_details ended.

  102. Convert table system_api_user ended.

  103. Convert table system_payment ended.

  104. Convert table system_region ended.

  105. Convert table system_shop_dist ended.

  106. 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

到此,相信大家对“MySQL怎么批量修改存储引擎”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯