我知道我可以单独发出一个alter表,将表存储从MyISAM更改为InnoDB。
我想知道是否有一种方法可以快速将它们全部更改为InnoDB?
我知道我可以单独发出一个alter表,将表存储从MyISAM更改为InnoDB。
我想知道是否有一种方法可以快速将它们全部更改为InnoDB?
当前回答
对这个util脚本的一些修复
SET @DATABASE_NAME = 'Integradb';
SELECT CONCAT('ALTER TABLE ', table_schema, '.', table_name, ' ENGINE=InnoDB;') AS sql_statements
FROM information_schema.tables AS tb
WHERE table_schema = @DATABASE_NAME
AND `ENGINE` = 'MyISAM'
AND `TABLE_TYPE` = 'BASE TABLE'
ORDER BY table_name DESC;
其他回答
如果您使用的是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';"
)
<?php
// connect your database here first
mysql_connect('host', 'user', 'pass');
$databases = mysql_query('SHOW databases');
while($db = mysql_fetch_array($databases)) {
echo "database => {$db[0]}\n";
mysql_select_db($db[0]);
$tables = mysql_query('SHOW tables');
while($tbl = mysql_fetch_array($tables)) {
echo "table => {$tbl[0]}\n";
mysql_query("ALTER TABLE {$tbl[0]} ENGINE=InnoDB");
}
}
遵循步骤:
Use MySql commands as follows, for converting to InnoDB (ALTER TABLE t1 ENGINE = InnoDB) or (ALTER TABLE t1 ENGINE = MyISAM) for MyISAM (You should do this for each individual tables, t1 is for the table name.). Write a script that loops on all tables and run the alter command Use an already available script to handle that: https://github.com/rafihaidari/convert-mysql-tables-storage-engine Try this SQL to Get all info will get all the tables information then you can change all the table from isam to InnoDB SELECT CONCAT('ALTER TABLE ',TABLE_NAME,' ENGINE=InnoDB;') FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='MyISAM' AND table_schema = 'your_DB_Name';
在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;"
试试这个shell脚本
DBENGINE='InnoDB' ;
DBUSER='your_db_user' ;
DBNAME='your_db_name' ;
DBHOST='your_db_host'
DBPASS='your_db_pass' ;
mysqldump --add-drop-table -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME > mtest.sql; mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME -Nse "SHOW TABLES;" | while read TABLE ; do mysql -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME -Nse "ALTER TABLE $TABLE ENGINE=$DBENGINE;" ; done