千家信息网

MySQL怎么批量修改存储引擎

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,本篇内容主要讲解"MySQL怎么批量修改存储引擎",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MySQL怎么批量修改存储引擎"吧!再看MySQL手册,看到
千家信息网最后更新 2025年02月06日MySQL怎么批量修改存储引擎

本篇内容主要讲解"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. 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 -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. (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 -h292.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

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

引擎 存储 内容 脚本 学习 实用 更深 兴趣 原因 字段 字符 字符集 实用性 实际 手册 操作简单 方法 时候 更多 朋友 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 如何根据表格行号执行数据库操作 戴尔服务器e1000 东平app软件开发 计算机网络技术人员四级有什么用 汕头智能软件开发费用是多少 软件开发任务管理工具 sql取数据库总数 access数据库属于什么数据库 软件开发建设原则 中国影响力人物数据库两会 扁桃方舟服务器 武昌订制软件开发中心 福州市一中网络技术全国第三 上海知名服务器代理厂家 个体户开票服务器连接异常 运满满网络安全检查大概要多久 sql 备份一个表数据库 杭州网络技术怎么样 创建数据库时设置两个字段为外键 抖音阿雷玩的恐龙岛哪个服务器 新媒体时代网络素养及网络安全 网络安全工程师周记 抖音直播怎么显示服务器 非关系型数据库完整性好吗 建立在数据库以工厂核心技术 先进网络技术生产过程 电脑服务器没有及时响应怎么办 北京口碑好的软件开发价格 海康智能多媒体服务器装系统 我的世界服务器大全群
0