我知道我可以单独发出一个alter表,将表存储从MyISAM更改为InnoDB。
我想知道是否有一种方法可以快速将它们全部更改为InnoDB?
我知道我可以单独发出一个alter表,将表存储从MyISAM更改为InnoDB。
我想知道是否有一种方法可以快速将它们全部更改为InnoDB?
当前回答
如果您使用的是Windows,您可以在批处理文件中使用以下循环完成此任务。
set database=YOURDATABASENAME
for /F "tokens=1 skip=1 usebackq" %%a in (`mysql %%database%% -e "show table status where Engine != 'InnoDB';"`) do (
mysql %database% -e "ALTER TABLE %%a ENGINE = 'InnoDB';"
)
只需将YOURDATABASENAME更改为目标数据库的名称,或者使用%~1通过命令行传递数据库名称。
所有当前不是InnoDB的表都将被转换为InnoDB。如果您像问题所建议的那样专门针对MyISAM,下面的代码有一个仅针对MyISAM的更新的MySQL条件。
set database=YOURDATABASENAME
for /F "tokens=1 skip=1 usebackq" %%a in (`mysql %%database%% -e "show table status where Engine = 'MyISAM';"`) do (
mysql %database% -e "ALTER TABLE %%a ENGINE = 'InnoDB';"
)
其他回答
一行:
mysql -u root -p dbName -e
"show table status where Engine='MyISAM';" | awk
'NR>1 {print "ALTER TABLE "$1" ENGINE = InnoDB;"}' |
mysql -u root -p dbName
这很简单。只有两步。
复制,粘贴并运行: SET @DATABASE_NAME = ' name_your_db '; SELECT CONCAT('ALTER TABLE ", table_name, ' ENGINE=InnoDB;') AS sql_statements FROM information_schema。TABLE AS tb WHERE ' ENGINE ' = 'MyISAM' AND ' TABLE_TYPE ' = 'BASE TABLE'
(复制粘贴所有结果在SQL选项卡)
将所有结果复制到SQL选项卡并在下面一行中粘贴。 开始事务; 提交;
例如:
START TRANSACTION;
ALTER TABLE `admin_files` ENGINE=InnoDB;
COMMIT;
在mysql中,你可以使用文本编辑器进行搜索/替换:
SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'myisam';
注意:你可能应该忽略information_schema和mysql,因为“mysql和information_schema数据库,实现了一些mysql内部,仍然使用MyISAM。特别是,你不能切换授权表来使用InnoDB。”(http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html)
在任何情况下,请注意要忽略并运行的表:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'myisam';
现在只需复制/粘贴列表到你的文本编辑器,搜索/替换“|”与“ALTER TABLE”等。
然后你会有一个这样的列表,你可以简单地粘贴到你的mysql终端:
ALTER TABLE arth_commentmeta ENGINE=Innodb;
ALTER TABLE arth_comments ENGINE=Innodb;
ALTER TABLE arth_links ENGINE=Innodb;
ALTER TABLE arth_options ENGINE=Innodb;
ALTER TABLE arth_postmeta ENGINE=Innodb;
ALTER TABLE arth_posts ENGINE=Innodb;
ALTER TABLE arth_term_relationships ENGINE=Innodb;
ALTER TABLE arth_term_taxonomy ENGINE=Innodb;
ALTER TABLE arth_terms ENGINE=Innodb;
ALTER TABLE arth_usermeta ENGINE=Innodb;
如果你的文本编辑器不能很容易地做到这一点,这里有另一个解决方案,为一个类似的列表(你可以粘贴到mysql),你的数据库只有一个前缀,从linux终端:
mysql -u [username] -p[password] -B -N -e 'show tables like "arth_%"' [database name] | xargs -I '{}' echo "ALTER TABLE {} ENGINE=INNODB;"
在下面的脚本中,用特定的数据替换<用户名>、<密码>和<模式>。
要显示可以复制粘贴到mysql客户端会话的语句,输入以下命令:
echo 'SHOW TABLES;' \
| mysql -u <username> --password=<password> -D <schema> \
| awk '!/^Tables_in_/ {print "ALTER TABLE `"$0"` ENGINE = InnoDB;"}' \
| column -t \
要简单地执行更改,使用以下命令:
echo 'SHOW TABLES;' \
| mysql -u <username> --password=<password> -D <schema> \
| awk '!/^Tables_in_/ {print "ALTER TABLE `"$0"` ENGINE = InnoDB;"}' \
| column -t \
| mysql -u <username> --password=<password> -D <schema>
图片来源:这是本文所概述内容的一个变体。
我是一个新手,必须找到自己的解决方案,因为网上的mysql命令通常充满了拼写错误,这对刚开始使用的人来说是一个现实生活的噩梦。这是我的解决方案....
我用excel一次准备了几十个命令(可以复制和粘贴),而不是每个表一个命令。
How? expand your putty window and enter mysql and then run the command "SHOW TABLE STATUS;" and the copy/paste the output to microsoft excel. Go to the Data tab and use the "text to columns" feature an delimit the columns by a space key. Then Sort the columns by whichever column shows your table types and delete all rows which the tables are already in InnoDb format (because we don't need to run commands against them, they are already done). Then add 2 columns to the left of the tables column, and 2 columns to the right. Then paste in the first part of the command in column-1 (see below). Column 2 should contain only a space. Column 3 is your tables column. Column 4 should contain only a space. Column 5 is the last part of your command. It should look like this:
column-1 column-2 column-3 column-4 column-5
ALTER TABLE t_lade_tr ENGINE=InnoDB;
ALTER TABLE t_foro_detail_ms ENGINE=InnoDB;
ALTER TABLE t_ljk_ms ENGINE=InnoDB;
然后每次复制粘贴5行到mysql中。这将一次转换大约5个。我注意到,如果我一次做了更多的操作,那么命令就会失败。