批量修改Mysql表引擎为InnoDB的方法

x33g5p2x  于2021-03-14 发布在 Mysql  
字(1.9k)|赞(0)|评价(0)|浏览(506)

一般批量修改MYSQL中某表的数据库引擎可以利用官方工具mysql_convert_table_format来实现, 这里指的是不使用其他工具仅用shell的方法来实现。(以下例子效果是将数据库shop中所有引擎不为InnoDB的表修改为使用InnoDB引擎)[ 查看表引擎的语句:show create table tableName; ],其实核心关键点是这条语句:

alert table tableName engine=innodb;

下面开始修改:

备份数据库

在命令行下运行得到备份SQL

mysqldump -uroot -p******** shop > shop20160505.sql

在命令行建立一个测试数据库并导入SQL

mysql -uroot -p******** -e 'create database shop_to_innodb charset utf8';
mysql -uroot -p******** shop_to_innodb < shop20160505.sql

shell获取需要更换表引擎

mysql -uroot -p******** -e "show table status from shop_to_innodb where Engine <> 'InnoDB' \G"|grep Name|awk '{print "alter table "$2" engine=innodb;";}' > mysql_change_to_innodb.txt

上面的语句在命令行中执行得到 mysql_change_to_innodb.txt 文件(包含了shop_to_innodb库中所有引擎不为InnoDB的改变语句)。

查看导出的修改语句

使用vim命令或者cat命令查看 mysql_change_to_innodb.txt 文件,检查是否为自己要修改的内容。

alter table a engine=innodb;
alter table b engine=innodb;
alter table category engine=innodb;
alter table ecs_account_log engine=innodb;
alter table ecs_ad engine=innodb;
alter table ecs_ad_custom engine=innodb;
alter table ecs_ad_position engine=innodb;
alter table ecs_admin_action engine=innodb;
alter table ecs_admin_log engine=innodb;
alter table ecs_admin_message engine=innodb;
alter table ecs_admin_user engine=innodb;
alter table ecs_adsense engine=innodb;
alter table ecs_affiliate_log engine=innodb;
alter table ecs_agency engine=innodb;
alter table ecs_area_region engine=innodb;
alter table ecs_article engine=innodb;
alter table ecs_article_cat engine=innodb;
alter table ecs_attribute engine=innodb;
alter table ecs_auction_log engine=innodb;
alter table ecs_auto_manage engine=innodb;
alter table ecs_back_goods engine=innodb;
alter table ecs_back_order engine=innodb;
alter table ecs_bonus_type engine=innodb;
alter table ecs_booking_goods engine=innodb;
alter table ecs_brand engine=innodb;
alter table ecs_card engine=innodb;
alter table ecs_cart engine=innodb;
... ...

执行修改语句

如果没用问题我们可以接着执行下面的语句。

mysql -uroot -p******** shop_to_innodb < mysql_change_to_innodb.txt

如果数据库非常大可能要等一段时间

相关文章