Code前端首页关于Code前端联系我们

将Mysql表引擎转换为InnoDB的批量方法

terry 2年前 (2023-09-26) 阅读数 44 #数据库

数据库引擎批量修改MYSQL中的特定表可以使用官方工具mysql_convert_table_format来实现。这展示了如何仅使用 shell 而不使用任何其他工具。 (下面例子的效果是将数据库shop中所有引擎不是InnoDB的表改为使用InnoDB引擎)【参见Table引擎的语句: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库中InnoDB的所有其他机器更改语句shop_to_innodb)。

查看导出的编辑语句

使用命令vim 或命令cat 查看查看no_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

如果数据库很大,需要一些时间

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门